EastBK
EastBK

Reputation: 411

onKeyDown not fire when pressing Left, Right, Up, Down arrow in Android

OnKeyDown callback in InputMethodService works for a lot of hardware keyboard buttons but It doesn't fire with Left, Right, Up, Down arrow. How can I handle it?

Thanks for your attention!

Upvotes: 1

Views: 6860

Answers (1)

brendan
brendan

Reputation: 1735

Have you looked at the developer documentation on handling key events? You could try onKeyDown or onKeyUp to only receive one event and print out the keyCode to see what is getting received:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
  Log.i("your tag", "Keycode: " + keyCode);
  switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_LEFT:
        leftKeyClick();
        return true;
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        rightKeyClick();
        return true;
    case KeyEvent.KEYCODE_DPAD_UP:
        upKeyClick();
        return true;
    case KeyEvent.KEYCODE_DPAD_DOWN:
        downKeyClick();
        return true;
    default:
        return super.onKeyUp(keyCode, event);
  }
}

The KeyEvent Object reference gives more details. If you are still not detecting the arrow keys then you may need to override the onKeyDown or onKeyUp method of your View, this similar question might be useful.

Upvotes: 6

Related Questions