0
\$\begingroup\$

So I'm currently working my way through openGL tutorials, using LWJGL to do all my work in. However as I get to the part on how to access Keyboard and Mouse events in this library, I stumble across this little gem:

public static void poll()

Polls the keyboard for its current state. Access the polled values using the isKeyDown method. By using this method, it is possible to "miss" keyboard keys if you don't poll fast enough... (from http://www.lwjgl.org/javadoc/org/lwjgl/input/Keyboard.html#poll())

So in order to make use of Buffered Input (as the top answer in LWJGL multiple keyboard/mouse input checks calls it) you have to use this poll() method.

This means that no matter what I do, I will miss key presses if I use LWJGL for this. I have made a test with this bit of code:

while(Keyboard.next()) {
    int character = Keyboard.getEventCharacter();
    int key = Keyboard.getEventKey();
    String charName= Keyboard.getKeyName(key);
    System.out.println(charName+""+character);
}

When pressing the buttons Z X C V at the same time, an example output is as follows: Z122 V118 X120 C0 V0 Z0

As you can see the C press and X release aren't found in the buffer. In some cases a single keystroke had both the press and release missing from the buffer.

I need this to not be the case and polling faster doesn't solve the issue at all (why would that even be a solution?). So what are the alternatives for this system?

(P.S. Yes pressing those 4 buttons at the same time was properly recorded in other applications)

\$\endgroup\$
1
\$\begingroup\$

I'd like to help but your code is working fine for me.

public static void main(String[] args) throws Exception {

  DisplayMode displayMode = new DisplayMode(640, 480);
  Display.setDisplayMode(displayMode);
  Display.setTitle("Input test");
  Display.create();

  System.out.println("OpenGL version: " + GL11.glGetString(GL11.GL_VERSION));

  while(!Display.isCloseRequested()) {
    while(Keyboard.next()) {
        int character = Keyboard.getEventCharacter();
        int key = Keyboard.getEventKey();
        String charName= Keyboard.getKeyName(key);
        System.out.println(charName+""+character);
    }
    Display.sync(60);
    Display.update();
  }

  Display.destroy();
}

Are you running at a fair framerate and calling Input.poll()? Input.poll() is included in Display.update() but Display.update() does a bit more than that so it might be the culprit. I have experienced window lag and other failures when the display is not being updated. As far as I know Input.poll() gets all input changes since the last call to Input.poll(). If I understand correctly the computer's hardware has a rather large input buffer as well. Missing keys just shouldn't happen.

\$\endgroup\$
  • \$\begingroup\$ After a cleanup in the mess that is my tutorial following project, I'm no longer able to reproduce the problem. Thanks for the information, this gave me a bit more insight on how this works. \$\endgroup\$ – dammkewl Jul 13 '14 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.