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.
- 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?Christopher Orr– Christopher Orr2009-12-30 02:11:20 +00:00Commented Dec 30, 2009 at 2:11
- 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)); }); }user238974– user2389742010-01-01 00:29:24 +00:00Commented Jan 1, 2010 at 0:29
- I am just reading the keys for now.user238974– user2389742010-01-01 00:32:15 +00:00Commented Jan 1, 2010 at 0:32
- Your code works on the emulator keyboard and not on the soft keyboard. Any clue for the soft keyboard?Muhammad Maqsoodur Rehman– Muhammad Maqsoodur Rehman2010-04-28 06:44:34 +00:00Commented Apr 28, 2010 at 6:44
6 Answers
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.
3 Comments
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.
Comments
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.
Comments
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); } }