简体   繁体   English

LWJGL关键事件状态不起作用

[英]LWJGL Key Event State not working

LWJGL seems to be bugging out on me (or I am being a dunce again). LWJGL似乎在困扰我(或者我再次成为笨蛋)。 I am using the following code: 我正在使用以下代码:

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

When I press and release 'Q', it prints out neither "Pressed" nor "Released". 当我按下并释放“ Q”时,它既不会打印出“ Pressed”,也不会打印出“ Released”。 Help? 救命?

EDIT: Problem solved. 编辑:问题解决了。 You first must poll the Keyboard buffer before using getEventKey(). 在使用getEventKey()之前,必须首先轮询键盘缓冲区。 Fixed code is: 固定代码为:

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");
            }
        }
    }

Per API: 每个API:

public static int getEventKey() 公共静态int getEventKey()

Please note that the key code returned is NOT valid against the current keyboard layout . 请注意,返回的键码对当前的键盘布局无效 To get the actual character pressed call getEventCharacter 要获取实际按下的字符,请调用getEventCharacter

Returns: The key from the current event 返回:当前事件的键

Try changing your first if statement to use getEventCharacter menthod as: 尝试更改您的第一个if语句以使用getEventCharacter方法:

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

This is what I do in my programs... I make a boolean for the character and then run a method in the game loop to check it: 这就是我在程序中所做的...我为角色创建了一个布尔值,然后在游戏循环中运行一个方法来检查它:

public static boolean keyboardA;

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

public static void game_loop() {
refresh();
}

If you need me to explain more just comment :P Hope it helps 如果您需要我解释更多内容,请评论:P希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM