Keyboard - repeated events

Started by Busata, September 18, 2011, 20:53:04

Previous topic - Next topic

Busata

Hello!

I'm trying to write a small wrapper around the Keyboard class, and came up with the following:

protected void updateKeyStates() {
        currentKeyStates.clear();

        while (Keyboard.next()) {
            int keyCode = Keyboard.getEventKey();
            boolean eventState = Keyboard.getEventKeyState();
            long eventDuration = Keyboard.getEventNanoseconds();
            char eventChar = Keyboard.getEventCharacter();
            String associatedAction = getActionForKey(keyCode);
            System.out.println(associatedAction + " " + eventState);
            currentKeyStates.offer(new KeyEvent(keyCode, eventState, eventChar, eventDuration,associatedAction));
        }
    }


In the actual gamecode you can request the currentKeystates, loop through them & process. Now the problem I have is with repeated/multiple keyevents,
I have enabled repeatEvents with enableRepeatEvents(true); but when I for example press up & left key; only the last pressed key will be repeated & the up one won't be in the buffer.

Is this is a bug or intended? I was thinking about managing the keystate manually (so assuming that when a keyevent was pressed down, and the next run it's not "released", it's still pressed down). Or am I making it too complex? The reason I'm using a queue is to stay flexible & sort of keep the order in which the keys were pressed, if I ever want a combo system or something.

Thanks in advance for answering!

Chuck

If you're going to keep track of key state manually, I would definitely turn off key repeat.  And truthfully, unless you can prove that manually keeping track of keyboard state is appreciably faster, I would just stick with Keyboard.isKeyDown every update.  All the locking that Keyboard does is likely optimized away in any single-threaded program anyway.



Busata

Yep, I had a discussion about it on irc, I was complicating things :-) thanks!