Android - Get keyboard key press
Asked Answered
C

4

33

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 ?

Calculation answered 30/6, 2012 at 10:6 Comment(2)
And when you examined the source code to classes like EditText and TextView, to see how they handle it, what did you learn?Chrysanthemum
Study the source code can help but take too time.Calculation
S
43

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).

Skinhead answered 30/6, 2012 at 18:52 Comment(7)
However there are problem with the character selected by the popup that appear on long press. For example if i long press the 'a' and i select 'à' both getKeyCode and getUnicodeChar return 0Calculation
Have a look at getDeadChar() method in KeyEvent classSkinhead
Ok but this produce the character with the gived accent. The problem is that i can't know what key is press because of the 0 return in functions getKeyCode and getUnicodeChar.Calculation
Unfortunately no. Because of the problem with return 0 posted above. It just print 0 like my function do when i press à or è and other. However i found the solution casting the int returned by getUnicodeChar()Calculation
@Skinhead Dispatch Veent is not invoking for Keboard View Keys, It is invoking only when Back key is pressed/ Hardware keys Pressed.Clippard
@Roster, yes, that is correct. So how do we solve it?Brume
@Nulik, see my answer belowDuley
C
18

With the hint of vasart i can get the KeyPress event. To make the keycode printable i have used the function getUnicodeChar passing it the meta button state then just a char cast solve the problem.

This is the working code:

@Override
public boolean dispatchKeyEvent(KeyEvent KEvent) 
{
    int keyaction = KEvent.getAction();

    if(keyaction == KeyEvent.ACTION_DOWN)
    {
        int keycode = KEvent.getKeyCode();
        int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState() );
        char character = (char) keyunicode;

        System.out.println("DEBUG MESSAGE KEY=" + character + " KEYCODE=" +  keycode);
    }


    return super.dispatchKeyEvent(KEvent);
}

Of course this work only with ASCII character.

Calculation answered 5/7, 2012 at 16:50 Comment(1)
In my Case , Custom Keyboard view is not responding to dispatchkeyEvent .. I have implemeted keyboardView and i would like to detect combination of keys like ctrl+c , Ctrl+v like that. How can i acheive thatClippard
D
4

There are no option to handling key press events on soft keyboard (an on-screen keyboard) only from a hardware keyboard.

for more details: Handling Keyboard Actions

Note: When handling keyboard events with the KeyEvent class and related APIs, you should expect that such keyboard events come only from a hardware keyboard. You should never rely on receiving key events for any key on a soft input method (an on-screen keyboard).

Duley answered 20/2, 2017 at 14:0 Comment(0)
M
2

When Keyboard is opened in activity your activity actually becomes foreground... All TextArea or TextFields have their own mechanism to get keypressed from onScreen keyboard... if you want to use onKeyDown() listner for virtual keyboard make sure that you set in AndroidManifest File under your activity android:windowSoftInputMode="stateAlwaysVisible" then onkeyDown() will work it did worked for me ...

Messidor answered 4/11, 2015 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.