tags:

views:

481

answers:

2

I am a new comer to Android development so I imagine I have a simple problem, but I can't seem to figure out what's wrong.

I basically copied the LunarLander game example from the samples, but I tried to to only copy the parts I needed to make a little pong game or something. But for some reason, onKeyUp and onKeyDown are not getting called. Part of the problem is I'm still not exactly sure what the debugging methodology for Android is.

public class PongView extends SurfaceView implements SurfaceHolder.Callback{

...

    /** 
     * Standard override for key up.  Used to move the paddles
     * @return
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent msg) {
            return thread.doKeyUp(keyCode, msg);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg) {
        return thread.doKeyDown(keyCode, msg);
    }

...
}

EDIT SOLUTION:

I forgot to put this line in my View's constructor.

    setFocusable(true);
A: 

Are you using a device with a hardware keyboard or are you using the soft (onscreen) keyboard? I don't think these events will fire with the soft keyboard.

mbaird
I am just running it in the emulator, and they fire fine in the LunarLander example with the same emulator.
FranticPedantic
+1  A: 

You will get key events only if your view is focusable and is focused. In the case of a game, you can set your view to be focusable in touch mode to get the key events pretty much automatically.

Romain Guy