Get pressed key

Started by Kajos, February 20, 2013, 17:36:34

Previous topic - Next topic

Kajos

I have the following code:

    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

quew8

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()) {...}

Kajos

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?

quew8

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.

Kajos

Yeah, but it would be nice if one could opt out of this feature, that's what I mean.

quew8

And what I mean is that if you could opt out of it then you could break every other running program without even trying.

Kajos

Ah okay, so it´s a requirement :P

quew8

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.