User Guide

Key.addListener() 343
Key.addListener()
Availability
Flash Player 6.
Usage
Key.addListener (newListener:Object) : Void
Parameters
newListener
An object with methods onKeyDown and onKeyUp.
Returns
Nothing.
Description
Method; 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.
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.
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();