Hello Guest

[CLOSED] Event Keys in jinput

  • 1 Replies
[CLOSED] Event Keys in jinput
« on: November 30, 2011, 21:35:04 »

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.

Code: [Select]
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)

« Last Edit: November 30, 2011, 21:38:09 by n4pgamer »


Offline kappa

  • *****
  • 1316
Re: [IDEA] Event Keys in jinput
« Reply #1 on: February 24, 2012, 15:07:10 »
using the Keyboard.isKeyDown() method like you use above can lead to missed keys, hence the need for an event buffer.