Key.addListener()

Availability

Flash Player 6.

Usage

Key.addListener (newListener)

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.

myListener = 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 myButton, 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 your file, you would create a function that does something more meaningful.

function myOnPress() {
  trace( "hello" );
}

function myOnKeyDown() {
  if (Key.isDown(Key.CONTROL) && Key.getCode() == 55) // 55 is key code for 7
  {
    Selection.setFocus( myButton );
    myButton.onPress();
  }
}

var myListener = new Object();
myListener.onKeyDown = myOnKeyDown;
Key.addListener( myListener );

myButton.onPress = myOnPress;
myButton._accProps.shortcut = "Ctrl+F"
Accessibility.updateProperties();

See also

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