Key


Object   |   +-Key public class Key extends Object

The Key class is a top-level class whose methods and properties you can use without a constructor. Use the methods of the Key class to build an interface that can be controlled by a user with a standard keyboard. The properties of the Key class are constants representing the keys most commonly used to control applications, such as Arrow keys, Page Up, and Page Down.

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

Property summary

Modifiers

Property

Description

static

BACKSPACE:Number

The key code value for the Backspace key (8).

static

CAPSLOCK:Number

The key code value for the Caps Lock key (20).

static

CONTROL:Number

The key code value for the Control key (17).

static

DELETEKEY:Number

The key code value for the Delete key (46).

static

DOWN:Number

The key code value for the Down Arrow key (40).

static

END:Number

The key code value for the End key (35).

static

ENTER:Number

The key code value for the Enter key (13).

static

ESCAPE:Number

The key code value for the Escape key (27).

static

HOME:Number

The key code value for the Home key (36).

static

INSERT:Number

The key code value for the Insert key (45).

static

LEFT:Number

The key code value for the Left Arrow key (37).

static

_listeners:Array

[read-only]

A list of references to all listener objects that are registered with the Key object.

static

PGDN:Number

The key code value for the Page Down key (34).

static

PGUP:Number

The key code value for the Page Up key (33).

static

RIGHT:Number

The key code value for the Right Arrow key (39).

static

SHIFT:Number

The key code value for the Shift key (16).

static

SPACE:Number

The key code value for the Spacebar (32).

static

TAB:Number

The key code value for the Tab key (9).

static

UP:Number

The key code value for the Up Arrow key (38).


Properties inherited from class Object

constructor (Object.constructor property), __proto__ (Object.__proto__ property), prototype (Object.prototype property), __resolve (Object.__resolve property)


Event summary

Event

Description

onKeyDown = function() {}

Notified when a key is pressed.

onKeyUp = function() {}

Notified when a key is released.


Method summary

Modifiers

Signature

Description

static

addListener(listener:Object) : Void

Registers an object to receive onKeyDown and onKeyUp notification.

static

getAscii() : Number

Returns the ASCII code of the last key pressed or released.

static

getCode() : Number

Returns the key code value of the last key pressed.

static

isAccessible() : Boolean

Returns a Boolean value indicating, depending on security restrictions, whether the last key pressed may be accessed by other SWF files.

static

isDown(code:Number) : Boolean

Returns true if the key specified in keycode is pressed; false otherwise.

static

isToggled(code:Number) : Boolean

Returns true if the Caps Lock or Num Lock key is activated (toggled to an active state); false otherwise.

static

removeListener(listener:Object) : Boolean

Removes an object previously registered with Key.addListener().


Methods inherited from class Object

addProperty (Object.addProperty method), hasOwnProperty (Object.hasOwnProperty method), isPropertyEnumerable (Object.isPropertyEnumerable method), isPrototypeOf (Object.isPrototypeOf method), registerClass (Object.registerClass method), toString (Object.toString method), unwatch (Object.unwatch method), valueOf (Object.valueOf method), watch (Object.watch method)


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)

BACKSPACE (Key.BACKSPACE property)

public static BACKSPACE : Number

The key code value for the Backspace key (8).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example creates a new listener object and defines a function for 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 (Key.CAPSLOCK property)

public static CAPSLOCK : Number

The key code value for the Caps Lock key (20).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example creates a new listener object and defines a function for 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.

CONTROL (Key.CONTROL property)

public static CONTROL : Number

The key code value for the Control key (17).

Availability: ActionScript 1.0; Flash Player 5

Example

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();

DELETEKEY (Key.DELETEKEY property)

public static DELETEKEY : Number

The key code value for the Delete key (46).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example lets you draw lines with the mouse pointer using the Drawing API and listener objects. Press the Backspace or Delete key to remove the lines that you draw.

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 (Key.DOWN property)

public static DOWN : Number

The key code value for the Down Arrow key (40).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example moves a movie clip called 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 (Key.END property)

public static END : Number

The key code value for the End key (35).

Availability: ActionScript 1.0; Flash Player 5

ENTER (Key.ENTER property)

public static ENTER : Number

The key code value for the Enter key (13).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example moves a movie clip called 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 (Key.ESCAPE property)

public static ESCAPE : Number

The key code value for the Escape key (27).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example sets a timer. When you press Escape, the Output panel displays information that includes how long it took you to press the key.

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.

getAscii (Key.getAscii method)

public static getAscii() : Number

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.

Availability: ActionScript 1.0; Flash Player 5

Returns

Number - 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.

Example

The following example calls the 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.

See also

isAccessible (Key.isAccessible method)

getCode (Key.getCode method)

public static getCode() : Number

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.

Availability: ActionScript 1.0; Flash Player 5

Returns

Number - 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.

Example

The following example calls the 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.

See also

getAscii (Key.getAscii method), isAccessible (Key.isAccessible method)

HOME (Key.HOME property)

public static HOME : Number

The key code value for the Home key (36).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example attaches a draggable movie clip called 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 (Key.INSERT property)

public static INSERT : Number

The key code value for the Insert key (45).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example creates a new listener object and defines a function for 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 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);

isAccessible (Key.isAccessible method)

public static isAccessible() : Boolean

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.

Availability: ActionScript 1.0; Flash Player 8

Returns

Boolean - The value TRue if the last key pressed may be accessed. If access is not permitted, this method returns false.

isDown (Key.isDown method)

public static isDown(code:Number) : Boolean

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.

Availability: ActionScript 1.0; Flash Player 5

Parameters

code:Number - The key code value assigned to a specific key or a Key class property associated with a specific key.

Returns

Boolean - The value true if the key specified in keycode is pressed; false otherwise.

Example

The following script lets the user control a movie clip's (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 (Key.isToggled method)

public static isToggled(code:Number) : Boolean

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.

Availability: ActionScript 1.0; Flash Player 5

Parameters

code:Number - The key code for the Caps Lock key (20) or the Num Lock key (144).

Returns

Boolean - The value true if the Caps Lock or Num Lock key is activated (toggled to an active state); false otherwise.

Example

The following example calls the 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. The Output panel displays 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.

LEFT (Key.LEFT property)

public static LEFT : Number

The key code value for the Left Arrow key (37).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example moves a movie clip called 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);

_listeners (Key._listeners property)

public static _listeners : Array [read-only]

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.

Availability: ActionScript 1.0; Flash Player 6

Example

The following example shows how to use the 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

onKeyDown (Key.onKeyDown event listener)

onKeyDown = function() {}

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()+"\tACSII: "+Key.getAscii()+"\tKey:   "+chr(Key.getAscii())); }; keyListener.onKeyUp = function() {   trace("UP -> Code: "+Key.getCode()+"\tACSII: "+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.

Availability: ActionScript 1.0; Flash Player 6

See also

addListener (Key.addListener method)

onKeyUp (Key.onKeyUp event listener)

onKeyUp = function() {}

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()+"\tACSII: "+Key.getAscii()+"\tKey:   "+chr(Key.getAscii())); }; keyListener.onKeyUp = function() {   trace("UP -> Code: "+Key.getCode()+"\tACSII: "+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.

Availability: ActionScript 1.0; Flash Player 6

See also

addListener (Key.addListener method)

PGDN (Key.PGDN property)

public static PGDN : Number

The key code value for the Page Down key (34).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example rotates a movie clip called 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 (Key.PGUP property)

public static PGUP : Number

The key code value for the Page Up key (33).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example rotates a movie clip called 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);

removeListener (Key.removeListener method)

public static removeListener(listener:Object) : Boolean

Removes an object previously registered with Key.addListener().

Availability: ActionScript 1.0; Flash Player 6

Parameters

listener:Object - An object.

Returns

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.

Example

The following example moves a movie clip called 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);

RIGHT (Key.RIGHT property)

public static RIGHT : Number

The key code value for the Right Arrow key (39).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example moves a movie clip called 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 (Key.SHIFT property)

public static SHIFT : Number

The key code value for the Shift key (16).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example scales 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 (Key.SPACE property)

public static SPACE : Number

The key code value for the Spacebar (32).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example moves a movie clip called 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 (Key.TAB property)

public static TAB : Number

The key code value for the Tab key (9).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example creates a text field, and displays the date in the text field when you press Tab.

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 (Key.UP property)

public static UP : Number

The key code value for the Up Arrow key (38).

Availability: ActionScript 1.0; Flash Player 5

Example

The following example moves a movie clip called 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);



ActionScript 2.0 Language Reference for Macromedia Flash 8
ActionScript 2.0 Language Reference for Macromedia Flash 8
ISBN: 0321384040
EAN: 2147483647
Year: 2004
Pages: 113

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net