1
\$\begingroup\$

Currently I am implementing the possibility in my game to change the keyboard configuration.
Therefor, I have a menu in which is a button for each action the player can do. If the player clicks on one of these buttons, the game should wait until the player hits a key on the keyboard.

But this is my problem. I have no idea how to wait for a keypress. I tried to do something like:

while(!Keyboard.next()){}
// Now there should be events. Just get the key of the last event.

But it did not work and ended with an infinite loop.
The problem is, it has to work in a single method, as it is the onClick method of the button.

Does somebody know a way to handle this problem correctly?
And how is this done usually? (I have seen such functionality in some games)

\$\endgroup\$
2
\$\begingroup\$

As I'm sure you found: while(!Keyboard.next()){} would just wait forever, since Keyboard is not getting updated during the wait.

In your OnClick function, I'd store the action that's being set. Then, when entering your update loop, check to see if you're waiting for an action. If you are, check for a keypress. Something like:

//check for cancel (like clicking a separate action)
if(actionToSet != null) {
    if(Keyboard.hasNext()) {
        //check to make sure key pressed is not cancel set action key (like ESC)
        actionToSet.bind(Keyboard.getNext());
    }
}
//do other stuff
\$\endgroup\$
1
\$\begingroup\$

Thanks to MichaelHouse for his suggestion, but what I actually found works (since those methods he suggested didn't appear to exist for the Keyboard class I had), is to set a boolean waitingToSet and in my update method, I had this:

    if (waitingToSet){
        if (Keyboard.getEventKeyState()){
            System.out.println(Keyboard.getKeyName(Keyboard.getEventKey()));
            waitingToSet = false;
        }
    }

This worked perfectly for me. Of course you can just then go and bind that Keyboard.getEventKey() returned value as the setting you want.

EDIT: Just a caveat: if you're planning on using this with LibGDX, it won't work properly. The Integer values for the Keyboard class and the Input.Keys field are different, so calling Gdx.Input.isKeyPressed(i) where i is the int value you caught earlier, will map to the wrong key. It turned out to be a huge mission for me to get new key mappings to work properly for LibGDX. Ended up using a Preferencesfile and writing my own method that looped through every possible Key and/or button to return the proper one:

public static int getKey(){
    for (int i=0;i<256;i++){
        if (Gdx.input.isKeyJustPressed(i)){
            return i;
        }
    }
    return -1;
}

This worked perfectly. So change the Keyboard.getEventKeyState()to Gdx.input.isKeyJustPressed(Input.Keys.ANY_KEY) and then call the above method inside the if statement. Then you'll have the right key.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.