listener – onKeyListener not working with soft keyboard (Android)-ThrowExceptions

Exception or error:

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.

How to solve:

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.

###

This seems to be device specific. I can confirm that this works on the Xoom and the Acer A100. However, the Samsung Galaxy Tab Plus only fires the event for the non-character buttons. (All devices running Honeycomb)

###

I got around this by putting the listener into it’s own method and calling it again after the first time. In the onCreate I call setKeyListenerForEnter();

Then, here’s the method:

public void setKeyListenerForEnter(){

    final EditText search_entry = (EditText) findViewById(R.id.search_entry);
    search_entry.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
                getSearchResults(v);
                setKeyListenerForEnter();
              return true;
            }
            return false;
        }
    });
}

I’m not sure if this is a better solution than handling the IME keyboard itself, but it is a solution.

###

setFocusableInTouchMode(true); //Enable soft keyboard on touch for target view

setFocusable(true); //Enable hard keyboard to target view

example:

public class CanvasView extends View{
    public CanvasView(Context c){
        super(c);

        //enable keyboard
        setOnKeyListener(new KeyBoard());
        setFocusable(true);
        setFocusableInTouchMode(true);
    }
} 

Leave a Reply

Your email address will not be published. Required fields are marked *