Android: No KeyEvent on unfocusable view?

时光毁灭记忆、已成空白 提交于 2019-12-12 03:47:26

问题


In my case, I'm required to intercept all key events before they are dispatched to target. So I installed a KeyListener on every view or widget, Activity.onKey can't satisfy me in the case.

However, I found that if view (like RelativeLayout) is not focusable, it won't receive any KeyEvent. And I also got following warnings

WARN/KeyCharacterMap(312): No keyboard for id 0
WARN/KeyCharacterMap(312): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

If I forced view to be focusable, it's going to receive KeyEvent. I'm curious about this phenomenon. I've no idea about if system never sends KeyEvents to view which is unfocusable or just I used wrong way to intercept key events.


回答1:


To do this use dispatchKeyEvent (see http://developer.android.com/reference/android/view/Window.Callback.html#dispatchKeyEvent%28android.view.KeyEvent%29) on the Activity. This should receive every KeyEvent. You can do any modifications and then call super.dispatchKeyEvent if you want the standard processing to kick in.



来源:https://stackoverflow.com/questions/6093465/android-no-keyevent-on-unfocusable-view

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