Developing Flash Lite 2.x and 3.0 Applications

Handle keypress events through a key listener

  1. Create a new document from the Flash Lite 2.0 template that you created in "Creating a Flash Lite document template" in Getting Started with Flash Lite 2.x and 3.0, 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.
  6. Open the Actions panel (Window > Actions), and enter the following code:
    var myListener:Object = new Object();
    myListener.onKeyDown = function() {
        if (Key.getCode() == Key.LEFT) {
            circle._x -= 10;
        } else if (Key.getCode() == Key.RIGHT) {
            circle._x += 10;
        } else if (Key.getCode() == Key.UP) {
            circle._y -= 10;
        } else if (Key.getCode() == Key.DOWN) {
            circle._y += 10;
        }
    };
    Key.addListener(myListener);
    
    
  7. Test the application by selecting Control > Test Movie.

    Press the four navigation keys on the emulator's keypad (or the corresponding arrow keys on your keyboard) to make the circle move around the Stage.