2
\$\begingroup\$

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 Keyboard.next() and Mouse.next(). This works fine, except if you have multiple places where you want to do this check.

So if you do Mouse.next once for all the events in the 'queue', the queue is empty and future Mouse.next'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.

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

Accordingly to http://legacy.lwjgl.org/javadoc/org/lwjgl/input/Keyboard.html you can make this like this:

while (Keyboard.next())
{
    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.

Edit:

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
KeyboardEvents.clear();
while (Keyboard.next())
{
    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.

\$\endgroup\$
3
  • \$\begingroup\$ Have you read my question? "So if you do Mouse.next once for all the events in the 'queue', the queue is empty and future Mouse.next's don't return anything.". So I already tried this, but it only works once every frame. \$\endgroup\$
    – Basaa
    Jul 7, 2013 at 6:07
  • \$\begingroup\$ So let me update the answer, 1 min \$\endgroup\$ Jul 7, 2013 at 6:13
  • \$\begingroup\$ Answer edited, hope it fits your needs. \$\endgroup\$ Jul 7, 2013 at 6:26
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ Jul 7, 2013 at 2:03
  • \$\begingroup\$ That's what I said. But I have to know how to do that in LWJGL. \$\endgroup\$
    – Basaa
    Jul 7, 2013 at 6:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .