Post by The Guy on Dec 14, 2008 15:26:50 GMT
3D on the web with Java and Irrlicht - Keyboard Input
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.
EngineManager.java Source Code
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.
BaseObject.java Source Code
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.
Player.java Source Code
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.
Check out the online demo here, browse the source code here, and download the source code in a TAR file here.
next>>
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.
EngineManager.java Source Code
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.
BaseObject.java Source Code
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.
Player.java Source Code
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.
Check out the online demo here, browse the source code here, and download the source code in a TAR file here.
next>>