Pin Me

3D on the web with Java and Irrlicht - Keyboard Input

written by: Matthew Casperson•edited by: Linda Richter•updated: 10/12/2009

Learn how to create fully fledged 3D applications that integrate seamlessly with a web browser using Java, WebStart and Irrlich with this comprehensive tutorial series. This tutorial shows you how to read the keyboard input.

  • slide 1 of 2

    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.

  • slide 2 of 2

    Related Article

    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.