The LWJGL Keyboard class, what about isHeld and isReleased?

Started by Krisbam, October 11, 2010, 22:32:34

Previous topic - Next topic

Krisbam

Hello again!

I'm currently implementing a frame animation system, and I was wondering how I should go about handling it with LWJGL.
With other keyboard libraries I've used before, there has always been a way to check whether a key is pressed, held or released. This way, one can easily reset the animation when the key is released, etc.

Since there only seems to be a way to check whether a key is down or not, I'm not quite sure how I should go about tackling this problem. Anyone care shed a light on this?

Matthias

Use the event interface:

while(Keyboard.next()) {
   gui.handleKey(
      Keyboard.getEventKey(),
      Keyboard.getEventCharacter(),
      Keyboard.getEventKeyState());
}


Ciao Matthias

Krisbam

Quote from: Matthias on October 11, 2010, 22:58:28
Use the event interface:

while(Keyboard.next()) {
   gui.handleKey(
      Keyboard.getEventKey(),
      Keyboard.getEventCharacter(),
      Keyboard.getEventKeyState());
}


Ciao Matthias
Could you elaborate on that? I don't quite get it. I'm very new when it comes to Keyboard/Event handling as I haven't done much with GUI's before.
I appreciate the help very much!

CodeBunny

I would like a more in-depth explanation, also. I get what each method is supposed to do, in general, but more clarification would be good.

Also, the API says (for getEventKey()):

Please note that the key code returned is NOT valid against the current keyboard layout.


Uh... when is this going to get fixed? Has it been yet? If this is true, it sounds like the method is worthless.

Matthias

getEventKey() returns one of the KEY_xxx constants from the Keyboard class. The constant names may not match with the native keyboard layout. If you want to enter text use the getEventCharacter() method.

This is not a bug - so there is nothing to fix. And the method is for sure not worthless - it's very important and works.

CodeBunny

Okay, good to know. I mistook that to mean it didn't match the KEY_XXXX members of the Keyboard class.  :-[ My bad.


Here's a method I've put together that I'm hoping will create lists of all the pressed (not held, Keyboard manages that already) and released keys since the last update. I'm also compiling a list of chars, in case I want to deal with user text. What I can then do, is call wasKeyTyped(int key) and detect true only for the first tap, etc.

Does this work?

public static void update()
	{
		ArrayList<Integer> keysPressedSinceUpdate = new ArrayList<Integer>();
		ArrayList<Integer> keysReleasedSinceUpdate = new ArrayList<Integer>();
		ArrayList<Character> charactersTypedSinceUpdate = new ArrayList<Character>();
		
		for(int i = 0; i < Keyboard.getNumKeyboardEvents(); i++)
		{
			if(Keyboard.getEventKeyState())
			{
				charactersTypedSinceUpdate.add(Keyboard.getEventCharacter());
				if(!Keyboard.isRepeatEvent())
				{
					keysPressedSinceUpdate.add(Keyboard.getEventKey());
				}
			}
			else
			{
				keysReleasedSinceUpdate.add(Keyboard.getEventKey());
			}
			
			Keyboard.next();
		}
		
		characters = new char[charactersTypedSinceUpdate.size()];
		for(int i = 0; i < charactersTypedSinceUpdate.size(); i++)
		{
			characters[i] = charactersTypedSinceUpdate.get(i);
		}
		
		keysPressed = new int[keysPressedSinceUpdate.size()];
		for(int i = 0; i < keysPressedSinceUpdate.size(); i++)
		{
			keysPressed[i] = keysPressedSinceUpdate.get(i);
		}
		
		keysReleased = new int[keysReleasedSinceUpdate.size()];
		for(int i = 0; i < keysReleasedSinceUpdate.size(); i++)
		{
			keysReleased[i] = keysReleasedSinceUpdate.get(i);
		}
	}


characters is an array of chars, keysPressed and keyReleased and arrays of ints.

CodeBunny

The above code had a mild bug I found - I needed to call next() before each check instead of after.

But I still have a problem: The above code works perfectly fine as long as I don't hold down the key long enough to start repeat events. When repeat events start, it stops recognizing input for some reason.

Why is this?