Event handling allows you to get more granular and most of all chronological information about input from the user. Event handling provides a way to implement interactions with user interfaces, where specific input sequences are important, e.g. touch down, touch up on a button means the user clicked the button. Such interactions are hard to implement with polling.

Input Processor

Event handling is done using the common observer pattern. First we have to implement a listener interface called InputProcessor:

  1. public class MyInputProcessor implements InputProcessor {
  2. public boolean keyDown (int keycode) {
  3. return false;
  4. }
  5. public boolean keyUp (int keycode) {
  6. return false;
  7. }
  8. public boolean keyTyped (char character) {
  9. return false;
  10. }
  11. public boolean touchDown (int x, int y, int pointer, int button) {
  12. return false;
  13. }
  14. public boolean touchUp (int x, int y, int pointer, int button) {
  15. return false;
  16. }
  17. public boolean touchDragged (int x, int y, int pointer) {
  18. return false;
  19. }
  20. public boolean mouseMoved (int x, int y) {
  21. return false;
  22. }
  23. public boolean scrolled (float amountX, float amountY) {
  24. return false;
  25. }
  26. }

The first three methods allow you to listen for keyboard events:

  • keyDown(): Called when a key was pressed down. Reports the key code, as found in Keys.
  • keyUp(): Called when a key was lifted. Reports the key code as above.
  • keyTyped(): Called when a Unicode character was generated by the keyboard input. This can be used to implement text fields and similar user interface elements.

The next five methods report mouse/touch events:

  • touchDown(): Called when a finger went down on the screen or a mouse button was pressed. Reports the coordinates as well as the pointer index and mouse button (always Buttons.LEFT for touch screens).
  • touchUp(): Called when a finger was lifted from the screen or a mouse button was released. Reports the last known coordinates as well as the pointer index and mouse button (always Buttons.Left for touch screens).
  • touchDragged(): Called when a finger is being dragged over the screen or the mouse is dragged while a button is pressed. Reports the coordinates and pointer index. The button is not reported as multiple buttons could be pressed while the mouse is being dragged. You can use Gdx.input.isButtonPressed() to check for a specific button.
  • mouseMoved(): Called when the mouse is moved over the screen without a mouse button being down. This event is only relevant on the desktop and will never occur on touch screen devices where you only get touchDragged() events.
  • scrolled(): Called when the scroll wheel of the mouse was turned. Reports either -1 or 1 depending on the direction of spin. This will never be called for touch screen devices.

Each of the methods returns a boolean. We’ll look into why that is in the InputMultiplexer section below.

Once you implement your InputProcessor you have to tell libGDX about it so it can be called when a new input event arrives:

  1. MyInputProcessor inputProcessor = new MyInputProcessor();
  2. Gdx.input.setInputProcessor(inputProcessor);

From this point on, all new input events will be pushed to the MyInputProcessor instance. Events are dispatched right before the call to ApplicationListener.render(), on the rendering thread.

InputAdapter

InputAdapter implements all the InputProcessor methods, returning false from each. You can extend InputAdapter so you only need to implement the methods you need. You can also use an anonymous inner class.

  1. Gdx.input.setInputProcessor(new InputAdapter () {
  2. @Override
  3. public boolean touchDown (int x, int y, int pointer, int button) {
  4. // your touch down code here
  5. return true; // return true to indicate the event was handled
  6. }
  7. @Override
  8. public boolean touchUp (int x, int y, int pointer, int button) {
  9. // your touch up code here
  10. return true; // return true to indicate the event was handled
  11. }
  12. });

InputMultiplexer

Sometimes you want to chain InputProcessors, e.g. you have one processor for your UI which should be invoked first, and a second processor for input events that manipulate your game’s world. You can use the InputMultiplexer class to achieve this:

  1. InputMultiplexer multiplexer = new InputMultiplexer();
  2. multiplexer.addProcessor(new MyUiInputProcessor());
  3. multiplexer.addProcessor(new MyGameInputProcessor());
  4. Gdx.input.setInputProcessor(multiplexer);

The InputMultiplexer will hand any new events to the first InputProcessor that was added to it. If that processor returns false from the method invoked to handle the event, this indicates the event was not handled and the multiplexer will hand the event to the next processor in the chain. Through this mechanism, the MyUiInputProcessor can handle any events that fall inside one of its widgets and pass on any other events to the MyGameInputProcessor.

Example of continuous Input handle

If you want to move an actor using the Input Processor, you will notice that it will move only when the key is typed (or pressed with keydown). To continuosly handle input, or to move a sprite, you could add a flag to your actor like this:

  1. public class Bob
  2. {
  3. boolean leftMove;
  4. boolean rightMove;
  5. ...
  6. updateMotion()
  7. {
  8. if (leftMove)
  9. {
  10. x -= 5 * Gdx.graphics.getDeltaTime();
  11. }
  12. if (rightMove)
  13. {
  14. x += 5 * Gdx.graphics.getDeltaTime();
  15. }
  16. }
  17. ...
  18. public void setLeftMove(boolean t)
  19. {
  20. if(rightMove && t) rightMove = false;
  21. leftMove = t;
  22. }
  23. public void setRightMove(boolean t)
  24. {
  25. if(leftMove && t) leftMove = false;
  26. rightMove = t;
  27. }

Then, in your Input processor:

  1. ...
  2. @Override
  3. public boolean keyDown(int keycode)
  4. {
  5. switch (keycode)
  6. {
  7. case Keys.LEFT:
  8. bob.setLeftMove(true);
  9. break;
  10. case Keys.RIGHT:
  11. bob.setRightMove(true);
  12. break;
  13. }
  14. return true;
  15. }
  16. @Override
  17. public boolean keyUp(int keycode)
  18. {
  19. switch (keycode)
  20. {
  21. case Keys.LEFT:
  22. bob.setLeftMove(false);
  23. break;
  24. case Keys.RIGHT:
  25. bob.setRightMove(false);
  26. break;
  27. }
  28. return true;
  29. }

Prev | Next