Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

  • http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/InputProcessor.html#keyDown%28int%29
like image 235
coffeenet Avatar asked Jan 10 '23 21:01

coffeenet


1 Answers

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.

like image 161
Robert P Avatar answered Jan 17 '23 15:01

Robert P