KeyListener 无法识别何时按下某个键

KeyListener does not recognize when a key is pressed

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Tastatur implements KeyListener {
    
    private boolean[] keys = new boolean[66568];
    private boolean left, right, up, down, fw, bw;
    
    public void update() {  //Den boolean-Variablen werden keys zugewiesen
        this.left = this.keys[KeyEvent.VK_LEFT] || this.keys[KeyEvent.VK_A];
        this.right = this.keys[KeyEvent.VK_RIGHT] || this.keys[KeyEvent.VK_D];
        this.fw = this.keys[KeyEvent.VK_UP] || this.keys[KeyEvent.VK_W];
        this.bw = this.keys[KeyEvent.VK_DOWN] || this.keys[KeyEvent.VK_S];
        this.up = this.keys[KeyEvent.VK_SPACE];
        this.down = this.keys[KeyEvent.VK_C] || this.keys[KeyEvent.VK_CONTROL];
    }
    
    public boolean getLeft() {
        return this.left;
    }
    
    public boolean getRight() {
        return this.right;
    }
    
    public boolean getFW() {
        return this.fw;
    }
    
    public boolean getBW() {
        return this.bw;
    }
    
    public boolean getUp() {
        return this.up;
    }
    
    public boolean getDown() {
        return this.down;
    }
    
    @Override
    public void keyPressed(KeyEvent arg0) {
        keys[arg0.getKeyCode()] = true;
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        keys[arg0.getKeyCode()] = false;
        
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    
}

所以,我的问题是,如果我使用例如getLeft() 在某处它总是 returns false,即使按键被按下。我使用它的方法完全没问题(如果我手动将 getleft 设置为 true,其他方法就可以工作)并且我追踪到这个 class 的错误。看起来,它根本无法识别任何键盘输入。所以错误一定是在 KeyPressed、KeyReleased 或 update() 的某处,对吧?如您所见,我不是那么有经验,所以我非常感谢您的帮助。请帮助我找出我的 KeyListener 不起作用的原因。

提前致谢!

需要添加 GUI 并将侦听器绑定到某些 GUI 组件(TextArea in)。侦听器应包含逻辑(如果按下某个键,该怎么做)。波纹管是一个最小的例子。

import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class TestKey extends JFrame implements KeyListener{

    private static final long serialVersionUID = 1L;
    JTextArea out;
    public static void main(String[] args)
    {
        new TestKey();
    }
    public TestKey()
    {
        this.setSize(400, 200);
        setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JTextArea in = new JTextArea(3,30);
        out = new JTextArea(3,30);
        add(in);
        add(out);
        in.addKeyListener(this);
        setVisible(true);   
    }
    @Override
    public void keyTyped(KeyEvent e){
    
    }
    @Override
    public void keyPressed(KeyEvent e) {
        out.setText(Character.toString(e.getKeyChar()));
        if(e.getKeyCode()==37)
        {
            out.setText("left");
        }
        else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
        {
            out.setText("right");
        }
        System.out.println(e);
        
    }
    @Override
    public void keyReleased(KeyEvent e) {   
    }

}

输出