Unpossible
Unpossible

Reputation: 10697

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

Upvotes: 8

Views: 13718

Answers (3)

Kirra
Kirra

Reputation: 9

onKeyListener only works with:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Set it in the onCreate method.

Upvotes: -1

Chris Johnson
Chris Johnson

Reputation: 131

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!

Upvotes: 6

raukodraug
raukodraug

Reputation: 11629

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

Upvotes: 15

Related Questions