Package | Top Level |
Class | public class Key |
Inheritance | Key Object |
Player version: | Flash Player 6 |
A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.
Property | ||
---|---|---|
_listeners : Array
[static][read-only]A list of references to all listener objects that are registered with the Key object.
|
Properties inherited from class Object | |
---|---|
__proto__, __resolve, constructor, prototype |
Method | ||
---|---|---|
addListener(listener:Object):Void
[static]Registers an object to receive
onKeyDown and onKeyUp notification. |
||
[static]Returns the ASCII code of the last key pressed or released.
|
||
[static]Returns the key code value of the last key pressed.
|
||
[static]Returns a Boolean value indicating, depending on security restrictions, whether the last key pressed may be accessed by other SWF files.
|
||
[static]Returns
true if the key specified in keycode is pressed; false otherwise. |
||
[static]Returns
true if the Caps Lock or Num Lock key is activated (toggled to an active state); false otherwise. |
||
[static]Removes an object previously registered with
Key.addListener() . |
Methods inherited from class Object | |
---|---|
addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch |
Event | Summary | Defined by | ||
---|---|---|---|---|
onKeyDown = function() {}
| Notified when a key is pressed. | Key | ||
onKeyUp = function() {}
| Notified when a key is released. | Key |
Constant | ||
---|---|---|
BACKSPACE : Number
[static]The key code value for the Backspace key (8).
|
||
CAPSLOCK : Number
[static]The key code value for the Caps Lock key (20).
|
||
CONTROL : Number
[static]The key code value for the Control key (17).
|
||
DELETEKEY : Number
[static]The key code value for the Delete key (46).
|
||
DOWN : Number
[static]The key code value for the Down Arrow key (40).
|
||
END : Number
[static]The key code value for the End key (35).
|
||
ENTER : Number
[static]The key code value for the Enter key (13).
|
||
ESCAPE : Number
[static]The key code value for the Escape key (27).
|
||
HOME : Number
[static]The key code value for the Home key (36).
|
||
INSERT : Number
[static]The key code value for the Insert key (45).
|
||
LEFT : Number
[static]The key code value for the Left Arrow key (37).
|
||
PGDN : Number
[static]The key code value for the Page Down key (34).
|
||
PGUP : Number
[static]The key code value for the Page Up key (33).
|
||
RIGHT : Number
[static]The key code value for the Right Arrow key (39).
|
||
SHIFT : Number
[static]The key code value for the Shift key (16).
|
||
SPACE : Number
[static]The key code value for the Spacebar (32).
|
||
TAB : Number
[static]The key code value for the Tab key (9).
|
||
UP : Number
[static]The key code value for the Up Arrow key (38).
|
Properties inherited from class Object | |
---|---|
__proto__, __resolve, constructor, prototype |
_listeners | property |
_listeners:Array
[read-only]
Player version: | Flash Player 6 |
A list of references to all listener objects that are registered with the Key object. This property is intended for internal use, but it may be useful if you want to ascertain the number of listeners currently registered with the Key object. Objects are added to and removed from this array by calls to the addListener()
and removelistener()
methods.
public static function get _listeners():Array
length
property to ascertain the number of listener objects currently registered to the Key object. var myListener:Object = new Object(); myListener.onKeyDown = function () { trace ("You pressed a key."); } Key.addListener(myListener); trace(Key._listeners.length); // Output: 1
addListener | () | method |
public static function addListener(listener:Object):Void
Player version: | Flash Player 6 |
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.
Parameterslistener:Object — An object with methods onKeyDown and onKeyUp . |
See also
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. In this example, when you press Control+7 the myOnPress
function writes the text "hello" to the log file.
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();
getAscii | () | method |
public static function getAscii():Number
Player version: | Flash Player 5 |
Returns the ASCII code of the last key pressed or released. The ASCII values returned are English keyboard values. For example, if you press Shift+2, Key.getAscii()
returns @
on a Japanese keyboard, which is the same as it does on an English keyboard.
A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.
ReturnsNumber —
The ASCII value of the last key pressed. This method returns 0 if no key was pressed or released, or if the key code is not accessible for security reasons.
|
See also
getAscii()
method any time a key is pressed. The example creates a listener object named keyListener
and defines a function that responds to the onKeyDown
event by calling Key.getAscii()
.The keyListener
object is then registered to the Key
object, which broadcasts the onKeyDown
message whenever a key is pressed while the SWF file plays. var keyListener:Object = new Object(); keyListener.onKeyDown = function() { trace("The ASCII code for the last key typed is: "+Key.getAscii()); }; Key.addListener(keyListener);
When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.
The following example adds a call to Key.getAscii()
to show how the two methods differ. The main difference is that Key.getAscii()
differentiates between uppercase and lowercase letters, and Key.getCode()
does not.
var keyListener:Object = new Object(); keyListener.onKeyDown = function() { trace("For the last key typed:"); trace("\tThe Key code is: "+Key.getCode()); trace("\tThe ASCII value is: "+Key.getAscii()); trace(""); }; Key.addListener(keyListener);
When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.
getCode | () | method |
public static function getCode():Number
Player version: | Flash Player 5 |
Returns the key code value of the last key pressed.
Note: The Flash Lite implementation of this method returns a string or a number, depending on the key code passed in by the platform. The only valid key codes are the standard key codes accepted by this class and the special key codes listed as properties of the ExtendedKey
class.
A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.
ReturnsNumber —
The key code of the last key pressed. This method returns 0 if no key was pressed or released, or if the key code is not accessible for security reasons.
|
See also
getCode()
method any time a key is pressed. The example creates a listener object named keyListener
and defines a function that responds to the onKeyDown
event by calling Key.getCode()
. The keyListener
object is then registered to the Key
object, which broadcasts the onKeyDown
message whenever a key is pressed while the SWF file plays. var keyListener:Object = new Object(); keyListener.onKeyDown = function() { // compare return value of getCode() to constant if (Key.getCode() == Key.ENTER) { trace ("Virtual key code: "+Key.getCode()+" (ENTER key)"); } else { trace("Virtual key code: "+Key.getCode()); } }; Key.addListener(keyListener);
When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.
The following example adds a call to Key.
getAscii()
to show how the two methods differ. The main difference is that Key.
getAscii()
differentiates between uppercase and lowercase letters, and Key.getCode()
does not.
var keyListener:Object = new Object(); keyListener.onKeyDown = function() { trace("For the last key typed:"); trace("\tThe Key code is: "+Key.getCode()); trace("\tThe ASCII value is: "+Key.getAscii()); trace(""); }; Key.addListener(keyListener);
When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.
isAccessible | () | method |
public static function isAccessible():Boolean
Player version: | Flash Player 8 |
Returns a Boolean value indicating, depending on security restrictions, whether the last key pressed may be accessed by other SWF files. By default code from a SWF file in one domain may not access a keystroke generated from a SWF file in another domain. For more information on cross-domain security, see "Understanding Security" in Learning ActionScript 2.0 in Flash.
ReturnsBoolean —
The value true if the last key pressed may be accessed. If access is not permitted, this method returns false .
|
isDown | () | method |
public static function isDown(code:Number):Boolean
Player version: | Flash Player 5 |
Returns true
if the key specified in keycode
is pressed; false
otherwise. On the Macintosh, the key code values for the Caps Lock and Num Lock keys are identical.
A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.
Parameterscode:Number — The key code value assigned to a specific key or a Key class property associated with a specific key. |
Boolean —
The value true if the key specified in keycode is pressed; false otherwise.
|
car_mc
) location: car_mc.onEnterFrame = function() {
if (Key.isDown(Key.RIGHT)) {
this._x += 10;
} else if (Key.isDown(Key.LEFT)) {
this._x -= 10;
}
};
isToggled | () | method |
public static function isToggled(code:Number):Boolean
Player version: | Flash Player 5 |
Returns true
if the Caps Lock or Num Lock key is activated (toggled to an active state); false
otherwise. Although the term toggled usually means that something is switched between two options, the method Key.isToggled() will only return true
if the key is toggled to an active state. On the Macintosh, the key code values for the Caps Lock and Num Lock keys are identical.
A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.
Parameterscode:Number — The key code for the Caps Lock key (20) or the Num Lock key (144). |
Boolean —
The value true if the Caps Lock or Num Lock key is activated (toggled to an active state); false otherwise.
|
isToggled()
method any time a key is pressed and executes a trace statement any time the Caps Lock key is toggled to an active state. The example creates a listener object named keyListener
and defines a function that responds to the onKeyDown
event by calling Key.isToggled()
. The keyListener
object is then registered to the Key
object, which broadcasts the onKeyDown
message whenever a key is pressed while the SWF file plays. var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.CAPSLOCK)) { trace("you pressed the Caps Lock key."); trace("\tCaps Lock == "+Key.isToggled(Key.CAPSLOCK)); } }; Key.addListener(keyListener);
Information displays in the Output panel when you press the Caps Lock key. Information is written to the log file when you press the Caps Lock key. The Output panel displays either true
or false
, depending on whether the Caps Lock is activated using the isToggled method. The log file writes either true
or false
, depending on whether the Caps Lock is activated using the isToggled method.
The following example creates two text fields that update when the Caps Lock and Num Lock keys are toggled. Each text field displays true when the key is activated, and false when the key is deactivated.
this.createTextField("capsLock_txt", this.getNextHighestDepth(), 0, 0, 100, 22); capsLock_txt.autoSize = true; capsLock_txt.html = true; this.createTextField("numLock_txt", this.getNextHighestDepth(), 0, 22, 100, 22); numLock_txt.autoSize = true; numLock_txt.html = true; // var keyListener:Object = new Object(); keyListener.onKeyDown = function() { capsLock_txt.htmlText = "<b>Caps Lock:</b> "+Key.isToggled(Key.CAPSLOCK); numLock_txt.htmlText = "<b>Num Lock:</b> "+Key.isToggled(144); }; Key.addListener(keyListener);
The MovieClip.getNextHighestDepth()
method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components' DepthManager class instead of the MovieClip.getNextHighestDepth()
method.
removeListener | () | method |
public static function removeListener(listener:Object):Boolean
Player version: | Flash Player 6 |
Removes an object previously registered with Key.addListener()
.
listener:Object — An object. |
Boolean —
If the listener was successfully removed, the method returns true . If the listener was not successfully removed (for example, because the listener was not on the Key object's listener list), the method returns false .
|
car_mc
using the Left and Right arrow keys. The listener is removed when you press Escape, and car_mc
no longer moves. var keyListener:Object = new Object(); keyListener.onKeyDown = function() { switch (Key.getCode()) { case Key.LEFT : car_mc._x -= 10; break; case Key.RIGHT : car_mc._x += 10; break; case Key.ESCAPE : Key.removeListener(keyListener); } }; Key.addListener(keyListener);
onKeyDown | event listener |
public onKeyDown = function() {}
Player version: | Flash Player 6 |
Notified when a key is pressed. To use onKeyDown
, you must create a listener object. You can then define a function for onKeyDown
and use addListener()
to register the listener with the Key object, as shown in the following example:
var keyListener:Object = new Object(); keyListener.onKeyDown = function() { trace("DOWN -> Code: "+Key.getCode()+"\tASCII: "+Key.getAscii()+"\tKey: "+chr(Key.getAscii())); }; keyListener.onKeyUp = function() { trace("UP -> Code: "+Key.getCode()+"\tASCII: "+Key.getAscii()+"\tKey: "+chr(Key.getAscii())); }; Key.addListener(keyListener);
Listeners enable different pieces of code to cooperate because multiple listeners can receive notification about a single event.
A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.
See also
onKeyUp | event listener |
public onKeyUp = function() {}
Player version: | Flash Player 6 |
Notified when a key is released. To use onKeyUp
, you must create a listener object. You can then define a function for onKeyUp
and use addListener()
to register the listener with the Key object, as shown in the following example:
var keyListener:Object = new Object(); keyListener.onKeyDown = function() { trace("DOWN -> Code: "+Key.getCode()+"\tASCII: "+Key.getAscii()+"\tKey: "+chr(Key.getAscii())); }; keyListener.onKeyUp = function() { trace("UP -> Code: "+Key.getCode()+"\tASCII: "+Key.getAscii()+"\tKey: "+chr(Key.getAscii())); }; Key.addListener(keyListener);
Listeners enable different pieces of code to cooperate because multiple listeners can receive notification about a single event.
A Flash application can only monitor keyboard events that occur within its focus. A Flash application cannot detect keyboard events in another application.
See also
BACKSPACE | constant |
public static var BACKSPACE:Number
Player version: | Flash Player 5 |
The key code value for the Backspace key (8).
onKeyDown
. The last line uses addListener()
to register the listener with the Key object so that it can receive notification from the key down event. var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.BACKSPACE)) { trace("you pressed the Backspace key."); } else { trace("you DIDN'T press the Backspace key."); } }; Key.addListener(keyListener);
When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.
CAPSLOCK | constant |
public static var CAPSLOCK:Number
Player version: | Flash Player 5 |
The key code value for the Caps Lock key (20).
onKeyDown
. The last line uses addListener()
to register the listener with the Key object so that it can receive notification from the key down event. var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.CAPSLOCK)) { trace("you pressed the Caps Lock key."); trace("\tCaps Lock == "+Key.isToggled(Key.CAPSLOCK)); } }; Key.addListener(keyListener);
Information is displayed in the Output panel when you press the Caps Lock key. The Output panel displays either true
or false
, depending on whether the Caps Lock key is activated using the isToggled method. Information is written to the log file when you press the Caps Lock key. The log file writes either true
or false
, depending on whether the Caps Lock is activated using the isToggled()
method.
CONTROL | constant |
public static var CONTROL:Number
Player version: | Flash Player 5 |
The key code value for the Control key (17).
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. In this example, when you press Control+7 the myOnPress
function writes the text "hello" to the log file. 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();
DELETEKEY | constant |
public static var DELETEKEY:Number
Player version: | Flash Player 5 |
The key code value for the Delete key (46).
this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth()); var mouseListener:Object = new Object(); mouseListener.onMouseDown = function() { this.drawing = true; canvas_mc.moveTo(_xmouse, _ymouse); canvas_mc.lineStyle(3, 0x99CC00, 100); }; mouseListener.onMouseUp = function() { this.drawing = false; }; mouseListener.onMouseMove = function() { if (this.drawing) { canvas_mc.lineTo(_xmouse, _ymouse); } updateAfterEvent(); }; Mouse.addListener(mouseListener); // var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.DELETEKEY) || Key.isDown(Key.BACKSPACE)) { canvas_mc.clear(); } }; Key.addListener(keyListener);
When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.
The MovieClip.getNextHighestDepth()
method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components' DepthManager class instead of the MovieClip.getNextHighestDepth()
method.
DOWN | constant |
public static var DOWN:Number
Player version: | Flash Player 5 |
The key code value for the Down Arrow key (40).
car_mc
a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. Give a sound in the library a linkage identifier of horn_id
for this example. var DISTANCE:Number = 10; var horn_sound:Sound = new Sound(); horn_sound.attachSound("horn_id"); var keyListener_obj:Object = new Object(); keyListener_obj.onKeyDown = function() { switch (Key.getCode()) { case Key.SPACE : horn_sound.start(); break; case Key.LEFT : car_mc._x -= DISTANCE; break; case Key.UP : car_mc._y -= DISTANCE; break; case Key.RIGHT : car_mc._x += DISTANCE; break; case Key.DOWN : car_mc._y += DISTANCE; break; } }; Key.addListener(keyListener_obj);
END | constant |
public static var END:Number
Player version: | Flash Player 5 |
The key code value for the End key (35).
ENTER | constant |
public static var ENTER:Number
Player version: | Flash Player 5 |
The key code value for the Enter key (13).
car_mc
a constant distance (10) when you press an arrow key. The car_mc
instance stops when you press Enter and deletes the onEnterFrame
event. var DISTANCE:Number = 5; var keyListener:Object = new Object(); keyListener.onKeyDown = function() { switch (Key.getCode()) { case Key.LEFT : car_mc.onEnterFrame = function() { this._x -= DISTANCE; }; break; case Key.UP : car_mc.onEnterFrame = function() { this._y -= DISTANCE; }; break; case Key.RIGHT : car_mc.onEnterFrame = function() { this._x += DISTANCE; }; break; case Key.DOWN : car_mc.onEnterFrame = function() { this._y += DISTANCE; }; break; case Key.ENTER : delete car_mc.onEnterFrame; break; } }; Key.addListener(keyListener);
When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.
ESCAPE | constant |
public static var ESCAPE:Number
Player version: | Flash Player 5 |
The key code value for the Escape key (27).
var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.ESCAPE)) { // Get the current timer, convert the value to seconds and round it to two decimal places. var timer:Number = Math.round(getTimer()/10)/100; trace("you pressed the Esc key: "+getTimer()+" ms ("+timer+" s)"); } }; Key.addListener(keyListener);
When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.
HOME | constant |
public static var HOME:Number
Player version: | Flash Player 5 |
The key code value for the Home key (36).
car_mc
at the x and y coordinates of 0,0. When you press the Home key, car_mc
returns to 0,0. Create a movie clip that has a linkage ID car_id
, and add the following ActionScript to Frame 1 of the Timeline: this.attachMovie("car_id", "car_mc", this.getNextHighestDepth(), {_x:0, _y:0}); car_mc.onPress = function() { this.startDrag(); }; car_mc.onRelease = function() { this.stopDrag(); }; var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.HOME)) { car_mc._x = 0; car_mc._y = 0; } }; Key.addListener(keyListener);
The MovieClip.getNextHighestDepth()
method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components' DepthManager class instead of the MovieClip.getNextHighestDepth()
method.
INSERT | constant |
public static var INSERT:Number
Player version: | Flash Player 5 |
The key code value for the Insert key (45).
onKeyDown
. The last line uses addListener()
to register the listener with the Key object so that it can receive notification from the key down event and write information to the log file. The last line uses addListener()
to register the listener with the Key object so that it can receive notification from the key down event and display information in the Output panel. var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.INSERT)) { trace("You pressed the Insert key."); } }; Key.addListener(keyListener);
LEFT | constant |
public static var LEFT:Number
Player version: | Flash Player 5 |
The key code value for the Left Arrow key (37).
car_mc
a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. Give a sound in the library a linkage identifier of horn_id
for this example. var DISTANCE:Number = 10; var horn_sound:Sound = new Sound(); horn_sound.attachSound("horn_id"); var keyListener_obj:Object = new Object(); keyListener_obj.onKeyDown = function() { switch (Key.getCode()) { case Key.SPACE : horn_sound.start(); break; case Key.LEFT : car_mc._x -= DISTANCE; break; case Key.UP : car_mc._y -= DISTANCE; break; case Key.RIGHT : car_mc._x += DISTANCE; break; case Key.DOWN : car_mc._y += DISTANCE; break; } }; Key.addListener(keyListener_obj);
PGDN | constant |
public static var PGDN:Number
Player version: | Flash Player 5 |
The key code value for the Page Down key (34).
car_mc
when you press the Page Down or Page Up key. var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.PGDN)) { car_mc._rotation += 5; } else if (Key.isDown(Key.PGUP)) { car_mc._rotation -= 5; } }; Key.addListener(keyListener);
PGUP | constant |
public static var PGUP:Number
Player version: | Flash Player 5 |
The key code value for the Page Up key (33).
car_mc
when you press the Page Down or Page Up key. var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.PGDN)) { car_mc._rotation += 5; } else if (Key.isDown(Key.PGUP)) { car_mc._rotation -= 5; } }; Key.addListener(keyListener);
RIGHT | constant |
public static var RIGHT:Number
Player version: | Flash Player 5 |
The key code value for the Right Arrow key (39).
car_mc
a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. For this example, give a sound in the library a linkage identifier of horn_id
. var DISTANCE:Number = 10; var horn_sound:Sound = new Sound(); horn_sound.attachSound("horn_id"); var keyListener_obj:Object = new Object(); keyListener_obj.onKeyDown = function() { switch (Key.getCode()) { case Key.SPACE : horn_sound.start(); break; case Key.LEFT : car_mc._x -= DISTANCE; break; case Key.UP : car_mc._y -= DISTANCE; break; case Key.RIGHT : car_mc._x += DISTANCE; break; case Key.DOWN : car_mc._y += DISTANCE; break; } }; Key.addListener(keyListener_obj);
SHIFT | constant |
public static var SHIFT:Number
Player version: | Flash Player 5 |
The key code value for the Shift key (16).
car_mc
when you press Shift. var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.SHIFT)) { car_mc._xscale = 2; car_mc._yscale = 2; } else if (Key.isDown(Key.CONTROL)) { car_mc._xscale /= 2; car_mc._yscale /= 2; } }; Key.addListener(keyListener);
SPACE | constant |
public static var SPACE:Number
Player version: | Flash Player 5 |
The key code value for the Spacebar (32).
car_mc
a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. For this example, give a sound in the library a linkage identifier of horn_id
. var DISTANCE:Number = 10; var horn_sound:Sound = new Sound(); horn_sound.attachSound("horn_id"); var keyListener_obj:Object = new Object(); keyListener_obj.onKeyDown = function() { switch (Key.getCode()) { case Key.SPACE : horn_sound.start(); break; case Key.LEFT : car_mc._x -= DISTANCE; break; case Key.UP : car_mc._y -= DISTANCE; break; case Key.RIGHT : car_mc._x += DISTANCE; break; case Key.DOWN : car_mc._y += DISTANCE; break; } }; Key.addListener(keyListener_obj);
TAB | constant |
public static var TAB:Number
Player version: | Flash Player 5 |
The key code value for the Tab key (9).
this.createTextField("date_txt", this.getNextHighestDepth(), 0, 0, 100, 22); date_txt.autoSize = true; var keyListener:Object = new Object(); keyListener.onKeyDown = function() { if (Key.isDown(Key.TAB)) { var today_date:Date = new Date(); date_txt.text = today_date.toString(); } }; Key.addListener(keyListener);
When you use this example, be sure to select Control > Disable Keyboard Shortcuts in the test environment.
The MovieClip.getNextHighestDepth()
method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components' DepthManager class instead of the MovieClip.getNextHighestDepth()
method.
UP | constant |
public static var UP:Number
Player version: | Flash Player 5 |
The key code value for the Up Arrow key (38).
car_mc
a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. For this example, give a sound in the library a linkage identifier of horn_id
. var DISTANCE:Number = 10; var horn_sound:Sound = new Sound(); horn_sound.attachSound("horn_id"); var keyListener_obj:Object = new Object(); keyListener_obj.onKeyDown = function() { switch (Key.getCode()) { case Key.SPACE : horn_sound.start(); break; case Key.LEFT : car_mc._x -= DISTANCE; break; case Key.UP : car_mc._y -= DISTANCE; break; case Key.RIGHT : car_mc._x += DISTANCE; break; case Key.DOWN : car_mc._y += DISTANCE; break; } }; Key.addListener(keyListener_obj);