8

How can I detect key presses reliably with a hard or soft keyboard?

My app remotely controls another device over wifi, and I need to detect every key press on either a soft or hard keyboard. I don't really need an EditText because I just need to send the characters one at a time as they are pressed and don't need the final text string.

I have tried using an EditText with OnKeyPress, but ran into the problems here with not getting key presses with soft keyboards. And TextWatcher isn't a good option because I need each key press.

I'll use an EditText if I have to, but would prefer not to. What I really want is to:

  • Bring up a soft keyboard when the user hits a Search button
  • User presses keys and I transmit the codes to remote device. Don't really need to see anything on screen in an EditText since it will be shown on the remote device
  • User presses the custom Done button on the soft keyboard to close it

Any suggestions?

Community
  • 1
  • 1
Gregg Reno
  • 441
  • 4
  • 15

1 Answers1

1

Well you could override the [onKeyDown(int keyCode, KeyEvent event)][1] and(or) [onKeyUp(int keyCode, KeyEvent event)][2] methods in the applications activity class, this would allow you to get notification even about such keys as the back key and other hardware keys...

Note: you can get notification about trackball movement and so on...

[1]: http://developer.android.com/reference/android/view/View.html#onKeyDown(int, android.view.KeyEvent) [2]: http://developer.android.com/reference/android/view/View.html#onKeyUp(int, android.view.KeyEvent)

Marek Szanyi
  • 2,348
  • 2
  • 22
  • 28
  • 1
    Thanks, but this doesn't seem to work with a soft keyboard. I'm overriding onKeyDown and it isn't triggered with letters or numbers. – Gregg Reno Nov 23 '10 at 20:59