就我而言,我需要在将所有关键事件分派到目标之前拦截它们。所以我在每个视图或小部件上安装了一个 KeyListener,Activity.onKey 在这种情况下不能满足我。
但是,我发现如果视图(如RelativeLayout)不可聚焦,它将不会接收任何KeyEvent。我还收到以下警告
警告/KeyCharacterMap(312): id 0 没有键盘
WARN/KeyCharacterMap(312):使用默认键盘映射:/system/usr/keychars/qwerty.kcm.bin
如果我强制视图可聚焦,它将接收 KeyEvent。我对这个现象很好奇。我不知道系统是否从不发送 KeyEvents 来查看哪些无法聚焦,或者只是我使用了错误的方式来拦截关键事件。
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)
为此,请使用
dispatchKeyEvent
(请参阅 http://developer.android.com/reference/android/view/Window.Callback.html#dispatchKeyEvent%28android.view.KeyEvent%29) 在活动上。这应该接收每个 KeyEvent。如果您希望启动标准处理,您可以进行任何修改,然后调用super.dispatchKeyEvent
。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 callsuper.dispatchKeyEvent
if you want the standard processing to kick in.