Do I need to Keyboard.poll()?

I am using Keyboard.isKeyDown(KEY_xxxx) to detect keypresses & have not included a call to Keyboard.poll() in the main loop.

This works, but am I creating a steadily growing list of unprocessed key events?

If so, would a periodic call to Keyboard.poll() clear them?

On a similar vein, do I need to call Mouse.poll() regularly to avoid a build up of mouse events?

Alan

/Edit - The description of Display.update() says it polls input devices. I do have that in my code. Hopefully that means there is no need to poll separately.

you need to read the buffered events, or simply discard them:

while (Keyboard.next()) { /* loop through events*/}

Matzon,

Thanks for the reply, which has confirmed my suspicions. Should I empty the mouse queue too?


while (Keyboard.next()) { };
while (Mouse.next()) { };

(I am assuming it is Mouse.next() but can’t check right now as the javadoc’s on my other computer)

Alan

If I remember correctly the event queues (at least the keyboard event queue) only queue up to some 10 (or so) events…
I’d say there is no need to “pseudo” process them just to make them go away !?

LastBoyScout

Interesting.

I checked memory usage & it didn’t seem to grow significantly during game play without emptying the event queues.

Looking at the source we have (for keyboard)


	public static void poll() {
		if (!created)
			throw new IllegalStateException("Keyboard must be created before you can poll the device");
		Display.getImplementation().pollKeyboard(keyDownBuffer);
		read();
	}

	private static void read() {
		readBuffer.compact();
		int numEvents = Display.getImplementation().readKeyboard(readBuffer, readBuffer.position());
		readBuffer.position(readBuffer.position() + numEvents*EVENT_SIZE);
		readBuffer.flip();
	}


I guess I need to check what happens if you try to write past the end of a NIO buffer :wink:

Alan :slight_smile:

/Edit BufferOverflowException it seems - so why didn’t this happen?

coz the native code only adds up to a certain point
then it starts warning if debug is turned on I think :slight_smile: -Dorg.lwjgl.util.Debug=true

Thanks.

All clear now.

Alan :slight_smile: