简体   繁体   English

按住LWJGL中的一个键

[英]Pressing and Holding a key in LWJGL

I am using LWJGL and I want to cause an event to rapidly take place when I press and hold a key (like holding a letter key in word). 我正在使用LWJGL,因此我想在按住某个键(例如在单词中按住字母键)时迅速发生一个事件。

This is my attempt: 这是我的尝试:

while(Keyboard.next())
{
    if (Keyboard.getEventKeyState())
    {
        if (Keyboard.isKeyDown(Keyboard.KEY_UP)) 
        {
            i += 5.0f;
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) 
        {
            i -= 1.0f;
        }
    }
}

if (Keyboard.getEventKeyState()) only runs when a key is pressed. if (Keyboard.getEventKeyState())仅在按下某个键时运行。

For holding down, use a boolean/integer to record whether your game loop should increase or decrease i . 要按住,请使用布尔值/整数来记录您的游戏循环是增加还是减少i

I answered my own question by using a robot and a thread when pressed: 当按下时,我通过使用机器人和线程回答了自己的问题:

        if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
        {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_DOWN);
            zpos -= 0.1f;
            Thread.sleep(100);
            robot.keyRelease(KeyEvent.VK_DOWN);
        }

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

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