Java and Irrlicht tutorial series - Keyobard Input

Java and Irrlicht tutorial series - Keyobard Input
Page content

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.

Related Article

**<img src="https://img.bhs4.com/7B/0/7B0BA5D2579A6D8043C9E4E793AD958582DB9530_large.jpg" alt="sandy">**

Flash and JavaScript 3D with Sandy-HX

This free article series shows you how to get the most out of the new Sandy-HX 3D engine to create Flash and JavaScript 3D applications.

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.

  1. 3D on the web with Java and Irrlicht - Getting Started
  2. 3D on the web with Java and Irrlicht - Lighting
  3. 3D on the web with Java and Irrlicht - Displaying 2D textures
  4. 3D on the web with Java and Irrlicht - Keyboard Input
  5. 3D on the web with Java and Irrlicht - 2D Collision Detection
  6. 3D on the web with Java and Irrlicht - Effects