View comments | RSS feed

onKeyDown (Button.onKeyDown handler)

onKeyDown = function() {}

Invoked when a button has keyboard focus and a key is pressed. The onKeyDown event handler is invoked with no parameters. You can use the Key.getAscii() and Key.getCode() methods to determine which key was pressed. You must define a function that executes when the event handler is invoked.

Availability: ActionScript 1.0; Flash Player 6

Example

In the following example, a function that sends text to the Output panel is defined for the onKeyDown handler. Create a button called my_btn on the Stage, and enter the following ActionScript in a frame on the Timeline:

my_btn.onKeyDown = function() {
    trace("onKeyDown: "+this._name+" (Key: "+getKeyPressed()+")");
};
function getKeyPressed():String {
    var theKey:String;
    switch (Key.getAscii()) {
    case Key.BACKSPACE :
        theKey = "BACKSPACE";
        break;
    case Key.SPACE :
        theKey = "SPACE";
        break;
    default :
        theKey = chr(Key.getAscii());
    }
    return theKey;
}

Select Control > Test Movie to test the SWF file. Make sure you select Control > Disable Keyboard Shortcuts in the test environment. Then press the Tab key until the button has focus (a yellow rectangle appears around the my_btn instance) and start pressing keys on your keyboard. When you press keys, they are displayed in the Output panel.

See also

onKeyUp (Button.onKeyUp handler), getAscii (Key.getAscii method), getCode (Key.getCode method)


Flash CS3


Comments


xzone9 productions said on Aug 3, 2008 at 4:24 PM :
The example is not performing as expected. No trace actions are revealed when previewing. Not sure why.
Joe ... Ward said on Aug 11, 2008 at 10:55 AM :
When running the example from Flash, the Flash interface intercepts most of the keystrokes, so you will only see traces for those keys that aren't used as shortcuts in the Flash UI. To allow all the keystrokes to make it to your movie, choose the "Control>Disable Keyboard Shortcuts" command from the Control menu of the Test Movie window.

 

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/00001450.html