Neither dispatchKeyEvent or onKeyListener capturing events in Android?

1k Views Asked by At

My code is not reporting any key events except DELETE and ENTER (maybe others), yet it should be reporting all keys. I have another app using dispatchKeyEvent and it works fine. I've tried matching the targetSDK, buildAPI, and minimum to that app in case it was an api bug as mentioned elsewhere, but none of that seemed to make any difference. I have not built the other app since updating AndroidSDK to 4.4.

Is there something I am missing?

public boolean dispatchKeyEvent(KeyEvent event) {
    Log.e("TEST","test " + " "+event.getKeyCode());
    return super.dispatchKeyEvent(event);
}

Does nothing for most keys (letters, tab, etc) now ↑

1

There are 1 best solutions below

0
On

From the KeyEvent docs:

As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later, and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier. Be aware that...

So you should never expect to get key events because users can run any different keyboard they feel like installing, and those keyboards may or may not pass the events through to be caught.

To the extent you do get key events, it's against Google's recommendation.