Button.onKeyDown
Availability
Flash Player 6.
Usage
my_btn.onKeyDown = function() : Void {
// your statements here
}
Parameters
None.
Returns
Nothing.
Description
Event handler; invoked when a button has keyboard focus and a key is pressed. The
event handler is invoked with no parameters. You can use the
methods to determine which key was pressed.
You must define a function that executes when the event handler is invoked.
Example
In the following example, a function that sends text to the Output panel is defined for the
handler. Create a button called
onKeyDown
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
When you press keys, they are displayed in the Output panel.
144
Chapter 2: ActionScript Language Reference
on the Stage, and enter the following
my_btn
instance) and start pressing keys on your keyboard.
my_btn
Key.getAscii()
and
Key.getCode()
onKeyDown
Need help?
Do you have a question about the FLASH MX 2004-ACTIONSCRIPT LANGUAGE and is the answer not in the manual?