开发者

Android View onKeyUp() not getting called

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);开发者_如何学Go


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜