Android onKeyDown not catching Dpad center and enter key presses

user1403565 picture user1403565 · Jul 20, 2012 · Viewed 7.4k times · Source

Doing a simple override in my base activity of onKeyDown, I'm able to capture all key presses except those of the enter and dpad center buttons (as determined via breakpoint). I've no clue as to why - can anyone shed some light on the situation?

EDIT: Quick update - it does capture Dpad center and enter key LONG presses, but still not normal presses.

Answer

tagtraeumer picture tagtraeumer · Jul 26, 2015

I know this question is already pretty old but in case some desperate coder got lost I post my answer.

I had a similar problem with my USB keyboard. When anything else except a EditText box was focussed the ENTER key was never caught by onKeyUp or onKeyDown.

if you use dispatchKeyEvent() you get the KeyEvent before it reaches the window and in my case I definitely get the ENTER key. But cautious, the event is called twice, once for key down and once for key up.

here a sample code:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    System.out.println(event.getAction() + " " + event.getKeyCode() + " - " + (char) event.getUnicodeChar());

    return true;
}