[CLOSED] Event Keys in jinput

Started by n4pgamer, November 30, 2011, 21:35:04

Previous topic - Next topic

n4pgamer

Hello.

I'm referencing LWJGL 2.8.1.
I had some trouble with the event keys in the class Keyboard. Because they were queued and I think that's not needed in games with hardware acceleration. So I added another input layer to handle event keys properly. I'll give you my code and you reconsider if you wanna add this feature to the Keyboard / Mouse.

import org.lwjgl.input.Keyboard;

public class MyKeyboard
{
	// remembers only one key because it is enough for me
	private static int lastKey = 0;

	public static boolean isKeyEvent(int key)
	{
		// checks the last event key being released to release it for the next hit
		if ((lastKey > 0) && (!Keyboard.isKeyDown(lastKey)))
			lastKey = 0;
		// blocks key repetetion 
		if (lastKey == key)
			return false;
		// here comes the hit (very first time the pressed key is recognized)
		if (Keyboard.isKeyDown(key))
		{
			lastKey = key;
			return true;
		}
		// key isn't pushed
		return false;
	}
	}
}

The use is simple: MyKeyboard.isKeyEvent(int)
It worked good for me because it easily recognized a simple key hit and never repeated it when the player is slow on releasing the key. You might want to save more than 1 key. (If you had that feature already in you class and I used it wrong or not at all then I'm sorry)

Cheers!

kappa

using the Keyboard.isKeyDown() method like you use above can lead to missed keys, hence the need for an event buffer.

Closed.