Search code examples
javaandroidwear-os

Wear OS onKeyDown not firing for Ticwatch E


I have a Ticwatch E connected using ADB and I'm viewing the logcat. There is only one button on this device but when I press it, my onKeyDown method does not fire. I've been reading the Android documentation on Physical buttons, and have followed what they have specified. Here is my code:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.d(TAG, "onKeyDown: keyCode=" + keyCode + "; keyEvent=" + event.toString());

    if (event.getRepeatCount() == 0) {
        if (keyCode == KeyEvent.KEYCODE_0) {
            Log.d(TAG, "ShowTokenActivity.onKeyDown: KeyEvent.KEYCODE_0");
        } else if (keyCode == KeyEvent.KEYCODE_1) {
            Log.d(TAG, "ShowTokenActivity.onKeyDown: KeyEvent.KEYCODE_1");
        } else if (keyCode == KeyEvent.KEYCODE_2) {
            Log.d(TAG, "ShowTokenActivity.onKeyDown: KeyEvent.KEYCODE_2");
        } else if (keyCode == KeyEvent.KEYCODE_3) {
            Log.d(TAG, "ShowTokenActivity.onKeyDown: KeyEvent.KEYCODE_3");
        }
    }

    return super.onKeyDown(keyCode, event);
}

My stackoverflow searching uncovered this:

I also read Use Key Events Directly from the android developer site which has an example with similar code to what I have above. The only difference is that the Activity class is final.


Solution

  • I believe the first one, is not available on Wear OS 2. And the first two are not available for apps in Wear OS 3. From the docs.

    Note: Wear OS 3.0 reserves two buttons for the OS, in contrast to Wear OS 2.0 which reserved only one. This reduces the number of buttons you can assign actions to.