Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As in many game libraries such as LWJGL, you can check mouse/keyboard events with Mouse.isButtonDown or Keyboard.IsKeyDown.

I want to check for button/key presses, not if it's currently pressed down (if that makes any sense) so I use and This works fine, except if you have multiple places where you want to do this check.

So if you do once for all the events in the 'queue', the queue is empty and future's don't return anything.

What would be the solution for this? Create a wrapper around the Keyboard class and save the keystate? If so, how can I save that state? Couldn't find anything in the javadocs.

Thanks in advance.

share|improve this question
up vote 5 down vote accepted

Accordingly to you can make this like this:

while (
    bool pressed = Keyboard.getEventKeyState();
    int key = Keyboard.getEventKey();
    if(pressed) processKeyPress(key);
    else processKeyRelease(key);

For more detail, you can google "Buffered input vs Unbuffered input" Buffered is when you receive the events of what happened, and unbuffered is when you have to poll the key state with "isKeyDown" at every frame.


For being able to use the events in more than one place, do the following:

class KbEvent
    public final bool Press;
    public final int KeyID;
    public KbEvent(bool press, int keyID)
        Press = press; KeyID = keyID;

//On your game definition
Vector<KbEvent> KeyboardEvents;

//On your game loop
while (
    KeyboardEvents.add(new KbEvent(Keyboard.getEventKeyState(), Keyboard.getEventKey()));

//Wherever you want to read all keyboard input:
for (KbEvent ev : KeyboardEvents)
    if(ev.Press) processKeyPress(ev.KeyID);
    else processKeyRelease(ev.KeyID);

practically the same extends for mouse, with just more variables.

This way, you'll save the current frame events elsewhere, and the re-use them everywhere you want. I don't know exactly HOW java handles the Vector class, but on C++ this should have no impact on frame rate.

share|improve this answer
Have you read my question? "So if you do once for all the events in the 'queue', the queue is empty and future's don't return anything.". So I already tried this, but it only works once every frame. – Basaa Jul 7 '13 at 6:07
So let me update the answer, 1 min – Gustavo Maciel Jul 7 '13 at 6:13
Answer edited, hope it fits your needs. – Gustavo Maciel Jul 7 '13 at 6:26
Thank you Gustavo. Much appreciated. – Basaa Jul 7 '13 at 7:36

I don't know the details for LWJGL, but in XNA, developers need to create a wrapper that keeps track of the last state of the mouse & keyboard, and the current state. That is the only way to detect a "press" or a "release". I would look for a way to remember what keys/buttons were pressed down, then use that to determine if something has changed.

share|improve this answer
XNA also have the nice method KeyboardState.GetPressedKeys() that returns an array with the keys that are currently pressed. This way you just have to check the current frame array with the previous' frame, for the keys that differ, you can send an press/release event. – Gustavo Maciel Jul 7 '13 at 2:03
That's what I said. But I have to know how to do that in LWJGL. – Basaa Jul 7 '13 at 6:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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