LWJGL Forum

Programming => OpenGL => Topic started by: ronen4kill on July 27, 2011, 18:30:48

Title: key pressed problem
Post by: ronen4kill on July 27, 2011, 18:30:48
Hi all :)

i'm having a little problem with opengl and LWJGL

i try do do that if you press one time f1 it will show you your mouse cords and if you press the 2nd time the cords will disapear.

now the problem is that if i press f1 its shows the cords and suddenly it disapears.. any help??

http://pastebin.com/ZV5S6YEv
Title: Re: key pressed problem
Post by: broumbroum on July 27, 2011, 19:34:51
   if (Keyboard.getEventKeyState()) is not the correct manner.
As you've got to know KeyEvent "events" (^^), they have several associated states (integers values). When you press a key, a key_pressed is dispatched and when you release the key a key_released is dispatched then. 
switch(Keyboard.getEventKeyState()) {case KeyPressed: break; case KeyReleased: break;...etc.} That's the way you can read KeyEvents.
:D
Title: Re: key pressed problem
Post by: Fool Running on July 28, 2011, 12:29:10
Actually he is using Keyboard.getEventKeyState() exactly right. From the javadoc:
QuotegetEventKeyState

public static boolean getEventKeyState()
Gets the state of the key that generated the current event
Returns:
True if key was down, or false if released

His problem is that he is only rendering the mouse coordinates inside the 'if' that handles the first F1 key down. As soon as the display updates again, the display is cleared and the rendering of the mouse coordinates is lost. Try something more like this:
                while (true) {
                        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
                        while(Keyboard.next())
                        {
                                if (Keyboard.getEventKeyState())
                                {
                       
                                        if(Keyboard.getEventKey() == Keyboard.KEY_F1 && count == 0)
                                        {
                                                count++;
     
                                        }else if(Keyboard.getEventKey() == Keyboard.KEY_F1 && count == 1)
                                        {
                                                count = 0;
                                        }
                                }
                        }
                       
                        if (count == 1)
                                render();

                        Display.update();

                        if (Display.isCloseRequested()) {
                                Display.destroy();
                                System.exit(0);
                        }
                }

Title: Re: key pressed problem
Post by: ronen4kill on July 28, 2011, 13:57:06
thanks for the help :) it works
Title: Re: key pressed problem
Post by: broumbroum on July 28, 2011, 18:50:27
alright ! .)