Keyboard Input Issue

Started by Freezerburn, January 07, 2012, 20:54:19

Previous topic - Next topic

Freezerburn

While working on various things in my general game engine, there were occasionally times that I would notice all of the keyboard input completely stopping until I restarted the entire window. It was odd, but I mostly ignored it until now, when I have discovered what is causing this issue. When I press, and hold, the spacebar, left shift, and esc keys (at least, I haven't tested every key on my keyboard) all input will completely stop. I even put in some code to let me know when I get any input whatsoever, and it never prints anything after the initial spacebar press event. I have absolutely no idea why this is happening, and I am hoping it is just my own code and can be easily fixed.

I am using Java 1.6 Update 1 for OS X Lion, with LWJGL version 2.8.2 (latest Stable Release on main page). I am working in version 11 of the IntelliJ IDEA IDE. (if that matters at all). Also of note is that I am using a Japanese keyboard, so some keys are in different locations, so I have some slightly more complicated code than might be necessary to accommodate that. (for example, where a backslash key is, I have a ¥ symbol) I noticed that the press event gave me the correct character along with an integer, so I am placing all press events into a Map so I can later get them back when I get the release event without using the Keyboard utility method, as it gives incorrect results. (giving me a \ when I should be getting a ¥ symbol, for instance)
Here is the code I use for keyboard events:
    private void checkKeys() {
        if( Keyboard.getNumKeyboardEvents() > 0 ) {
            System.out.println( "Got events" );
            while( Keyboard.next() ) {
                System.out.println( "Processing event" );
                boolean keyState = Keyboard.getEventKeyState();
                int key = Keyboard.getEventKey();
                char keyChar = ' ';
                
                if( keyState ) {
                    keyChar = Keyboard.getEventCharacter();
                    if( keyChar == '_' || keyChar == 'Â¥' ) continue;
                    if( keyMap.containsKey( key ) ) {
                        if( keyChar != keyMap.get( key ) ) {
                            System.out.println( "KEY CONFLICT FOUND FOR " + key + ", O=" + keyMap.get( key ) +
                                                " N=" + keyChar );
//                            System.exit( 1 );
                        }
                    }
                    else {
                        keyMap.put( key, keyChar );
                    }
                }
                else {
                    try {
                        keyChar = keyMap.get( key );
                    }
                    catch( NullPointerException e ) {
                        continue;
                    }
                }
                
                System.out.println( keyChar + ", " + keyState );

                if( keyChar == 'q' ) {
                    running = false;
                }
                else if( keyChar == 'p' ) {
                    if( keyState ) {
                        isPaused = isPaused ? false : true;
                    }
                }

                if( keyState ) {
                    EntityManager.giveKeyPress( keyChar, key );
                }
                else {
                    EntityManager.giveKeyRelease( keyChar, key );
                }
            }
        }


Please let me know if there is an issue in my code which might be causing this, or if this is some kind of bug in LWJGL I can work around, or whatever else. All help is much appreciated.

Freezerburn

I managed a workaround for this, thankfully, so here's what I did:

Apparently, enabling repeat events for the Keyboard will remove any issues with key input stopping entirely. So during my initialization phase I make sure to enable that. Then I simply add in a small check looking for a key that is a repeat and that is being pressed and ignore it entirely. This gives the same results as normally using the Keyboard class without repeat events, and doesn't kill keyboard input.
Hopefully this helps anyone else that might have the same issue, or if there is an issue in the LWJGL library itself for my specific scenario, gives the devs an idea of what is happening.