onKeyListener not working with soft keyboard (Android)

I am using onKeyListener to get the onKey events. It works fine with the normal keyboard. But it does not work with soft keyboard. I am only able to get onKey events for numerics and not alphabets. Is there any workaround to solve this? Any kind of help will be greatly appreciated.


I don't believe an OnKeyListener gets called at all with the software keyboard. It has something to do with the software keyboard being an IME device and IME devices possibly being things other than keyboards. It seems to make onKeyListener pretty much useless though, since it only works on phones with hardware keyboards. I worked around this issue recently by using TextWatcher on the EditText field in my Activity instead of using OnKeyListener.


onKeyListener worked perfectly on Android 1.5 via the soft keyboard

From Android 1.6 onwards the character and number keys are not going via the onKey event, yet the DEL key does

Frustrating


This is probably stupid, but that's how Android works at the moment.

The documentation states that the key events will only be propagated for the hardware key strokes, not software.

The device manufacturers are actually being discouraged to propagate soft keyboard events through key listeners, although it is completely up to the manufacturer to honour that or to actually treat the soft and hard keyboards with equal terms.

Starting from Android 4.2.2, Android system itself will not support key stoke events for the soft keyboards at all, so even the manufacturers will not be able to choose their way.

So the only foolproof option here is to implement your own IME (soft keyboard), and handle the keystrokes yourself.

TextWatcher can be used mostly to replace the key listeners, however editText.setText(...); will also trigger the TextWatcher events, so if one is interested in typed keys only then probably TextWatcher is not a solution either.

Please be cautious when using TextWatcher with AutocomleteTextView or EditText. Do not modify text in the AutocompleteTextView / EditText's content from within TextWatcher events, cause otherwise you'll most probably end up in an infinite event/listening loop.

Hope this helps to clarify the available options, but sadly it does not provide a working solution.

Disappointing that Google has missed on this important aspect of their UI.