Detecting user input via event polling
Typically, there are a couple of approaches when it comes to detecting user input. The most straightforward approach is event polling, which involves actively querying the state of a specific input peripheral whenever we want to make a decision based on it. This can happen either in every frame or on demand for specific bits of game logic.
Throughout this recipe, we will work with an example where you will learn how to query the keyboard, mouse, accelerometer, and compass of the device. The updated state of these devices will be shown on the screen.
Getting ready
The sample projects are required to follow this recipe; please make sure that you already have them in your Eclipse workspace.
How to do it…
The code for this recipe is found in the
InputPollingSample
class. We have the classic members to support regular rendering: camera, viewport, and sprite batch. Besides these, we add a BitmapFont
object to draw the state of the input devices on the...