User Guide

672 ActionScript classes
addListener (Key.addListener method)
public static addListener(listener:Object) : Void
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.
Availability: ActionScript 1.0; Flash Player 6
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.
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;