Key Object | Flash 5 |
determine the state of keys on the keyboard |
Table 18-8 lists the properties of the Key object.
Equivalent keycode |
Property |
Equivalent keycode |
||
---|---|---|---|---|
8 |
45 |
|||
20 |
37 |
|||
17 |
34 |
|||
46 |
33 |
|||
40 |
39 |
|||
35 |
16 |
|||
13 |
32 |
|||
27 |
9 |
|||
36 |
38 |
Registers an object to receive onKeyUp( ) and onKeyDown( ) events.
Returns the ASCII value of the last key pressed.
Returns the keycode of the last key pressed.
Checks if a specific key is currently depressed.
Checks if the Num Lock, Caps Lock, or Scroll Lock keys are activated.
Cancels event notices for the specified listener.
Occurs when a key is depressed.
Occurs when a key is released.
The Key object is used to determine which keys are currently depressed and which key was last released. We can use it to build interfaces controlled by the keyboard, such as a game with a spaceship moved via the arrow keys.
Because not all keyboards are identical, keyboard-controlled interfaces can sometimes be tricky to create. By choosing our scripting tools correctly, however, we can ensure that all users have the same experience.
There are two general approaches to detecting keyboard commands:
We can check if a key is currently depressed via the isDown( ) method. This is recommended for cases in which the keyboard state needs constant polling (i.e., checking repeatedly), such as in video games. To poll the keyboard, use isDown( ) to check the keyboard state within a MovieClip.onEnterFrame( ) handler or during an interval created via setInterval( ).
We can check which key was last released using the getCode( ) and getAscii( ) methods. This is recommended for typical keyboard-driven interfaces in which specific operations are performed in response to individual keys being pressed. We ordinarily use these methods from an onKeyDown( ) or onKeyUp( ) event listener, or a movie clip's onClipEvent(keyDown) or onClipEvent(keyUp) handler. There is no need to constantly check (i.e., poll) for the last key pressed. In fact, doing so leads to erroneously repeating some operation even if a key wasn't pressed repeatedly. That is, you should generally check getCode( ) and getAscii( ) from a listener or clip event only to guarantee the code is executed once and only once for each keystroke.
|
The so-called Windows virtual keycode (or simply keycode) returned by getCode( ) and required by isDown( ) is a number representing the physical keys on the keyboard, not the symbols on those keys and not their ASCII values. By using the keycode, we can identify keys even when a movie is running on different operating systems or when two keyboards use different languages or have different symbol layouts.
On most keyboards, the keycodes of the keys A to Z are the same as the code points (65-90) for the equivalent uppercase Latin 1 letters (also the same as their ASCII codes). The keycodes of the keys 0 to 9 are, likewise, the same as the Latin 1 and ASCII values for those numbers (48-57). The key codes of other keys do not necessarily match Latin 1 code points, although some, such as the Tab key, match the ASCII values. However, many of the nonletter and nonnumber keycodes are available as properties of Key, as shown in Table 18-8. For example, we don't have to remember that the up arrow uses keycode 38; we simply use the Key.UP property. The following code checks whether the up arrow key is currently depressed:
if (Key.isDown(Key.UP)) { trace("The up arrow is being pressed"); }
When working with a keycode that is not a letter or a number and is not available as a property of Key�such as those of the function keys (F1, F2, etc.)�it's safest to create a quick test movie to check the keycode of the desired key, as follows:
var keyTester = new Object(); keyTester.onKeyDown = function () { trace(Key.getCode()); } Key.addListener(keyTester);
When testing keys in Test Movie mode, be sure to disable keyboard shortcuts via Control Disable Keyboard Shortcuts.
The keycodes are listed in Appendix B.
The onKeyDown( ) and onKeyUp( ) listener events were added to the Key object in Flash 6. They are preferred over MovieClip.onKeyDown( ) and MovieClip.onKeyUp( ) and the Flash 5-style onClipEvent(keyDown) and onClipEvent(keyUp) handlers in most cases.
However, the following caveats apply:
In Flash 6, the updateAfterEvent( ) function, which refreshes the screen between frames, can be used only with MovieClip mouse and key events and setInterval( ). Hence, to create instant-refresh keyboard applications, use onClipEvent(keyDown) and onClipEvent(keyUp), which are directly analogous to Key.onKeyDown( ) and Key.onKeyUp( ).
To capture keystrokes while a specific movie clip has input focus only, use the MovieClip.onKeyDown( ) and MovieClip.onKeyUp( ) handlers. See those entries for details.
MovieClip.onKeyDown( ), MovieClip.onKeyUp( ); Chapter 10, Appendix B