有没有办法在使用键绑定时强制键释放?

时间:2018-01-22 20:39:41

标签: java key

有没有办法在没有实际释放密钥的情况下释放密钥。我知道这听起来很奇怪,但我试图强迫我的钥匙总是回到释放状态,无论如何。当我的游戏在玩家输掉后立即循环时,即使在新游戏中也保持按下的箭头键持续按下。当玩新游戏时,需要按下并释放按键以停用按键。任何想法?

 this.getInputMap(IFW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "Pright");
        this.getInputMap(IFW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "Pleft");
        this.getInputMap(IFW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Pup");
        this.getInputMap(IFW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Pdown");

        this.getInputMap(IFW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0,true), "Rright");
        this.getInputMap(IFW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0,true), "Rleft");
        this.getInputMap(IFW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0,true), "Rup");
        this.getInputMap(IFW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0,true), "Rdown");

        this.getActionMap().put("Pright", new ArrowAction("Pright"));
        this.getActionMap().put("Pleft", new ArrowAction("Pleft"));
        this.getActionMap().put("Pup", new ArrowAction("Pup"));
        this.getActionMap().put("Pdown", new ArrowAction("Pdown"));

        this.getActionMap().put("Rright", new ArrowAction("Rright"));
        this.getActionMap().put("Rleft", new ArrowAction("Rleft"));
        this.getActionMap().put("Rup", new ArrowAction("Rup"));
        this.getActionMap().put("Rdown", new ArrowAction("Rdown"));

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是使用键侦听器而不是使用操作和键绑定。然后在键监听器内部放置一个已释放的布尔值,该布尔值将检查是否在按下该键后再次按下该键以停止该事件。这也意味着您可以将其设置为已释放,因为您只需将布尔值设置为true即可。

相关问题