View comments | RSS feed

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;
Key.addListener(myListener);
my_btn.onPress = myOnPress;
my_btn._accProps.shortcut = "Ctrl+7";
Accessibility.updateProperties();

See also

getCode (Key.getCode method), isDown (Key.isDown method), onKeyDown (Key.onKeyDown event listener), onKeyUp (Key.onKeyUp event listener), removeListener (Key.removeListener method)


Flash CS3


Comments


j.t.g. said on Nov 19, 2007 at 5:12 AM :
If you ammend the example to test for ctrl-z (eg Key.getCode() == 90), it fails when viewed in IE (or any browser hosting the flash active-x control as far as I can tell).

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001749.html