How to get KeyUpEvents

Started by Se7enDays, March 19, 2008, 23:05:17

Previous topic - Next topic

Se7enDays

Hi,

i am using the Keyboard class for handling keyboard input events to turn lights on and off. It currently looks similar to this:

if(Keyboard.isKeyDown(Keyboard.KEY_L)) {
    light = !light;
    if (light) glEnable(GL_LIGHTING);
    else glDisable(GL_LIGHTING);


However, when i hold the L-Key down the light keeps flickering because many KeyDown-Events are generated. What would be the best way to ensure that the light is only toggled once when a user presses a key. A KeyUp-Event would help but there is none in the Keyboard class.

Matzon

either use a boolean toggle or use the keyboard even mechanism. The latter is probably better.

bobjob

this is one way, using Keyboard.getEventState();

while (Keyboard.next()) {

   int key = Keyboard.getEventKey();

   if (Keyboard.getEventKeyState()) {               //if key is DOWN
      switch (key) {

         case Keyboard.KEY_L:
            light = !light;
            if (light) glEnable(GL_LIGHTING);
            else glDisable(GL_LIGHTING);
         break;

         case Keyboard.KEY_#:
            ...
         break;

      }
   } else {                                                   //if key is is UP
      switch (key) {

         case Keyboard.KEY_#:
            ...
         break;

         case Keyboard.KEY_#:
            ...
         break;

      }
   }
}


Se7enDays

Thank you, it works fine now