You can either set the listener to a View
that has current focus, or use dispatchKeyEvent
in your Activity
, as seen in this answer.
Both will work.
Answer for your comments: If you've followed my link, you've probably implemented Activity.dispatchKeyEvent(KeyEvent)
by now. :-)
The code doesnt register anykey i press, but when i push the back key
it toasts for me "didnt work"
Do you happen to be using the emulator? I ask that because the latest SDK seems to have some problems with the emulator keyboard. The special keys (DPAD, HOME, BACK etc.) work, but the onscreen, QWERTY keyboard does not register any presses. The physical keyboard in my laptop won't register any presses either.
Don't ask me why.
And I say that because this week I posted the answer in the link, and it was working fine. The change is that I updated the Android SDK to R20/JB this morning, so I guess it could be a factor.
However, it will just work on a real device. I've just connected a physical keyboard to my tablet (P7510 / Honeycomb 3.2), and it listens to space presses just fine.
In case you're still doubting, here is proof: :-)
