Search code examples
androidandroid-studiowear-os

detect middle (rotary) button click on android waer


I am developing an app for Android Wear and are using the hardware buttons. I can manage to catch the buttons with the onKeyDown override:

    public boolean onKeyDown(int keyCode, KeyEvent event){
      Log.i("CLICK", Integer.toString(keyCode));
      if (event.getRepeatCount() == 0) {
        if (keyCode == KeyEvent.KEYCODE_STEM_1) {
          // Do stuff
          return true;
        } else if (keyCode == KeyEvent.KEYCODE_STEM_2) {
          // Do stuff
          return true;
        } else if (keyCode == KeyEvent.KEYCODE_STEM_3) {
          // Do stuff
          return true;
        }
      }
      return super.onKeyDown(keyCode, event);
    }

I would also like to catch a click on the middle (rotary) button, but when I click this button, the watch is going back to the default 'home'(watchface)-screen, and no event is being logged.

I can manage to catch the rotary scrolling, but it's the click I'd like to use.

    @Override
    public boolean onGenericMotionEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_SCROLL && RotaryEncoder.isFromRotaryEncoder(ev)) {
            // // Note that we negate the delta value here in order to get the right scroll direction.
            // float delta = -RotaryEncoder.getRotaryAxisValue(ev)
            //         * RotaryEncoder.getScaledScrollFactor(getContext());
            // scrollBy(0, Math.round(delta));
            return true;
        }
        return super.onGenericMotionEvent(ev);
    }

Is this possible, or is it impossible to override the functionallity of the middle watch button?


Solution

  • AFAIU It should be getting KEYCODE_HOME as key event on pressing RSB in onKeyDown(). If it is the case then it is controlled by Android framework only and apps can't do anything with it.

    Here is the official description

    Key code constant: Home key. This key is handled by the framework and is never delivered to applications.

    https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HOME