如何使用 netbeans J2ME 为不同的移动键(例如数字键和箭头键)编写代码?

发布于 2024-10-07 08:19:18 字数 33 浏览 5 评论 0原文

你好 我想知道用户在手机键盘上按了哪个键......

hi
i want to know which key user pressed in mobile's keypad.....

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

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

发布评论

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

评论(2

回忆追雨的时光 2024-10-14 08:19:18

这是示例代码:

package hello;

public class KeyDemoCanvas extends Canvas {
    String eventType = "- Press any!";
    int keyCode;

    public void keyPressed(int keyCode) {
        eventType = "pressed";
        this.keyCode = keyCode;
        repaint();
    }

    public void keyReleased(int keyCode) {
        eventType = "released";
        this.keyCode = keyCode;
        repaint();
    }

    public void keyRepeated(int keyCode) {
        eventType = "repeated";
        this.keyCode = keyCode;
        repaint();
    }

    protected void paint(Graphics g) {
        g.setGrayScale(255);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setGrayScale(0);

        int y = 0;
        // y = write (g, y, "Key "+ eventType);
        if (keyCode == 0) {
            return;
        }

        y = write(g, y, "Char/Code: " + ((keyCode < 0) ? "N/A" : ""
                + (char) keyCode) + "/" + keyCode);
        y = write(g, y, "Name: " + getKeyName(keyCode));
        String gameAction;
        switch (getGameAction(keyCode)) {
            case LEFT:
                gameAction = "LEFT";
                break;
            case RIGHT:
                gameAction = "RIGHT";
                break;
            case UP:
                gameAction = "UP";
                break;
            case DOWN:
                gameAction = "DOWN";
                break;
            case FIRE:
                gameAction = "FIRE";
                break;
            case GAME_A:
                gameAction = "GAME_A";
                break;
            case GAME_B:
                gameAction = "GAME_B";
                break;
            case GAME_C:
                gameAction = "GAME_C";
                break;
            case GAME_D:
                gameAction = "GAME_D";
                break;
            default:
                gameAction = "N/A";
        }
        write(g, y, "Action: " + gameAction);
    }

    public int write(Graphics g, int y, String s) {
        g.drawString(s, 0, y, Graphics.LEFT | Graphics.TOP);
        return y + g.getFont().getHeight();
    }
}

here's example code:

package hello;

public class KeyDemoCanvas extends Canvas {
    String eventType = "- Press any!";
    int keyCode;

    public void keyPressed(int keyCode) {
        eventType = "pressed";
        this.keyCode = keyCode;
        repaint();
    }

    public void keyReleased(int keyCode) {
        eventType = "released";
        this.keyCode = keyCode;
        repaint();
    }

    public void keyRepeated(int keyCode) {
        eventType = "repeated";
        this.keyCode = keyCode;
        repaint();
    }

    protected void paint(Graphics g) {
        g.setGrayScale(255);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setGrayScale(0);

        int y = 0;
        // y = write (g, y, "Key "+ eventType);
        if (keyCode == 0) {
            return;
        }

        y = write(g, y, "Char/Code: " + ((keyCode < 0) ? "N/A" : ""
                + (char) keyCode) + "/" + keyCode);
        y = write(g, y, "Name: " + getKeyName(keyCode));
        String gameAction;
        switch (getGameAction(keyCode)) {
            case LEFT:
                gameAction = "LEFT";
                break;
            case RIGHT:
                gameAction = "RIGHT";
                break;
            case UP:
                gameAction = "UP";
                break;
            case DOWN:
                gameAction = "DOWN";
                break;
            case FIRE:
                gameAction = "FIRE";
                break;
            case GAME_A:
                gameAction = "GAME_A";
                break;
            case GAME_B:
                gameAction = "GAME_B";
                break;
            case GAME_C:
                gameAction = "GAME_C";
                break;
            case GAME_D:
                gameAction = "GAME_D";
                break;
            default:
                gameAction = "N/A";
        }
        write(g, y, "Action: " + gameAction);
    }

    public int write(Graphics g, int y, String s) {
        g.drawString(s, 0, y, Graphics.LEFT | Graphics.TOP);
        return y + g.getFont().getHeight();
    }
}
兮颜 2024-10-14 08:19:18

使用 Canvas.getGameAction(keyCode) ,然后与 Canvas.UP、Canvas.DOWN 等进行比较。看看 这个

Use Canvas.getGameAction(keyCode) and then compare with Canvas.UP, Canvas.DOWN etc. Have a look at this.

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