Hello Guest

Get pressed key

  • 7 Replies
  • 7524 Views
*

Offline Kajos

  • *
  • 24
Get pressed key
« on: February 20, 2013, 17:36:34 »
I have the following code:

Code: [Select]
    private int getKeyboardKeyPressed() {
        while (Keyboard.next()) {
            if (Keyboard.getEventKeyState()) {
                int key = Keyboard.getEventKey();
                if (key == Keyboard.KEY_ESCAPE)
                    return -1;
               
                return key;
            }
        }
        return -1;
    }
   
    private int getMouseKeyPressed() {
        return Mouse.getEventButton();
    }

Mouse.getEventButton always returns 0 and the keyboard never returns anything. What am I doing wrong? I am using Swing to create a button and when the button is pressed the user has a second or three to input a key. I figure Swing could be causing the mouse to always return zero.

Now that I'm thinking about it, it could very well be that the window with Swing components doesn't have lwjgl's input attention (however this would contradict the previous statement, since it should return -1 for mouse then). What would be the best way to solve this?

Thanks, Kajos

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Get pressed key
« Reply #1 on: May 05, 2013, 20:27:21 »
If the LWJGL display does not have focus, Keyboard will not give you any events.
With the mouse, (the above also applies btw) you have to do the same iterating through events with while(Mouse.next()) {...}

*

Offline Kajos

  • *
  • 24
Re: Get pressed key
« Reply #2 on: May 05, 2013, 21:05:54 »
Yeah, I found that out. Would be handy if there was some integration in Swing or some way to get results of the keyboard without having the window focused. This isn't actually a requirement right, that the window needs to be focused?

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Get pressed key
« Reply #3 on: May 06, 2013, 10:23:49 »
The whole point of focus is that you can work in one program at a time and anything else running won't be "stealing" your keyboard and mouse events. Even having two text boxes in the same window would require focus because you have to know which one is being typed in. Essentially nothing could function without it.
I'm not sure what you mean about it being a requirement.

*

Offline Kajos

  • *
  • 24
Re: Get pressed key
« Reply #4 on: May 06, 2013, 10:44:24 »
Yeah, but it would be nice if one could opt out of this feature, that's what I mean.

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Get pressed key
« Reply #5 on: May 06, 2013, 18:05:57 »
And what I mean is that if you could opt out of it then you could break every other running program without even trying.

*

Offline Kajos

  • *
  • 24
Re: Get pressed key
« Reply #6 on: May 06, 2013, 20:23:36 »

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Get pressed key
« Reply #7 on: May 06, 2013, 22:03:10 »
Well I'm pretty sure that the Keyboard / Mouse classes just use wrappers around the native platform windowing input method (as in winapi for windows). So it would be a requirement for the window to have focus.