User Guide

28 Creating Interactivity and Navigation
Writing an event listener
Event listeners let an object, called a listener object, receive events broadcast by another
object, called a broadcaster object. The broadcaster object registers the listener object to
receive events generated by the broadcaster. For more information, see “Using event listeners
in Learning ActionScript 2.x in Flash.
A easy way to handle key press events is to create a key listener object that defines an
onKeyDown or onKeyUp function, and then register that object with the Key.addListener()
method. The following example code defines a key listener that responds when the user
presses the right navigation key on their device:
var myListener:Object = new Object();
myListener.onKeyDown = function() {
if (Key.getCode() == Key.RIGHT) {
trace("You pressed the right arrow key");
}
}
Key.addListener(myListener);
For more examples of using key listeners, see the remaining topics in this chapter.
Using a key listener to handle keypress events (Flash
Professional only)
The following procedures demonstrate how to use a key listener to handle keypress events in a
simple application. The application uses the four navigation keys to move a movie clip around
the Stage.
To use an event listener to handle keypress events:
1. Create a new document from the Flash Lite 2.0-Generic device template that you created
in “Creating a Flash Lite document template (Flash Professional only)” in Getting Started
with Flash Lite 2.x, and save it as keylistener.fla.
2. Select the layer in the Timeline named Content.
3. Using the Oval tool, create an oval or circle on the Stage and convert it to a movie clip.
4. With the new movie clip selected, in the Property inspector, type circle in the Instance
Name text box.
5. In the Timeline, select the first frame in Layer 1.