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

人盡茶涼 提交于 2019-12-11 06:58:14

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/20170681/onkeydown-not-fire-when-pressing-left-right-up-down-arrow-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!