Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AccessibilityService is not responding on OnKeyEvent()

Hi I am building an accessibility service. I wanted to deal with the keyboard inputs, and also determine BACK key events. I guess my best shot is overriding the onKeyEvent() callback. But What I found out is that it is even never being called. I tried to add android:canRequestFilterKeyEvents="true" in configuration XML and also in the onServiceConnected module I added

info.flags=AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS;
        info.flags=AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS;
        setServiceInfo(info);

But still no luck. Looks like the even onKeyEvent is never raised.

like image 910
P basak Avatar asked Oct 29 '25 18:10

P basak


1 Answers

If you want to handle the events of the homebutton and back button, you have to add this:

In your xml service declareted:

1-android:canRequestFilterKeyEvents="true"
2-Add flagRequestFilterKeyEvents to your android:accessibilityFlags

In your accessibility service class add onKeyEvent:

  @Override
        public boolean onKeyEvent(KeyEvent event) {
            int keyCode = event.getKeyCode();

            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    Log.e(TAG, "Back");

                case KeyEvent.KEYCODE_HOME:
                    Log.e(TAG, "Home");
                    return false;
            }
            return super.onKeyEvent(event);
        }
like image 112
S.P. Avatar answered Oct 31 '25 10:10

S.P.