Listening to Keyboard while using OpenGL

I have just started using OpenGL and I need to be able to listen to the keyboard.
Earlier I used to make a boolean[] (of length 8192) and add a KeyListener. Then I’d just set it so that the information would get put into the array. Like this:


buttonsPressed = new boolean[8192];
		frame.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {/*Not used*/}
			
			@Override
			public void keyPressed(KeyEvent e) {
				buttonsPressed[e.getKeyCode()] = true;
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				buttonsPressed[e.getKeyCode()] = false;
			}
		});

Now I wonder, how am I supposed to do the same thing with OpenGL?
I have no idea how to do this, can you just go “Display.addaddKeyListener(new KeyListener()…” maybe? (I find that a bit unlikely)

You can’t use listeners with LWJGL.

You need to poll input with the Keyboard class.

This method summary section of the LWJGL API documentation is what you need.

Look for create(), poll(), destroy(), and update(), and if needed, look at Stack Overflow questions:







If you’re using LWJGL, Keyboard.next() will read the next keyboard event. To get event information, do Keyboard.getEventKeyCode(); and Keyboard.getEventState();

You also might want to do something like Keyboard.getEventCharacter(); which will return the visible character. You can use this to make input fields, where users enter information.

@tom_mai78101
Thank you for a lot of information. I’m going to start reading it straight away ;D
One quick questiong though (to avoid doing an embarrasing mistake like I did recently), does the KyeBoard have to be controlled by the same Thread as the one which handles OpenGL or can I just create it and let another Thread handle it? I want to have one Thread for the graphics and one for everything else and if it’s possible to do this it would simplify things.

Thank you very much for the help!

Yes, the keyboard has to be handled within that one thread, and it’ll probably be a lot faster that way too.

Hello again!

I got it working and all that now but there is one problem I have.
The Keyboard doesn’t seem to generate any Events when you press the arrow-keys. It works for all letters and such but not for the arrows.
Since I’m making a game this is a bit inconvenient. Does anyone have a solution?
I’d be happy enough if I could make the arrow-keys cause an Event pointing at the W, S, A, D keys since they are often used too for movement.

One more thing, I get which key caused the Event by calling getEventCharacter() and it works fine for pressing keys but I just get a blank instead of a key when I release the key. I need a reliable way of telling when a certain key which is held down is released. Any ideas?

You shouldn’t be using getEventCharacter() for identifying which key is being pressed or released. Use getEventKey() instead and compare it to the constants in Keyboard (Keyboard.KEY_UP for example).

Code that does the same as the code in your first post (untested):


while(Keyboard.next()){
    buttonsPressed[Keyboard.getEventKey()] = Keyboard.getEventKeyState();
}

Thank you Agent.
Now it works perfectly and as a bonus I can even use it the same way as that GameCore you helped me write (with a boolean[])