Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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