Keyboard events.. problem here and there...

Started by Gpro, March 02, 2004, 03:43:06

Previous topic - Next topic

Gpro

Hi all experts,

Im a newbie for lwjgl, now im dealing with the keyboard inputs
Im trying to detect the upkey event and the downkey events, as what i know, the keyboard class has only isKeyDown(); but i just want to trigger the down event, instead of both events.

Thank you!!!! hope that i get reply soon... 8)

Matzon

The keyboard operates in two modes. Buffered and Unbuffered.

Unbuffered
Unbuffered mode gives you access to all keys at a the current time. That is, when you call Keyboard.poll(); you capture the state of the keyboards 256 keys (if that many available). You can then iterate all these 256 keys to check for isKeyDown();
This allows you to have direct access, the problem being that you might miss a key event, if you poll too slowly.

Buffered
When working in buffered mode, you have additional access to
getEventCharacter (only if translation has been enabled too)
getEventKey
getEventKeyState
methods were public fields in previous version (<0.9)

You would then call poll followed by a read();
which would give you access to both current keys, and any keys which have been pushed down and up.
You then iterate the events by calling:
int count = Keyboard.getNumKeyboardEvents();
while (Keyboard.next()) {
  if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
    // do stuff
  }
}


did that help ?  :roll:

tomb

There is two ways of ketting keybaord input in lwjgl: polling and buffered. isKeyDown() is the polling type and from this you can only find wich keys is down at the time of the poll().

Then there is buffered input, wich buffers all the keyevents, and is what you are after. Here is some code:
Keyboard.create();
Keyboard.enableBuffer(); // has to be called once after Keyboard is created to enable buffering

...
// inside your gameloop:
Keyboard.read();
while (Keyboard.next()) {
   boolean keyPressed = Keyboard.state;
   if (keyPressed && Keyboard.key == Keyboard.KEY_ESCAPE) {
      // escape key was pressed
   } else if (keyPressed == false && Keyboard.key == Keyboard.KEY_SPACE) {
      // space key was released
   }
}

Keyboard.read() fills the buffer. Keyboard.next() iterates the filled buffer. Keyboard.state gives you wether the key was pressed or released. Keyboard.key gives you wich key was pressed or released.

This will be made more clear in lwjgl 0.9, with better javadoc and static functions to get state and key.

tomb

Matzon beat me to it there. Damn my slow typing.

Btw. I'm looking forward to referring to the javadoc when these type of questions come up  :wink:

Matzon

hehe, yeah - but it did result in one interesting thing - my compare of keys did not take into account whether it was relased or pressed - just that the event "occured" - so yours is more correct :)

on a related note, shouldn't 'getNumKeyboardEvents()' be called getKeyboardEventCount() :?:

Gpro

hi hi im back,
Ya i think all the codes helps.. Thanks to Matzon and tomb!!!!!

Gpro :roll:

Soulice

so if I want to use a key press to turn off or on a variable, which is the method to use?  I dont want it to rifle thru and turn it off and on 60 times this frame.  If you press S, shields are on, if you press S again, shields are off.
S