Android: KeyListener for EditText does not receive keys

I have an EditText that I want to track KeyEvents, and I have a listener configured 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 an EditText is entered into the virtual keyboard, the only keystroke that starts logging is the back key. I checked that all other keystrokes do not even start onKey() . I'm sure this is something simple, but nothing was found on SO, which seems to be able to handle this.

Thanks,

Floor

+7
source share
3 answers

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

+15
source

From Android Help:

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

View.OnKeyListener

Class Overview Defining an interface for a callback that is called when a hardware key event is sent to this view. The callback will be called before the key event is passed to the view. This is only useful for hardware keyboards; The software input method is not required to run this listener.

It seems that OnKeyListener is designed specifically to respond only to the HARDWARE keys!

+5
source

onKeyListener only works with:

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

Set it in the onCreate method.

0
source

All Articles