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