Developing Flash Lite 2.x and 3.0 Applications

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 keypress 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 the device:

var myListener:Object = new Object();
myListener.onKeyDown = function() {
    if (Key.getCode() == Key.RIGHT) {
        trace("You pressed the right arrow key");
    }
}
Key.addListener(myListener);