I have a class MyEditText
that extends EditText
.
When receiving a specific keycode from an InputMethodService, say 35 which is equivilant of '#'
, I want to perform an action on
my EditText instead of appending it to the text.
Solutions that did not work:
Setting an
onEditorActionListener
does not help sinceonEditAction()
is called only for a specific set of codes such asKeyEvent.KEYCODE_ENTER
, and I need to detect the event of receiving a code out of that set.Setting an onKeyListener. This also did not work but I don't know the reason and I am not even sure this listener is meant for this purpose. This is the code I tried and it doesn't log anything when typing:
edittext.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { Log.d("EDITOR_TAG", "onKey()"); return false; } });
Edit: after further testing the
onKey()
also gets called forKeyEvent.KEYCODE_ENTER
but not other key codes. Just likeonEditorAction()
.A workaround that might work but I prefer to avoid: implementing
beforeTextChanged()
,onTextChanged()
andafterTextChanged()
of theTextWatcher
interface. The reason I want to avoid this is that it will interfere with a history mechanism implemented in those methods (for undo/redo). I want to consume the event of receiving a key code 35 before it even changes the text.