Java 2 ME GameCanvas -- 如何接收所有 keyPressed 事件?

发布于 2024-11-02 04:20:50 字数 1411 浏览 1 评论 0原文

你好
我正在写一个简单的j2me计算器。使用 GameCanvas 类作为我的界面的基础。我需要处理小键盘按键事件和游戏相关事件(例如 UPDOWNFIRE 等)来移动光标。问题是模拟器和我的索尼爱立信都没有收到一些数字键盘和游戏相关的事件。例如,我得到 SOFT1SEND24ASTERISK 键按下事件,但不接收 13LEFTRIGHUP strong>、DOWNFIRE 事件。

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: " + getKeyName(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;
}  

我怎样才能收到所有这些事件?
预先感谢

PS 我发现 CanvasGameCanvas 的基类)重新接收所有这些事件,但我必须使用 GameCanvas

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: " + getKeyName(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2024-11-09 04:20:50

检查 GameCanvas 构造函数的 javadoc。你应该用“false”来称呼它!

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文