【问题标题】:LWJGL Key Event State not workingLWJGL 键事件状态不起作用
【发布时间】:2012-12-21 22:08:47
【问题描述】:

LWJGL 似乎在骚扰我(或者我又是个笨蛋)。我正在使用以下代码:

if(Keyboard.getEventKey() == Keyboard.KEY_Q) {
    if (Keyboard.getEventKeyState()) {
        System.err.println("Pressed");
    }else {
        System.err.println("Released");
    }
}

当我按下并释放“Q”时,它既不会打印出“Pressed”也不会打印出“Released”。帮忙?

编辑:问题已解决。在使用 getEventKey() 之前,您首先必须轮询键盘缓冲区。固定代码为:

while(Keyboard.next()) {
        if(Keyboard.getEventKey() == Keyboard.KEY_Q) {
            System.out.println("HI");
            if (Keyboard.getEventKeyState()) {
                System.out.println("Pressed");
            }else {
                System.out.println("Released");
            }
        }
    }

【问题讨论】:

  • 你不应该使用System.out.println()吗?
  • 这并不重要。它只是将文本打印为红色,因此我可以在我拥有的所有其他打印输出之间看到它。就算改了也没关系

标签: java keyboard lwjgl


【解决方案1】:

每个 API:

public static int getEventKey()

请注意,返回的键码对当前键盘布局无效。要获取实际按下的字符,请调用 getEventCharacter

返回:当前事件的键

尝试将您的第一个 if 语句更改为使用 getEventCharacter 方法:

   if(Keyboard.getEventCharacter() == Keyboard.KEY_Q) {

【讨论】:

    【解决方案2】:

    这就是我在我的程序中所做的...我为角色创建一个布尔值,然后在游戏循环中运行一个方法来检查它:

    public static boolean keyboardA;
    
    public static void refresh() {
    keyboardA = Keyboard.isKeyDown(Keyboard.KEY_A);
    }
    
    public static void game_loop() {
    refresh();
    }
    

    如果您需要我解释更多,请评论:P 希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 2014-03-06
      • 1970-01-01
      • 2011-11-29
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多