LIBGDX: What is the meaning of the return value of an InputAdapter's keyDown, keyUp...etc

StackOverflow https://stackoverflow.com/questions/22779253

  •  25-06-2023
  •  | 
  •  

Question

I am having problems handling inputs from more than one separate InputAdapters that use keyDown/keyUp.

Gdx.input.setInputProcessor( new InputMultiplexer( keyboardController1, keyboardController2, keyboardController3));

Only the first one in line works, while the others don't; in this case keyboardController1. I am guessing it has something to do with the return true; at the end of the keyDown() methods'. I tried reading the documentation, tutorials, post...etc But, I still can't get a good grasp on what the return value means and does, nor to which boolean value I should set it to. My question: What is the meaning of the boolean return value of the keyDown/keyUp(and so forth)?

Était-ce utile?

La solution

The boolean value you return should be:

  1. true, if this InputProcessor or InputAdapter handled the input.
  2. false, if it did not handle the input.

Think about a game, where 2 players play on the same pc (splitscreen for example).
One player controlls with "WASD", the second with "Arrow keys". So the InputProcessor for the first player handles only the "WASD" keys and the InputProcessor for the second only the "Arrow keys".
If the pressed key was "W,A,S or D", the first InputProcessor handles it and you don't need to give it to the second one. So you return true. If the pressed key is one of the "Arrow keys", the first InputProcessor won't handle it and so you return false.

The multiplexer gets this return value and says:

  1. The return is true, input was handled, throw it away.
  2. The return was false, input not handled, give it to the next processor.

Hope you understand what i am talking about :P

Edit: the keyDown(int keyCode), keyUp(int keyCode) and other methods have the int keyCode, which tells you whihc key was pressed. Usually you use a switch to know which key was pressed. This switch looks like this:

switch(keyCode) {
case Keys.W:
     // Handle the event for "W" key pressed
case Keys.A:
     // Handle the event for "A" key pressed
// Other cases
default:
     return false;
}
return true;

For your other question in the comment: If 1 player is controlled by W,A,S,D he does not need to handle the input for other keys. So he returns false, if he did not handle the input and true otherwise. The second player is controlled by the arrow keys, so he does not need to handle other keys, like the W, A, S or D key. If you have more then one InputProcessor, which have to handle the same keys, you have to return false, so that the InputMultiplexer passes the event to every InputProcessor.

Autres conseils

if u are having more than one input processor in the game and u want that after your condition is handled by one of the processor it should not go to other processor than return true;

else if u want it to go to other processors also than u should return false.

Consider a case where u have two input processor and both have a key down. if u have a condition that only one should execute return true from both key downs else return false.

The documentation you linked yourself says the following

Returns: whether the input was processed

Returning true means that the event was processed by that handler and it won't be fired to any other handler which comes after that.

If you want the other processors in line to also receive the event and let them handle it as well, then your first handler needs to return false.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top