Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: KeyListener for an EditText not receiving keys

I have an EditText that I want to monitor KeyEvents for, and I have a listener set up as follows:

mText = (EditText) this.findViewById(R.id.title);
mText.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        final int view = v.getId();
        switch (view) {
            case R.id.title:
                Log.d(LOG_TAG, "key handled");
                break;
        }
        return false;
    }
});

My problem is that when the EditText is being typed into using the virtual keyboard, the only key press that triggers the logging is the backspace key. I've verified that all other keypresses aren't even triggering onKey(). I'm sure this is something simple, but didn't find anything on SO that seemed to deal with this.

Thanks,

Paul

like image 436
Unpossible Avatar asked Apr 05 '11 14:04

Unpossible


2 Answers

Try using addTextChangedListener(TextWatcher watcher) defined here with it you can handle the physical and the soft keyboard. I hope it helps

like image 187
raukodraug Avatar answered Oct 27 '22 22:10

raukodraug


From the Android reference at:

http://developer.android.com/reference/android/view/View.OnKeyListener.html

View.OnKeyListener

Class Overview Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.

It seems OnKeyListener is designed expressly to react to HARDWARE keys only!

like image 23
Chris Johnson Avatar answered Oct 27 '22 23:10

Chris Johnson