19

I've created a simple android game, based on the Lunar Lander sample, and I'm having a problem with handling key events. When the activity starts, the only keys that onKeyDown or onKeyUp get called for are the dpad up/down/left/right keys. Neither the menu, back, or dpad_center keys trigger onKey methods. However, once I've pushed one of the dpad up/down/left/right buttons, pressing the menu, back, or dpad_center keys do trigger these methods. I'm not getting any errors, or any indication of what's going wrong.

It's possible that the focus is set wrong - the activity is started from a button on screen, so it could be in touchscreen mode. If that's the case, shouldn't touching the back button get me in to the right focus mode so that I can catch the event?

I'm using the emulator from SDK-1.5r3. I have not been able to try this on a real phone yet. Here's my onKeyDown.

public boolean onKeyDown(int keyCode, KeyEvent msg) { Log.d(TAG, "onKeyDown: " + keyCode); return super.onKeyDown(keyCode, msg); } 

Thanks

Matt

3 Answers 3

37

Is this onKeyDown in a view or in the activity?

If setContentView is called passing in a view, and that view has setFocusable(true) called on it, all key events will bypass the activity and go straight into the view.

On the other hand, if your onKeyDown is in the view, and you haven't called setContentView on the Activity and setFocusable(true) on the view, then your Activity will get the key events and not the View.

Look for those specific calls but I think you're right about it being a focus issue.

Sign up to request clarification or add additional context in comments.

6 Comments

It was in the view - but while checking this out, I realized I didn't have setFocusableInTouchMode enabled - adding that in fixed the problem.
Ah, cool. I'm sorry I missed the actual issue but I'm glad I could help.
I tried this in a fragment but the onKeyDown() of my view never gets called even though i have set it's focusability to true : searchView.setFocusable(true); searchView.setFocusableInTouchMode(true);. The onKeyDown() of the Activity does get called though, but not for the back key unfortunately.
i think the back button gets handled extra in the method 'onBackPressed'
onKeyDown might not be called in response to KeyEvent.KEYCODE_MENUis if you have a toolbar -- the base class implementation of dispatchKeyEvent may decide that the toolbar handled the event, since ToolbarActionBar.onMenuKeyEvent always returns true.
|
23

Activity's onKeyDown/onKeyUp methods not always called. Unlike them dispatchKeyEvent on Activity fired always. Move keydown/keyup logic here. Works well.

@Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { // keydown logic return true; } return false; } 

3 Comments

Instead of returning false, call super and return the result at the end.
Doesn't work for Samsung SM-J120F running Android 5.1.1.
This is the solution! I had a problem with onKeyDown and onKeyUp overrides failing to intercept SPACE and ENTER keypresses, and this solution solved the problem for me by intercepting all keypresses!
0

insert this code

getWindow().getDecorView().setFocusable(true); if (SDK_INT >= Build.VERSION_CODES.O) { getWindow().getDecorView().setFocusedByDefault(true); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.