Difference between revisions of "Event-Driven Input"
Line 43: | Line 43: | ||
* [[Polling the Current Event State]] | * [[Polling the Current Event State]] | ||
* [[Event-Driven Input]] | * [[Event-Driven Input]] | ||
+ | |||
+ | [[Category:Input]] |
Revision as of 16:38, 8 August 2013
In some cases it makes sense to be notified when input changes occur. The most obvious case is when implementing a GUI. It's important that key presses aren't missed (which could happen if you poll input) and GUI are also generally event driven. The InputListener defines a set of methods that can be implemented to receive notifications of input events from an Input instance by adding it using addListener()
.
Note that if you're using a subclass of basic game that it already implements InputListener and hence you can simply use override the interface methods from the BasicGame class.
Contents
Mouse
Mouse events are reported to the input listener through the following methods:
public void mouseMoved(int oldx, int oldy, int newx, int newy) public void mousePressed(int button, int x, int y) public void mouseReleased(int button, int x, int y) public void mouseWheelMoved(int change)
What the methods are indicating when they're called should be fairly self explanatory, however it is worth noting that the origin of the screen for mouse coordinates is the top left - unlike that of OpenGL which uses bottom left. This means if the mouse is clicked at the top of the screen the mousePressed()
method will be called with a y value of zero.
Keyboard
Keyboard events are reported each time a key is pressed and release to the following two methods on the InputListener interface:
public void keyPressed(int key, char c) public void keyReleased(int key, char c)
Note that both the key code and the character represeting that key are reported. The character will only be reported for keys that can be visualised, a value of zero will be reported for other keys (like CTRL, ALT, etc).
Controllers
As with keyboard and mouse, game controllers connected will report events as buttons and directional controls are pressed. The following input listener methods will be called when controllers are manipulated:
public void controllerButtonPressed(int controller, int button) public void controllerButtonReleased(int controller, int button) public void controllerDownPressed(int controller) public void controllerDownReleased(int controller) public void controllerLeftPressed(int controller) public void controllerLeftReleased(int controller) public void controllerRightPressed(int controller) public void controllerRightReleased(int controller) public void controllerUpPressed(int controller) public void controllerUpReleased(int controller)
Each event method includes the index of the controller on which the change was detected. Basic game also provides this state as protected member variables. Controller input on events is very special case. In nearly all cases polling controllers state is more appropriate than relying on the input listener.
See Also
- Polling the Current Event State
- Event-Driven Input