onKeyListener not working with soft keyboard (Android)
Asked Answered
L

6

20

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.

Lorentz answered 28/12, 2009 at 2:30 Comment(4)
It sounds odd if you're getting some characters but not others from the soft keyboard. Can you post the code of your listener, and where you're attaching it?Dissogeny
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login_screen); emailTxt = (EditText) findViewById(R.id.email); emailTxt.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { Log.i("Key Value", String.valueOf(keyCode)); }); }Lorentz
I am just reading the keys for now.Lorentz
Your code works on the emulator keyboard and not on the soft keyboard. Any clue for the soft keyboard?Carnahan
M
13

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.

Menfolk answered 30/12, 2009 at 19:49 Comment(3)
I tried using TextWatcher but I need the KeyUp and KeyDown events for each key pressed. Textwatcher just gives me the keydown event.Lorentz
I don't think it's possible to listen to KeyUp and KeyDown from the software keyboard.Menfolk
OnKeyListener works for every symbol on some devices. On others some buttons work: BackSpace, Enter, etc.Marijane
A
9

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

Amain answered 25/10, 2010 at 13:13 Comment(0)
A
7

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.

Arduous answered 17/4, 2013 at 4:15 Comment(0)
A
3

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)

Alby answered 1/12, 2011 at 18:18 Comment(0)
O
1

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.

Oligocene answered 28/2, 2013 at 15:2 Comment(0)
R
-1
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);
    }
} 
Rimose answered 22/2, 2015 at 21:41 Comment(2)
Can you please give us an example of how one can use this code? I tried it now and it doesn't seem to work.Stoughton
public class CanvasView extends View{ public CanvasView(Context c){ super(c); //enable keyboard setOnKeyListener(new KeyBoard()); setFocusable(true); setFocusableInTouchMode(true); }} //enable keyboard to canvasViewRimose

© 2022 - 2024 — McMap. All rights reserved.