对KeyListeners的困惑

时间:2014-11-16 23:44:38

标签: java swing keylistener

我试图获取键盘输入并根据按下的按钮设置布尔值。我在我的一个播放器类中创建了这个类的实例以更新位置,这是我的代码:

public class Keyboard extends KeyAdapter implements KeyListener
{
boolean downClick, upClick, leftClick, rightClick = false;

public Keyboard()
{
    Game.getGame().getFrame().addKeyListener(new KeyAdapter())
}

public void keyPressed(KeyEvent e)
{
    if (e.getKeyCode() == KeyEvent.VK_DOWN) downClick = true;
    else if (e.getKeyCode() == KeyEvent.VK_UP) upClick = true;
    else if (e.getKeyCode() == KeyEvent.VK_LEFT) leftClick = true;
    else if (e.getKeyCode() == KeyEvent.VK_RIGHT) rightClick = true;
}

public void keyReleased(KeyEvent e)
{
    if (e.getKeyCode() == KeyEvent.VK_DOWN) downClick = false;
    else if (e.getKeyCode() == KeyEvent.VK_UP) upClick = false;
    else if (e.getKeyCode() == KeyEvent.VK_LEFT) leftClick = false;
    else if (e.getKeyCode() == KeyEvent.VK_RIGHT) rightClick = false;
}

public boolean isDownClick() 
{
    return downClick;
}

public boolean isUpClick() 
{
    return upClick;
}

public boolean isLeftClick() 
{
    return leftClick;
}

public boolean isRightClick() 
{
    return rightClick;
}

}

我非常积极Game.getGame().getFrame().addKeyListener(new KeyAdapter())不正确。只是澄清Game.getGame().getFrame()只是让我的JFrame。 addKeyListener想要什么作为参数?这是我的代码中唯一的问题,因为输入没有移动我的屏幕组件。组件代码很有效,工作正常。这一课中没有任何错误。

2 个答案:

答案 0 :(得分:3)

不要将KeyListener用于此类工作,使用键绑定API,它可以更好地控制生成关键事件所需的焦点级别。有关详细信息,请参阅How to Use Key Bindings

问题是多方面的......

首先,如果该组件具有焦点且具有焦点,KeyListener将仅在其注册的组件上引发事件。

其次,在用户和实际框架之间,有许多层......

enter image description here

......其中任何一个都可能从框架中窃取焦点并阻止事件到达它......

答案 1 :(得分:1)

您正在向框架添加空KeyAdapter。请尝试使用Game.getGame().getFrame().addKeyListener(this)。这样,实际上将调用您的方法,而不是随机KeyAdapter的方法。

这是更新后的代码:

public class Keyboard implements KeyListener { // A KeyAdapter already implements a key listener. You don't need both...
    boolean downClick = false;
    boolean upClick = false;
    boolean leftClick = false;
    boolean rightClick = false;
    // Unlike other languages, Java makes you define each variable by itself. Even though the previous would have worked, (by default Java Booleans are false), this is a nicer way to put it and follows conventions for what you're wanting to do.

    public Keyboard() {
        Game.getGame().getFrame().addKeyListener(this);
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_DOWN) downClick = true;
        else if (e.getKeyCode() == KeyEvent.VK_UP) upClick = true;
        else if (e.getKeyCode() == KeyEvent.VK_LEFT) leftClick = true;
        else if (e.getKeyCode() == KeyEvent.VK_RIGHT) rightClick = true;
    }

    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_DOWN) downClick = false;
        else if (e.getKeyCode() == KeyEvent.VK_UP) upClick = false;
        else if (e.getKeyCode() == KeyEvent.VK_LEFT) leftClick = false;
        else if (e.getKeyCode() == KeyEvent.VK_RIGHT) rightClick = false;
    }

    public boolean isDownClick() {
        return downClick;
    }

    public boolean isUpClick() {
        return upClick;
    }

    public boolean isLeftClick() {
        return leftClick;
    }

    public boolean isRightClick() {
        return rightClick;
    }
}