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)?
The boolean
value you return should be:
InputProcessor
or InputAdapter
handled 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:
true
, input was handled, throw it away.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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With