Almost every application needs to respond to user input, which means there needs to be a way to find out what keys on the keyboard have been pressed or how the mouse has been moved.
Irrlicht makes these input events available to an application through the IEventReceiver class. By extending the IEventReceiver class, and supplying the extended class to the createDevice function, we can receive notifications of keyboard key presses and mouse movements through the IEventReceiver OnEvent function.
In this tutorial the EngineManager will extend the IEventReceiver class in order to receive input events.
The EngineManager passes itself to the createDevice function, which is called in the startupEngineManager function. This causes the OnEvent function to be called when an input event is detected by Irricht. Irrlich calls OnEvent for a wide range of events, but we are only interested in the keyboard events at this point. By checking the value returned by event.getEventType() we can find out what type of event caused OnEvent to be called. The EEVENT_TYPE.EET_KEY_INPUT_EVENT type is used to identify keyboard events.
By getting the values returned by isKeyInputPressedDown() and event.getKeyInputKey() we determine what key triggered the event, and wether it was pressed or released. We use this information to then call either keyDown or keyUp on our collection of BaseObjects.
Just like the enterFrame function, the BaseObject class now defines the empty keyDown and keyUp functions. By extending the BaseObject class an object can optionally override the keyDown and keyUp functions to be notified of keyboard events.
The Player class is an example of a class that overrides the keyDown and keyUp events to respond to keyboard input. In the case of the Player class a number of Boolean values are set to true when a key is pressed down and false when the key is released. The Boolean values are then used in the enterFrame function to move the object around on the screen.
This post is part of the series: 3D on the web with Java and Irrlicht
Learn how to use the Irrlicht 3D engine with Java to deliver 3D application via the web.
- 3D on the web with Java and Irrlicht - Getting Started
- 3D on the web with Java and Irrlicht - Lighting
- 3D on the web with Java and Irrlicht - Displaying 2D textures
- 3D on the web with Java and Irrlicht - Keyboard Input
- 3D on the web with Java and Irrlicht - 2D Collision Detection
- 3D on the web with Java and Irrlicht - Effects