Android onKeyDown not catching Dpad center and enter key presses
Asked Answered
R

3

6

Doing a simple override in my base activity of onKeyDown, I'm able to capture all key presses except those of the enter and dpad center buttons (as determined via breakpoint). I've no clue as to why - can anyone shed some light on the situation?

EDIT: Quick update - it does capture Dpad center and enter key LONG presses, but still not normal presses.

Rubble answered 20/7, 2012 at 0:1 Comment(4)
Post some code of how you are capturing them.Nodical
In my main activity - '@Override public boolean onKeyDown(int keyCode, KeyEvent event) { return false; }'Rubble
Are you not doing anything else when the key is pressed? Something to indicate that you pressed it?Nodical
I've added Toast messages as well as break points to verify if events are caught - all key presses except the aforementioned ones are.Rubble
D
15

I know this question is already pretty old but in case some desperate coder got lost I post my answer.

I had a similar problem with my USB keyboard. When anything else except a EditText box was focussed the ENTER key was never caught by onKeyUp or onKeyDown.

if you use dispatchKeyEvent() you get the KeyEvent before it reaches the window and in my case I definitely get the ENTER key. But cautious, the event is called twice, once for key down and once for key up.

here a sample code:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    System.out.println(event.getAction() + " " + event.getKeyCode() + " - " + (char) event.getUnicodeChar());

    return true;
}
Damoiselle answered 26/7, 2015 at 8:27 Comment(2)
moreover, on android 2.3 you don't need this, but on 4.1 need. I'd faced with unability to catch ENTER in OnKeyDown from USB, it always "press" to focused button. Only dispatch solve itTapestry
I had problems in the android studio emulator for Android TV. OnKeyDown only got called for long clicks on the dpad. dispatchKeyEvent worked. THANK YOU for your answer!Fields
N
1

Did you read the documentation?

Key presses in software keyboards will generally NOT trigger this listener, although some may elect to do so in some situations. Do not rely on this to catch software key presses.

Also, your way of capturing keys is very vague. You are not even checking the keyCode sent to you by using:

@Override public boolean onKeyDown(int keyCode, KeyEvent event) { return false; }

You can handle onKey from a View:

public boolean onKey(View v, int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
            /* This is a sample for handling the Enter button */
            return true;
    }
    return false;
}

Remember to implement OnKeyListener and to set your listener

viewname.setOnKeyListener(this);
Nodical answered 20/7, 2012 at 17:22 Comment(0)
V
-1

it appears that DPAD keys are acting for the focused items as have been told here:

https://groups.google.com/forum/#!topic/android-developers/HsILBlpsK7I

Although I haven't tried it myself, maybe you can set focus to your view object and attach a key listener function on it.

Update: My colleague had the same problem and this suggestion worked for her. :) She was able to catch the key code from DPAD when the list view is focused.

Veats answered 6/12, 2013 at 9:12 Comment(1)
Update: My colleague had the same problem and this suggestion worked for her. :) She was able to catch the key code from DPAD when the list view is focused.Veats

© 2022 - 2024 — McMap. All rights reserved.