androidkeyeventaccessibilityserviceandroid-homebutton

What API level is needed to get KEYCODE_HOME KeyEvent in AccessibilityService?


Pressing the physical home buttons of two test devices results in getting the KEYCODE_HOME KeyEvent in my AccessibilityService only on one of the devices. Same manufacturer, different model, different API level. Is a certain API level needed to get the physical home button's KeyEvent?

@Override
protected boolean onKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KEYCODE_HOME)
        Log.d(TAG, "The event works on this device.");
    return super.onKeyEvent(event);
}

Solution

  • I've created and tested 2 virtual devices with different API levels in Android Emulator. It seems like the API level doesn't matter, as long as the virtual device's physical home button is used. So if a device doesn't get the KEYCODE_HOME KeyEvent in the AccessibilityService, the culprit is probably something other than the API level.

    The question mentions that not only the API levels differ, but the models too. The physical home button doesn't result in a KeyEvent on certain devices. E.g. the home button of the Samsung Galaxy S8, S8+, S9, S9+, Note10, Note10+ and Fold devices are officially considered a pressure sensor, not a button. To use this pressure sensor (the home button), see this question:

    How to read the pressure sensor?

    Screenshot of Samsung Galaxy S9 and S9+ specs on https://www.samsung.com/global/galaxy/galaxy-s9/specs/. Sensors: Iris, Fingerprint, HR, Pressure (!), Gyro, Proximity, Accelerometer, Geomagnetic, RGB Light, Barometer, Hall sensor. Buttons: On/Off, Volume, Bixby. (Home key is not on the list.)