开发者

Java 2 ME GameCanvas -- How to receive all keyPressed events?

Hello

I'm writing simple j2me calculator. using GameCanvas class as a basis of my interface. I need to handle both numpad key events and game-relaited events (such as UP, DOWN, FIRE, etc) for moving my cursor. The problem is that neither emulator, nor my Sony Ericsson doesn't receives some numpad and game-relaited events. For example, I get SOFT1, SEND, 2, 4, ASTERISK key pressed events, but don't receive 1, 3, LEFT, RIGH, UP, DOWN, FIRE events.

class CalculatorCanvas extends GameCanvas
{
    public CalculatorCanvas() throws java.io.IOException
    {
        super(true);
        setFullScreenMode(true);

        cursor = new Sprite(Image.createImage("/cursor.png"));
    }

    public void paint(Graphics g)
    {
        g.setColor(0);
        g.fillRect(0, 0, getWidth(), getHeight());
        cursor.paint(g);
    }

    protected void keyPressed(int keyCode)
    {
        System.out.println("keyPressed: " + get开发者_JAVA技巧KeyName(key));
        if (keyCode == getKeyCode(RIGHT))
            cursor.move(STEP, 0);
        if (key == getKeyCode(LEFT))
            cursor.move(-STEP, 0);
            /*...*/
        repaint();
    }

    private final int STEP = 3;
    private Sprite cursor;
}  

How can I receive all this events?

Thanks in advance

PS I've found that Canvas (base class for GameCanvas) reseives all this events, but I have to use GameCanvas


check javadoc for GameCanvas constructor. You should call it with "false" !

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜