Top Level

Class Key

Object


public class Key
extends Object

Player version: Flash Player 6

The Key class is a top-level class whose methods and properties you can use without a constructor. Use the methods of the Key class to build an interface that can be controlled by a user with a standard keyboard. The properties of the Key class are constants representing the keys most commonly used to control applications, such as Arrow keys, Page Up, and Page Down.

A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.




Property Summary
static BACKSPACE : Number
The key code value for the Backspace key (8).
static CAPSLOCK : Number
The key code value for the Caps Lock key (20).
static CONTROL : Number
The key code value for the Control key (17).
static DELETEKEY : Number
The key code value for the Delete key (46).
static DOWN : Number
The key code value for the Down Arrow key (40).
static END : Number
The key code value for the End key (35).
static ENTER : Number
The key code value for the Enter key (13).
static ESCAPE : Number
The key code value for the Escape key (27).
static HOME : Number
The key code value for the Home key (36).
static INSERT : Number
The key code value for the Insert key (45).
static LEFT : Number
The key code value for the Left Arrow key (37).
static _listeners : Array  [read-only]
A list of references to all listener objects that are registered with the Key object.
static PGDN : Number
The key code value for the Page Down key (34).
static PGUP : Number
The key code value for the Page Up key (33).
static RIGHT : Number
The key code value for the Right Arrow key (39).
static SHIFT : Number
The key code value for the Shift key (16).
static SPACE : Number
The key code value for the Spacebar (32).
static TAB : Number
The key code value for the Tab key (9).
static UP : Number
The key code value for the Up Arrow key (38).

Properties inherited from class Object
__proto__, __resolve, constructor, prototype


Event Summary
onKeyDown = function() {}
Notified when a key is pressed.
onKeyUp = function() {}
Notified when a key is released.


Method Summary
static addListener(listener:Object) : Void
Registers an object to receive onKeyDown and onKeyUp notification.
static getAscii() : Number
Returns the ASCII code of the last key pressed or released.
static getCode() : Number
Returns the key code value of the last key pressed.
static isAccessible() : Boolean
Returns a Boolean value indicating, depending on security restrictions, whether the last key pressed may be accessed by other SWF files.
static isDown(code:Number) : Boolean
Returns true if the key specified in keycode is pressed; false otherwise.
static isToggled(code:Number) : Boolean
Returns true if the Caps Lock or Num Lock key is activated (toggled to an active state); false otherwise.
static removeListener(listener:Object) : Boolean
Removes an object previously registered with Key.addListener().

Methods inherited from class Object
addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch


Property Detail

BACKSPACE Constant

public static BACKSPACE : Number

Player version: Flash Player 5

The key code value for the Backspace key (8).

Example
The following example creates a new listener object and defines a function for onKeyDown. The last line uses addListener() to register the listener with the Key object so that it can receive notification from the key down event.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.BACKSPACE)) {
    trace("you pressed the Backspace key.");
    } else {
    trace("you DIDN'T press the Backspace key.");
    }
};
Key.addListener(keyListener);

When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.


CAPSLOCK Constant

public static CAPSLOCK : Number

Player version: Flash Player 5

The key code value for the Caps Lock key (20).

Example
The following example creates a new listener object and defines a function for onKeyDown. The last line uses addListener() to register the listener with the Key object so that it can receive notification from the key down event.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.CAPSLOCK)) {
    trace("you pressed the Caps Lock key.");
    trace("\tCaps Lock == "+Key.isToggled(Key.CAPSLOCK));
    }
};
Key.addListener(keyListener);

Information is displayed in the Output panel when you press the Caps Lock key. The Output panel displays either true or false, depending on whether the Caps Lock key is activated using the isToggled method. Information is written to the log file when you press the Caps Lock key. The log file writes either true or false, depending on whether the Caps Lock is activated using the isToggled() method.


CONTROL Constant

public static CONTROL : Number

Player version: Flash Player 5

The key code value for the Control key (17).

Example
The following example assigns the keyboard shortcut Control+7 to a button with an instance name of my_btn and makes information about the shortcut available to screen readers (see _accProps). In this example, when you press Control+7 the myOnPress function displays the text hello in the Output panel. In this example, when you press Control+7 the myOnPress function writes the text "hello" to the log file.
function myOnPress() {
    trace("hello");
}
function myOnKeyDown() {
    // 55 is key code for 7
    if (Key.isDown(Key.CONTROL) && Key.getCode() == 55) {
    Selection.setFocus(my_btn);
    my_btn.onPress();
    }
}
var myListener:Object = new Object();
myListener.onKeyDown = myOnKeyDown;
Key.addListener(myListener);
my_btn.onPress = myOnPress;
my_btn._accProps.shortcut = "Ctrl+7";
Accessibility.updateProperties();


DELETEKEY Constant

public static DELETEKEY : Number

Player version: Flash Player 5

The key code value for the Delete key (46).

Example
The following example lets you draw lines with the mouse pointer using the Drawing API and listener objects. Press the Backspace or Delete key to remove the lines that you draw.
this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth());
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
    this.drawing = true;
    canvas_mc.moveTo(_xmouse, _ymouse);
    canvas_mc.lineStyle(3, 0x99CC00, 100);
};
mouseListener.onMouseUp = function() {
    this.drawing = false;
};
mouseListener.onMouseMove = function() {
    if (this.drawing) {
    canvas_mc.lineTo(_xmouse, _ymouse);
    }
    updateAfterEvent();
};
Mouse.addListener(mouseListener);
//
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.DELETEKEY) || Key.isDown(Key.BACKSPACE)) {
    canvas_mc.clear();
    }
};
Key.addListener(keyListener);

When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.


DOWN Constant

public static DOWN : Number

Player version: Flash Player 5

The key code value for the Down Arrow key (40).

Example
The following example moves a movie clip called car_mc a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. Give a sound in the library a linkage identifier of horn_id for this example.
var DISTANCE:Number = 10;
var horn_sound:Sound = new Sound();
horn_sound.attachSound("horn_id");
var keyListener_obj:Object = new Object();
keyListener_obj.onKeyDown = function() {
    switch (Key.getCode()) {
    case Key.SPACE :
    horn_sound.start();
    break;
    case Key.LEFT :
    car_mc._x -= DISTANCE;
    break;
    case Key.UP :
    car_mc._y -= DISTANCE;
    break;
    case Key.RIGHT :
    car_mc._x += DISTANCE;
    break;
    case Key.DOWN :
    car_mc._y += DISTANCE;
    break;
    }
};
Key.addListener(keyListener_obj);


END Constant

public static END : Number

Player version: Flash Player 5

The key code value for the End key (35).

ENTER Constant

public static ENTER : Number

Player version: Flash Player 5

The key code value for the Enter key (13).

Example
The following example moves a movie clip called car_mc a constant distance (10) when you press an arrow key. The car_mc instance stops when you press Enter and deletes the onEnterFrame event.
var DISTANCE:Number = 5;
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    switch (Key.getCode()) {
    case Key.LEFT :
    car_mc.onEnterFrame = function() {
        this._x -= DISTANCE;
    };
    break;
    case Key.UP :
    car_mc.onEnterFrame = function() {
        this._y -= DISTANCE;
    };
    break;
    case Key.RIGHT :
    car_mc.onEnterFrame = function() {
        this._x += DISTANCE;
    };
    break;
    case Key.DOWN :
    car_mc.onEnterFrame = function() {
        this._y += DISTANCE;
    };
    break;
    case Key.ENTER :
    delete car_mc.onEnterFrame;
    break;
    }
};
Key.addListener(keyListener);

When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.


ESCAPE Constant

public static ESCAPE : Number

Player version: Flash Player 5

The key code value for the Escape key (27).

Example
The following example sets a timer. When you press Escape, the Output panel displays information that includes how long it took you to press the key. When you press Escape, the log file writes information that includes how long it took you to press the key.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.ESCAPE)) {
    // Get the current timer, convert the value to seconds and round it to two decimal places.
    var timer:Number = Math.round(getTimer()/10)/100;
    trace("you pressed the Esc key: "+getTimer()+" ms ("+timer+" s)");
    }
};
Key.addListener(keyListener);

When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.


HOME Constant

public static HOME : Number

Player version: Flash Player 5

The key code value for the Home key (36).

Example
The following example attaches a draggable movie clip called car_mc at the x and y coordinates of 0,0. When you press the Home key, car_mc returns to 0,0. Create a movie clip that has a linkage ID car_id, and add the following ActionScript to Frame 1 of the Timeline:
this.attachMovie("car_id", "car_mc", this.getNextHighestDepth(), {_x:0, _y:0});
car_mc.onPress = function() {
    this.startDrag();
};
car_mc.onRelease = function() {
    this.stopDrag();
};
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.HOME)) {
    car_mc._x = 0;
    car_mc._y = 0;
    }
};
Key.addListener(keyListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.


INSERT Constant

public static INSERT : Number

Player version: Flash Player 5

The key code value for the Insert key (45).

Example
The following example creates a new listener object and defines a function for onKeyDown. The last line uses addListener() to register the listener with the Key object so that it can receive notification from the key down event and write information to the log file. The last line uses addListener() to register the listener with the Key object so that it can receive notification from the key down event and display information in the Output panel.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.INSERT)) {
    trace("You pressed the Insert key.");
    }
};
Key.addListener(keyListener);


LEFT Constant

public static LEFT : Number

Player version: Flash Player 5

The key code value for the Left Arrow key (37).

Example
The following example moves a movie clip called car_mc a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. Give a sound in the library a linkage identifier of horn_id for this example.
var DISTANCE:Number = 10;
var horn_sound:Sound = new Sound();
horn_sound.attachSound("horn_id");
var keyListener_obj:Object = new Object();
keyListener_obj.onKeyDown = function() {
    switch (Key.getCode()) {
    case Key.SPACE :
    horn_sound.start();
    break;
    case Key.LEFT :
    car_mc._x -= DISTANCE;
    break;
    case Key.UP :
    car_mc._y -= DISTANCE;
    break;
    case Key.RIGHT :
    car_mc._x += DISTANCE;
    break;
    case Key.DOWN :
    car_mc._y += DISTANCE;
    break;
    }
};
Key.addListener(keyListener_obj);


_listeners Property

public static _listeners : Array  [read-only]

Player version: Flash Player 6

A list of references to all listener objects that are registered with the Key object. This property is intended for internal use, but it may be useful if you want to ascertain the number of listeners currently registered with the Key object. Objects are added to and removed from this array by calls to the addListener() and removelistener() methods.

Example
The following example shows how to use the length property to ascertain the number of listener objects currently registered to the Key object.
    var myListener:Object = new Object();
    myListener.onKeyDown = function () {
    trace ("You pressed a key.");
    }
    Key.addListener(myListener);
    
    trace(Key._listeners.length); // Output: 1 


PGDN Constant

public static PGDN : Number

Player version: Flash Player 5

The key code value for the Page Down key (34).

Example
The following example rotates a movie clip called car_mc when you press the Page Down or Page Up key.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.PGDN)) {
    car_mc._rotation += 5;
    } else if (Key.isDown(Key.PGUP)) {
    car_mc._rotation -= 5;
    }
};
Key.addListener(keyListener);


PGUP Constant

public static PGUP : Number

Player version: Flash Player 5

The key code value for the Page Up key (33).

Example
The following example rotates a movie clip called car_mc when you press the Page Down or Page Up key.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.PGDN)) {
    car_mc._rotation += 5;
    } else if (Key.isDown(Key.PGUP)) {
    car_mc._rotation -= 5;
    }
};
Key.addListener(keyListener);


RIGHT Constant

public static RIGHT : Number

Player version: Flash Player 5

The key code value for the Right Arrow key (39).

Example
The following example moves a movie clip called car_mc a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. For this example, give a sound in the library a linkage identifier of horn_id.
var DISTANCE:Number = 10;
var horn_sound:Sound = new Sound();
horn_sound.attachSound("horn_id");
var keyListener_obj:Object = new Object();
keyListener_obj.onKeyDown = function() {
    switch (Key.getCode()) {
    case Key.SPACE :
    horn_sound.start();
    break;
    case Key.LEFT :
    car_mc._x -= DISTANCE;
    break;
    case Key.UP :
    car_mc._y -= DISTANCE;
    break;
    case Key.RIGHT :
    car_mc._x += DISTANCE;
    break;
    case Key.DOWN :
    car_mc._y += DISTANCE;
    break;
    }
};
Key.addListener(keyListener_obj);


SHIFT Constant

public static SHIFT : Number

Player version: Flash Player 5

The key code value for the Shift key (16).

Example
The following example scales car_mc when you press Shift.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.SHIFT)) {
    car_mc._xscale = 2;
    car_mc._yscale = 2;
    } else if (Key.isDown(Key.CONTROL)) {
    car_mc._xscale /= 2;
    car_mc._yscale /= 2;
    }
};
Key.addListener(keyListener);


SPACE Constant

public static SPACE : Number

Player version: Flash Player 5

The key code value for the Spacebar (32).

Example
The following example moves a movie clip called car_mc a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. For this example, give a sound in the library a linkage identifier of horn_id.
var DISTANCE:Number = 10;
var horn_sound:Sound = new Sound();
horn_sound.attachSound("horn_id");
var keyListener_obj:Object = new Object();
keyListener_obj.onKeyDown = function() {
    switch (Key.getCode()) {
    case Key.SPACE :
    horn_sound.start();
    break;
    case Key.LEFT :
    car_mc._x -= DISTANCE;
    break;
    case Key.UP :
    car_mc._y -= DISTANCE;
    break;
    case Key.RIGHT :
    car_mc._x += DISTANCE;
    break;
    case Key.DOWN :
    car_mc._y += DISTANCE;
    break;
    }
};
Key.addListener(keyListener_obj);


TAB Constant

public static TAB : Number

Player version: Flash Player 5

The key code value for the Tab key (9).

Example
The following example creates a text field, and displays the date in the text field when you press Tab.
this.createTextField("date_txt", this.getNextHighestDepth(), 0, 0, 100, 22);
date_txt.autoSize = true;
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.TAB)) {
    var today_date:Date = new Date();
    date_txt.text = today_date.toString();
    }
};
Key.addListener(keyListener);

When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.


UP Constant

public static UP : Number

Player version: Flash Player 5

The key code value for the Up Arrow key (38).

Example
The following example moves a movie clip called car_mc a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. For this example, give a sound in the library a linkage identifier of horn_id.
var DISTANCE:Number = 10;
var horn_sound:Sound = new Sound();
horn_sound.attachSound("horn_id");
var keyListener_obj:Object = new Object();
keyListener_obj.onKeyDown = function() {
    switch (Key.getCode()) {
    case Key.SPACE :
    horn_sound.start();
    break;
    case Key.LEFT :
    car_mc._x -= DISTANCE;
    break;
    case Key.UP :
    car_mc._y -= DISTANCE;
    break;
    case Key.RIGHT :
    car_mc._x += DISTANCE;
    break;
    case Key.DOWN :
    car_mc._y += DISTANCE;
    break;
    }
};
Key.addListener(keyListener_obj);



Event Detail

onKeyDown Event Listener

public onKeyDown = function() {}

Player version: Flash Player 6

Notified when a key is pressed. To use onKeyDown, you must create a listener object. You can then define a function for onKeyDown and use addListener() to register the listener with the Key object, as shown in the following example:
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    trace("DOWN -> Code: "+Key.getCode()+"\tACSII: "+Key.getAscii()+"\tKey: "+chr(Key.getAscii()));
};
keyListener.onKeyUp = function() {
    trace("UP -> Code: "+Key.getCode()+"\tACSII: "+Key.getAscii()+"\tKey: "+chr(Key.getAscii()));
};
Key.addListener(keyListener);

Listeners enable different pieces of code to cooperate because multiple listeners can receive notification about a single event.

A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.

See also
Key.addListener()

onKeyUp Event Listener

public onKeyUp = function() {}

Player version: Flash Player 6

Notified when a key is released. To use onKeyUp, you must create a listener object. You can then define a function for onKeyUp and use addListener() to register the listener with the Key object, as shown in the following example:
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    trace("DOWN -> Code: "+Key.getCode()+"\tACSII: "+Key.getAscii()+"\tKey: "+chr(Key.getAscii()));
};
keyListener.onKeyUp = function() {
    trace("UP -> Code: "+Key.getCode()+"\tACSII: "+Key.getAscii()+"\tKey: "+chr(Key.getAscii()));
};
Key.addListener(keyListener);

Listeners enable different pieces of code to cooperate because multiple listeners can receive notification about a single event.

A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.

See also
Key.addListener()


Method Detail

addListener Method

public static addListener(listener:Object) : Void

Player version: Flash Player 6

Registers an object to receive onKeyDown and onKeyUp notification. When a key is pressed or released, regardless of the input focus, all listening objects registered with addListener() have either their onKeyDown method or onKeyUp method invoked. Multiple objects can listen for keyboard notifications. If the listener newListener is already registered, no change occurs.

A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.

Parameters
listener:Object — An object with methods onKeyDown and onKeyUp.

Example
The following example creates a new listener object and defines a function for onKeyDown and onKeyUp. The last line uses addListener() to register the listener with the Key object so that it can receive notification from the key down and key up events.
var myListener:Object = new Object();
myListener.onKeyDown = function () {
    trace ("You pressed a key.");
}
myListener.onKeyUp = function () {
    trace ("You released a key.");
}
Key.addListener(myListener);

The following example assigns the keyboard shortcut Control+7 to a button with an instance name of my_btn and makes information about the shortcut available to screen readers (see _accProps). In this example, when you press Control+7 the myOnPress function displays the text hello in the Output panel. In this example, when you press Control+7 the myOnPress function writes the text "hello" to the log file.

function myOnPress() {
    trace("hello");
}
function myOnKeyDown() {
    // 55 is key code for 7
    if (Key.isDown(Key.CONTROL) && Key.getCode() == 55) {
    Selection.setFocus(my_btn);
    my_btn.onPress();
    }
}
var myListener:Object = new Object();
myListener.onKeyDown = myOnKeyDown;
Key.addListener(myListener);
my_btn.onPress = myOnPress;
my_btn._accProps.shortcut = "Ctrl+7";
Accessibility.updateProperties();

See also
Key.getCode(), Key.isDown(), Key.onKeyDown, Key.onKeyUp, Key.removeListener()

getAscii Method

public static getAscii() : Number

Player version: Flash Player 5

Returns the ASCII code of the last key pressed or released. The ASCII values returned are English keyboard values. For example, if you press Shift+2, Key.getAscii() returns @ on a Japanese keyboard, which is the same as it does on an English keyboard.

A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.

Returns
Number — The ASCII value of the last key pressed. This method returns 0 if no key was pressed or released, or if the key code is not accessible for security reasons.

Example
The following example calls the getAscii() method any time a key is pressed. The example creates a listener object named keyListener and defines a function that responds to the onKeyDown event by calling Key.getAscii().The keyListener object is then registered to the Key object, which broadcasts the onKeyDown message whenever a key is pressed while the SWF file plays.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    trace("The ASCII code for the last key typed is: "+Key.getAscii());
};
Key.addListener(keyListener);

When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.

The following example adds a call to Key.getAscii() to show how the two methods differ. The main difference is that Key.getAscii() differentiates between uppercase and lowercase letters, and Key.getCode() does not.

var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    trace("For the last key typed:");
    trace("\tThe Key code is: "+Key.getCode());
    trace("\tThe ASCII value is: "+Key.getAscii());
    trace("");
};
Key.addListener(keyListener);

When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.

See also
Key.isAccessible()

getCode Method

public static getCode() : Number

Player version: Flash Player 5

Returns the key code value of the last key pressed.

Note: The Flash Lite implementation of this method returns a string or a number, depending on the key code passed in by the platform. The only valid key codes are the standard key codes accepted by this class and the special key codes listed as properties of the ExtendedKey class.

A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.

Returns
Number — The key code of the last key pressed. This method returns 0 if no key was pressed or released, or if the key code is not accessible for security reasons.

Example
The following example calls the getCode() method any time a key is pressed. The example creates a listener object named keyListener and defines a function that responds to the onKeyDown event by calling Key.getCode(). The keyListener object is then registered to the Key object, which broadcasts the onKeyDown message whenever a key is pressed while the SWF file plays.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    // compare return value of getCode() to constant
    if (Key.getCode() == Key.ENTER) {
        trace ("Virtual key code: "+Key.getCode()+" (ENTER key)");
    }   
    else {
        trace("Virtual key code: "+Key.getCode());
    }
};
Key.addListener(keyListener);

When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.

The following example adds a call to Key.getAscii() to show how the two methods differ. The main difference is that Key.getAscii() differentiates between uppercase and lowercase letters, and Key.getCode() does not.

var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    trace("For the last key typed:");
    trace("\tThe Key code is: "+Key.getCode());
    trace("\tThe ASCII value is: "+Key.getAscii());
    trace("");
};
Key.addListener(keyListener);

When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.

See also
Key.getAscii, Key.isAccessible()

isAccessible Method

public static isAccessible() : Boolean

Player version: Flash Player 8

Returns a Boolean value indicating, depending on security restrictions, whether the last key pressed may be accessed by other SWF files. By default code from a SWF file in one domain may not access a keystroke generated from a SWF file in another domain. For more information on cross-domain security, see "Understanding Security" in Learning ActionScript 2.0 in Flash.

Returns
Boolean — The value true if the last key pressed may be accessed. If access is not permitted, this method returns false.

isDown Method

public static isDown(code:Number) : Boolean

Player version: Flash Player 5

Returns true if the key specified in keycode is pressed; false otherwise. On the Macintosh, the key code values for the Caps Lock and Num Lock keys are identical.

A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.

Parameters
code:Number — The key code value assigned to a specific key or a Key class property associated with a specific key.

Returns
Boolean — The value true if the key specified in keycode is pressed; false otherwise.

Example
The following script lets the user control a movie clip's (car_mc) location:
car_mc.onEnterFrame = function() {
  if (Key.isDown(Key.RIGHT)) {
    this._x += 10;
  } else if (Key.isDown(Key.LEFT)) {
    this._x -= 10;
  }
};


isToggled Method

public static isToggled(code:Number) : Boolean

Player version: Flash Player 5

Returns true if the Caps Lock or Num Lock key is activated (toggled to an active state); false otherwise. Although the term toggled usually means that something is switched between two options, the method Key.isToggled() will only return true if the key is toggled to an active state. On the Macintosh, the key code values for the Caps Lock and Num Lock keys are identical.

A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.

Parameters
code:Number — The key code for the Caps Lock key (20) or the Num Lock key (144).

Returns
Boolean — The value true if the Caps Lock or Num Lock key is activated (toggled to an active state); false otherwise.

Example
The following example calls the isToggled() method any time a key is pressed and executes a trace statement any time the Caps Lock key is toggled to an active state. The example creates a listener object named keyListener and defines a function that responds to the onKeyDown event by calling Key.isToggled(). The keyListener object is then registered to the Key object, which broadcasts the onKeyDown message whenever a key is pressed while the SWF file plays.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    if (Key.isDown(Key.CAPSLOCK)) {
    trace("you pressed the Caps Lock key.");
    trace("\tCaps Lock == "+Key.isToggled(Key.CAPSLOCK));
    }
};
Key.addListener(keyListener);

Information displays in the Output panel when you press the Caps Lock key. Information is written to the log file when you press the Caps Lock key. The Output panel displays either true or false, depending on whether the Caps Lock is activated using the isToggled method. The log file writes either true or false, depending on whether the Caps Lock is activated using the isToggled method.

The following example creates two text fields that update when the Caps Lock and Num Lock keys are toggled. Each text field displays true when the key is activated, and false when the key is deactivated.

this.createTextField("capsLock_txt", this.getNextHighestDepth(), 0, 0, 100, 22);
capsLock_txt.autoSize = true;
capsLock_txt.html = true;
this.createTextField("numLock_txt", this.getNextHighestDepth(), 0, 22, 100, 22);
numLock_txt.autoSize = true;
numLock_txt.html = true;
//
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    capsLock_txt.htmlText = "<b>Caps Lock:</b> "+Key.isToggled(Key.CAPSLOCK);
    numLock_txt.htmlText = "<b>Num Lock:</b> "+Key.isToggled(144);
};
Key.addListener(keyListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components DepthManager class instead of the MovieClip.getNextHighestDepth() method.


removeListener Method

public static removeListener(listener:Object) : Boolean

Player version: Flash Player 6

Removes an object previously registered with Key.addListener().

Parameters
listener:Object — An object.

Returns
Boolean — If the listener was successfully removed, the method returns true. If the listener was not successfully removed (for example, because the listener was not on the Key object's listener list), the method returns false.

Example
The following example moves a movie clip called car_mc using the Left and Right arrow keys. The listener is removed when you press Escape, and car_mc no longer moves.
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    switch (Key.getCode()) {
    case Key.LEFT :
    car_mc._x -= 10;
    break;
    case Key.RIGHT :
    car_mc._x += 10;
    break;
    case Key.ESCAPE :
    Key.removeListener(keyListener);
    }
};
Key.addListener(keyListener);