只有在持有密钥时,才能使Java中的KeyEvent发生一次

时间:2014-05-13 22:45:43

标签: java awt keylistener keyevent

我需要知道是否有可能只在按下某个键时发生一次动作,即使该按键被按下一段时间,如果可能,怎么办? 这是我现在的代码:

if(e.getKeyCode() == KeyEvent.VK_A)
{
   attack = true;
   moveX = -5;
   draw(moveX, moveY);
   players.get(username).setImageIcon("attack-left"); 
}

这是在keyPressed方法中,并且在keyReleased中我将moveX设置为0.因此,如果按下A,则图像应该向左5个单位并停止,无论A是否仍被按下或它已被释放。但这不起作用,它只是继续向左移动。我尝试使用keyTyped,但它没有用。

提前致谢,如果需要,我可以提供更多代码。

2 个答案:

答案 0 :(得分:3)

执行此操作的一种方法是将最后一个键保存到字段中。然后,如果按下的当前键与按下的最后一个键相同,则不要执行任何操作。在键上,清除该字段。

以下是一些示例代码:

package com.sandbox;

import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

public class SwingSandbox {

    private static Character lastKey = null;

    public static void main(String[] args) throws IOException {
        JFrame frame = buildFrame();
        frame.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (lastKey == null || lastKey != e.getKeyChar()) {
                    lastKey = e.getKeyChar();
                    System.out.println("keyTyped: " + lastKey);
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                if (lastKey == null || lastKey != e.getKeyChar()) {
                    lastKey = e.getKeyChar();
                    System.out.println("keyPressed: " + lastKey);
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                lastKey = null;
                System.out.println("keyReleased");
            }
        });

    }

    private static JFrame buildFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
        return frame;
    }


}

尽管@ schmop的评论说,我没有体验到这一点。使用此代码:

package com.sandbox;

import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

public class SwingSandbox {

    public static void main(String[] args) throws IOException {
        JFrame frame = buildFrame();
        frame.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                System.out.println("keyTyped");
            }

            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println("keyPressed");
            }

            @Override
            public void keyReleased(KeyEvent e) {
                System.out.println("keyReleased");
            }
        });

    }

    private static JFrame buildFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
        return frame;
    }


}

按下一个键可以打印出来:

  

keyPressed keyTyped keyPressed keyTyped keyPressed keyTyped keyPressed   keyTyped keyPressed keyTyped keyPressed keyTyped keyPressed keyTyped   keyPressed keyTyped keyPressed keyTyped keyPressed keyTyped keyPressed   keyTyped keyPressed keyTyped keyPressed keyTyped keyPressed keyTyped   keyPressed keyTyped keyPressed keyTyped keyPressed keyTyped keyPressed   keyTyped keyPressed keyTyped keyPressed keyTyped

答案 1 :(得分:0)

解决问题的一种方法是在释放密钥时停止moveX移动。

public void keyReleased(KeyEvent e) {
        moveX = 0;
        moveY = 0; //If you have a moveY
    }