Search code examples
javakeyboardkeylwjgltapandhold

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).

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

Solution

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