Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Get keyboard key press

I want to catch the press of any key of the softkeyboard. I don't want a EditView or TextView in my Activity, the event must be handled from a extended View inside my Activity.

I just tried this:

1) Override the onKeyUp(int keyCode, KeyEvent event) Activity method. This don't work with softkeyboard, it just catch few hardkeyboard.

2) Create my OnKeyListener and register that in my View that contains a registered and working OnTouchListener. This doesn't work at all with softkeyboard.

3) Override the onKeyUp(int keyCode, KeyEvent event) View method. This not work at all neither if I set my OnKeyListener nor if I don't set it.

4) With the InputMethodManager object Call the method showSoftInput and passing it my View. This don't work neither for raise up the keyboard, indeed i have to call toggleSoftInput; nor to catch the key events.

I tested all only in the emulator but i think it's enough. Why it's so complicate take a simple key event from a keyboard ?

like image 553
Bemipefe Avatar asked Jun 30 '12 10:06

Bemipefe


People also ask

What is keystroke in Android?

The analysis of typing patterns, formally known as keystroke dynamics is useful to enhance the security of password-based authentication. Moreover, touchscreen allows adding features ranging from pressure of the screen or finger area to the classical time-based features used for keystroke dynamics.

How does Android handle keyboard events?

To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.

What is the symbolic constant for Menu key?

The menu key for Android keyboard is constant value 82 which if you're using the emulator can be triggered using Ctrl + M .


1 Answers

For handling hardware keys and Back key you could use dispatchKeyEvent(KeyEvent event) in your Activity

@Override public boolean dispatchKeyEvent(KeyEvent event) {     Log.i("key pressed", String.valueOf(event.getKeyCode()));     return super.dispatchKeyEvent(event); } 

UPD: unfortunately you can't handle soft keyboard events (see Handle single key events), unless you develop your own custom keyboard (follow the link to learn how Creating input method).

like image 84
vasart Avatar answered Sep 29 '22 22:09

vasart