Capturing keypresses

You can use the global on() handler to intercept the built-in behavior of keypresses in Flash Player, as shown in the following example:

/* When you press the Left or Right Arrow key, the movie clip to which the handler is attached changes transparency. */
on (keyPress "<Left>") {
    this._alpha -= 10;
}
on (keyPress "<Right>") {
    this._alpha += 10;
}

Make sure that you select Control > Disable Keyboard Shortcuts, or certain keys with built-in behavior won't be overridden when you use Control > Test Movie to test the application. See the keyPress parameter of on handler in the ActionScript 2.0 Language Reference.

You can use the methods of the built-in Key class to detect the last key that the user pressed. The Key class does not require a constructor function; to use its methods, you call the methods on the class, as shown in the following example:

Key.getCode();

You can obtain either virtual key codes or American Standard Code for Information Interchange (ASCII) values of keypresses:

A virtual key code is assigned to every physical key on a keyboard. For example, the left arrow key has the virtual key code 37. By using a virtual key code, you ensure that your SWF file's controls are the same on every keyboard, regardless of language or platform.

ASCII values are assigned to the first 127 characters in every character set. ASCII values provide information about a character on the screen. For example, the letter "A" and the letter "a" have different ASCII values.

To decide which keys to use and determine their virtual key codes, use one of the following approaches:

The key code of the desired key appears in the Output panel.

A common place to use Key class methods is within an event handler. In the following SWF file, the user can move the car using the arrow keys. The Key.isDown() method indicates whether the key being pressed is the right, left, up, or down arrow. The event listener, Key.onKeyDown, determines the Key.isDown(keyCode) value from the if statements. Depending on the value, the handler instructs Flash Player to update the position of the car and to show the direction.

The following example shows how to capture keypresses to move a movie clip up, down, left, or right on the Stage, depending on which corresponding arrow key (up, down, left, or right) is pressed. Also, a text field shows the name of the pressed key.

To create a keyboard-activated movie clip:

  1. On the Stage, create a movie clip that can move in response to keyboard arrow activity.

    In this example, the movie clip instance name is car_mc.

  2. Select Frame 1 in the Timeline; then select Window > Actions to open the Actions panel if it is not already visible.
  3. To set how far the car moves across the screen with each keypress, define a distance variable and set its value to 10:
    var distance:Number = 10;
    
  4. Add the following ActionScript code to the Actions panel below the existing code:
    this.createTextField("display_txt", 999, 0, 0, 100, 20);
    
  5. To create the event handler for the car movie clip that checks which arrow key (left, right, up, or down) is currently pressed, add the following code to the Actions panel:
    var keyListener:Object = new Object();
    keyListener.onKeyDown = function() {
    };
    Key.addListener(keyListener);
    
  6. To check if the Left Arrow key is pressed and to move the car movie clip accordingly, add code to the body of the onEnterFrame event handler.

    Your code should look like the following example (new code is in boldface):

    var distance:Number = 10;
    this.createTextField("display_txt", 999, 0, 0, 100, 20);
    var keyListener:Object = new Object();
    keyListener.onKeyDown = function() {
        if (Key.isDown(Key.LEFT)) {
            car_mc._x = Math.max(car_mc._x - distance, 0);
            display_txt.text = "Left";
        }
    };
    Key.addListener(keyListener);
    

    If the Left Arrow key is pressed, the car's _x property is set to the current _x value minus distance or the value 0, whichever is greater. Therefore, the value of the _x property can never be less than 0. Also, the word Left should appear in the SWF file.

  7. Use similar code to check if the Right, Up, or Down Arrow key is being pressed.

    Your complete code should look like the following example (new code is in boldface):

    var distance:Number = 10;
    this.createTextField("display_txt", 999, 0, 0, 100, 20);
    var keyListener:Object = new Object();
    keyListener.onKeyDown = function() {
        if (Key.isDown(Key.LEFT)) {
            car_mc._x = Math.max(car_mc._x - distance, 0);
            display_txt.text = "Left";
        } else if (Key.isDown(Key.RIGHT)) {
            car_mc._x = Math.min(car_mc._x + distance, Stage.width - car_mc._width);
            display_txt.text = "Right";
        } else if (Key.isDown(Key.UP)) {
            car_mc._y = Math.max(car_mc._y - distance, 0);
            display_txt.text = "Up";
        } else if (Key.isDown(Key.DOWN)) {
            car_mc._y = Math.min(car_mc._y + distance, Stage.height - car_mc._height);
            display_txt.text = "Down";
        }
    };
    Key.addListener(keyListener);
    
  8. Select Control > Test Movie to test the file.

For more information about the methods of the Key class, see Key in the ActionScript 2.0 Language Reference.