LWJGL Forum

Programming => OpenGL => Topic started by: Se7enDays on March 19, 2008, 23:05:17

Title: How to get KeyUpEvents
Post by: Se7enDays on March 19, 2008, 23:05:17
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.
Title: Re: How to get KeyUpEvents
Post by: Matzon on March 20, 2008, 09:44:53
either use a boolean toggle or use the keyboard even mechanism. The latter is probably better.
Title: Re: How to get KeyUpEvents
Post by: bobjob on March 20, 2008, 10:22:48
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;

      }
   }
}

Title: Re: How to get KeyUpEvents
Post by: Se7enDays on March 20, 2008, 21:24:36
Thank you, it works fine now