Reputation: 19
I need to catch a press of a key and return the keycode. the key that was pressed on the software keyboard, I tried to find examples and explanations about that but they are a bit vague.
Does anyone know and can explain to me how to catch a keyboard key press? (I understand onKeyListener is not good for me, and I found the onKeyActionListener but I still didn't understand how to use it, and if it really what I need)
Upvotes: 0
Views: 3054
Reputation: 19189
Here's an answer to a similar question using a workaround.
You can also check out OnEditorActionListener for TextView
's.
A regular KeyListener works most of the time, but here's a quote from the docs:
Key presses on soft input methods are not required to trigger the methods in this listener, and are in fact discouraged to do so. The default android keyboard will not trigger these for any key to any application targetting Jelly Bean or later, and will only deliver it for some key presses to applications targetting Ice Cream Sandwich or earlier.
Upvotes: 1
Reputation: 399
I think the thing you are looking for is
editText.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_ENTER: //or any other key
//do something
return true;
default:
break;
}
}
return false;
}
});
Upvotes: 1