我无法让 KeyEvent 侦听器工作

发布于 2024-09-26 04:29:13 字数 1328 浏览 0 评论 0原文

好的,首先。 如果我这样做 System.out.println(e); 当我按下该键时,它会打印出来。然而,我一生都无法弄清楚如何将其存储为 int 。如果我这样做的话,我的 IDE 不会给我任何错误 int 按下 = e.KEY_PRESSED();或 int Pressed = e.getKeyCode(); 但如果我尝试打印按下什么也不会发生。

我已经尝试让它工作几个小时了,谷歌搜索 KeyEvent 处理程序和 Javadocs 似乎对我没有什么帮助。

public void keyPressed(KeyEvent e) {
    pressed = e.getKeyCode();
    System.out.println(pressed);

}

    do{
        time = System.currentTimeMillis();
        do{
            if(pressed == 37||pressed==38||pressed==39||pressed==40){
                lastvalid=pressed;
            }
        }
        while(System.currentTimeMillis() < time + speed);

        switch(lastvalid){
            case 37: catarloc.set(0, (Integer)catarloc.get(0)-1); break;
            case 38: catarloc.set(1, (Integer)catarloc.get(1)-1); break;
            case 39: catarloc.set(0, (Integer)catarloc.get(0)+1); break;
            case 40: catarloc.set(1, (Integer)catarloc.get(1)+1); break;
        }

        if(Math.random() > .95 || apples < 1){
            applearray[(int)(Math.random()*100/2.8)][(int)(Math.random()*100/4)] = true;
            apples++;
        }
        score+=catarloc.size()-1;
        label.setText("Score     "+ score);
        mainWindow.repaint();
    }
    while(win == false || lose == false);

Ok, first off.
If I do System.out.println(e);
that does print when I push the key. However I can not for the life of me figure out how to store this into an int. My IDE gives me no errors if I do
int pressed = e.KEY_PRESSED(); or int pressed = e.getKeyCode();
but if I try to print pressed nothing happens.

I've been trying to get this to work for hours and Googling KeyEvent handlers and the Javadocs seem to be of little help to me on this.

public void keyPressed(KeyEvent e) {
    pressed = e.getKeyCode();
    System.out.println(pressed);

}

    do{
        time = System.currentTimeMillis();
        do{
            if(pressed == 37||pressed==38||pressed==39||pressed==40){
                lastvalid=pressed;
            }
        }
        while(System.currentTimeMillis() < time + speed);

        switch(lastvalid){
            case 37: catarloc.set(0, (Integer)catarloc.get(0)-1); break;
            case 38: catarloc.set(1, (Integer)catarloc.get(1)-1); break;
            case 39: catarloc.set(0, (Integer)catarloc.get(0)+1); break;
            case 40: catarloc.set(1, (Integer)catarloc.get(1)+1); break;
        }

        if(Math.random() > .95 || apples < 1){
            applearray[(int)(Math.random()*100/2.8)][(int)(Math.random()*100/4)] = true;
            apples++;
        }
        score+=catarloc.size()-1;
        label.setText("Score     "+ score);
        mainWindow.repaint();
    }
    while(win == false || lose == false);

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

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

发布评论

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

评论(2

三月梨花 2024-10-03 04:29:13

< 怎么样?代码>keyEvent.getKeyCode()?它返回一个 int。 KEY_PRESSED 是 KeyEvent 上的 static Final int 成员,表示按下按键时的事件,而不是实际的按键代码。

更新:(哎呀)我看到您已经尝试过 getKeyCode。如果打印 getKeyLocationgetKeyChar

What about keyEvent.getKeyCode()? It returns an int. KEY_PRESSED is a static final int member on KeyEvent and represents an event when a key is pressed down, not the actual key code.

Update: (Whoops) I see that you already tried getKeyCode. What happens if you print getKeyLocation or getKeyChar?

霊感 2024-10-03 04:29:13

根据事件类型:

  • keyEvent.getKeyChar() 为您提供与按键关联的字符,但仅适用于 KEY_TYPED 事件。否则它会返回 CHAR_UNDEFINED
  • keyEvent.getKeyCode() 为您提供虚拟按键代码,但仅适用于 KEY_PRESSEDKEY_RELEASED事件。否则返回VK_UNDEFINED

Depending on the type of event:

  • keyEvent.getKeyChar() gives you the character associated with the key, but only for KEY_TYPED events. Otherwise it returns CHAR_UNDEFINED
  • keyEvent.getKeyCode() gives you the virtual key code, but only for KEY_PRESSED and KEY_RELEASED events. Otherwise it returns VK_UNDEFINED.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文