Methods of the Key class may be used to find out which key a user pressed during a movie. In Flash MX and higher, you can set up a listener to check for a key press. A listener is an object which listens for a particular event broadcast by Flash (in this case, a keypress event, which Flash will broadcast whenever a key is pressed (repeatedly, if the key is held down) and which triggers the onKeyDown handler of any listeners set up to listen for Key events):
keyListener = new Object();
keyListener.onKeyDown = function() {
x = Key.getAscii();
trace("You hit: " + x);
};
Key.addListener(keyListener);
Whenever a key is pressed, its ascii code will be available via the Key.getAscii method. For letter and number keys, a display/value that is more intuitive can be gotten by using
x = String.fromCharCode(Key.getAscii())
If you have a movieclip on stage, like square_mc above, the code below will check for activity on the arrow keys and move square_mc accordingly. Notice that the Flash movie must have focus (must be clicked on somewhere, eg) before it can respond to key events in a browser. Also notice that no onEnterFrame is needed for continuous checking, because Keypress events are automatically checked for and broadcast by Flash. The onKeyDown function will be carried out for whatever key was last pressed (and/or held down):
var LBOUND:Number = 40;
var TBOUND:Number = 30;
var RBOUND:Number = 280 - square_mc._width;
var BBOUND:Number = 170 - square_mc._height;
var INCR:Number = 7; // number of pixels to move each frame
keylistener = new Object();
keylistener.onKeyDown = function() {
if (Key.getCode() == Key.RIGHT)
if (square_mc._x < (RBOUND-INCR)) square_mc._x += INCR;
else square_mc._x = RBOUND;
if (Key.getCode() == Key.LEFT)
if (square_mc._x > (LBOUND+INCR)) square_mc._x -= INCR;
else square_mc._x = LBOUND;
if (Key.getCode() == Key.UP)
if (square_mc._y > (TBOUND+INCR)) square_mc._y -= INCR;
else square_mc._y = TBOUND;
if (Key.getCode() == Key.DOWN)
if (square_mc._y < (BBOUND-INCR)) square_mc._y += INCR;
else square_mc._y = BBOUND;
};
Key.addListener(keylistener);
Using an onEnterFrame checker and looking for Key.isDown instead can produce a much more responsive result (including response to multiple keys). Here is the same movie running at 21fps (same as above) using an onEnterFrame loop to check and respond (code follows):
var LBOUND:Number = 40;
var TBOUND:Number = 30;
var RBOUND:Number = 280 - square_mc._width;
var BBOUND:Number = 170 - square_mc._height;
var INCR:Number = 7; // number of pixels to move each frame
onEnterFrame = function() {
if ((Key.isDown(Key.LEFT)))
if (square_mc._x > (LBOUND+INCR)) square_mc._x -= INCR;
else square_mc._x = LBOUND;
if (Key.isDown(Key.UP))
if (square_mc._y > (TBOUND+INCR)) square_mc._y -= INCR;
else square_mc._y = TBOUND;
if (Key.isDown(Key.RIGHT))
if (square_mc._x < (RBOUND-INCR)) square_mc._x += INCR;
else square_mc._x = RBOUND;
if (Key.isDown(Key.DOWN))
if (square_mc._y < (BBOUND-INCR)) square_mc._y += INCR;
else square_mc._y = BBOUND;
};
You can use Key.isDown to check for the Enter key (or any other specified key) and have the behave as though another control was clicked. Eg, if you have a button (a movieclip named go_mc) and want the same thing to happen when users press Enter as when they click the button, you can include this code:
keyListener = new Object();
keyListener.onKeyDown=function(){
if(Key.isDown(Key.ENTER)){
go_mc.onRelease();
}
}
Key.addListener(keyListener);
To disable that, use:
Key.removeListener(keyListener);
To test the above from the Flash IDE, you'll need to do Control, Test Movie then choose Control again and check Disable Keyboard Shortcuts.
As shown above, the getCode method of the Key class returns a code associated with a particular key on the keyboard. Here is a quick 'n dirty table which shows the values returned for keys on my American keyboard for Windows. I'd be glad to post similar tables for other keyboards if anyone wishes to send one.
(It's easiest to create if you use code like
keyListener = new Object();
keyListener.onKeyDown = function() {
trace("<tr><td>" + Key.getAscii() + "</td><td>" +
String.fromCharCode(Key.getAscii()) +
"</td><td>" + Key.getCode() + "</td></tr>");
};
Key.addListener(keyListener);
and then go pound each key, cut and paste the output window contents, and modify as necessary.)
Ascii = Key.getAscii()
| Ascii() | String | Code |
| 27 | .(esc) | 27 |
| 8 | .(backspace) | 8 |
| 0 | (capslock) | 20 |
| 0 | (shift) | 16 |
| (alt) | 18 | |
| 0 | (ctrl) | 17 |
| 13 | (enter) | 13 |
| 32 | (space) | 32 |
| function keys | ||
| 0 | (F1) | 112 |
| 0 | (F2) | 113 |
| 0 | (F3) | 114 |
| 0 | (F4) | 115 |
| 0 | (F5) | 116 |
| 0 | (F6) | 117 |
| 0 | (F7) | 118 |
| 0 | (F8) | 119 |
| 0 | (F9) | 120 |
| 0 | (F10) | 121 |
| 0 | (F11) | 122 |
| 0 | (F12) | 123 |
| letters and other main keybd w/shift key on | ||
| 126 | ~ | 192 |
| 33 | ! | 49 |
| 64 | @ | 50 |
| 35 | # | 51 |
| 36 | $ | 52 |
| 37 | % | 53 |
| 94 | ^ | 54 |
| 38 | & | 55 |
| 42 | * | 56 |
| 40 | ( | 57 |
| 41 | ) | 48 |
| 95 | _ | 189 |
| 43 | + | 187 |
| 124 | | | 220 |
| 81 | Q | 81 |
| 87 | W | 87 |
| 69 | E | 69 |
| 82 | R | 82 |
| 84 | T | 84 |
| 89 | Y | 89 |
| 85 | U | 85 |
| 73 | I | 73 |
| 79 | O | 79 |
| 80 | P | 80 |
| 123 | { | 219 |
| 125 | } | 221 |
| 65 | A | 65 |
| 83 | S | 83 |
| 68 | D | 68 |
| 70 | F | 70 |
| 71 | G | 71 |
| 72 | H | 72 |
| 74 | J | 74 |
| 75 | K | 75 |
| 76 | L | 76 |
| 58 | : | 186 |
| 34 | " | 222 |
| 90 | Z | 90 |
| 88 | X | 88 |
| 67 | C | 67 |
| 86 | V | 86 |
| 66 | B | 66 |
| 78 | N | 78 |
| 77 | M | 77 |
| 60 | < | 188 |
| 62 | > | 190 |
| 63 | ? | 191 |
| letters and other main keybd, no shift | ||
| 96 | ` | 192 |
| 49 | 1 | 49 |
| 50 | 2 | 50 |
| 51 | 3 | 51 |
| 52 | 4 | 52 |
| 53 | 5 | 53 |
| 54 | 6 | 54 |
| 55 | 7 | 55 |
| 56 | 8 | 56 |
| 57 | 9 | 57 |
| 48 | 0 | 48 |
| 45 | - | 189 |
| 61 | = | 187 |
| 92 | \ | 220 |
| 113 | q | 81 |
| 119 | w | 87 |
| 101 | e | 69 |
| 114 | r | 82 |
| 116 | t | 84 |
| 121 | y | 89 |
| 117 | u | 85 |
| 105 | i | 73 |
| 111 | o | 79 |
| 112 | p | 80 |
| 91 | [ | 219 |
| 93 | ] | 221 |
| 97 | a | 65 |
| 115 | s | 83 |
| 100 | d | 68 |
| 102 | f | 70 |
| 103 | g | 71 |
| 104 | h | 72 |
| 106 | j | 74 |
| 107 | k | 75 |
| 108 | l | 76 |
| 59 | ; | 186 |
| 39 | ' | 222 |
| 122 | z | 90 |
| 120 | x | 88 |
| 99 | c | 67 |
| 118 | v | 86 |
| 98 | b | 66 |
| 110 | n | 78 |
| 109 | m | 77 |
| 44 | , | 188 |
| 46 | . | 190 |
| 47 | / | 191 |
| keypad keys w/numlock on | ||
| 47 | / | 111 |
| 42 | * | 106 |
| 45 | - | 109 |
| 55 | 7 | 103 |
| 56 | 8 | 104 |
| 57 | 9 | 105 |
| 52 | 4 | 100 |
| 53 | 5 | 101 |
| 54 | 6 | 102 |
| 49 | 1 | 97 |
| 50 | 2 | 98 |
| 51 | 3 | 99 |
| 48 | 0 | 96 |
| 46 | . | 110 |
| 13 | 13 | |
| 43 | + | 107 |
| keypad keys w/numlock off | ||
| 47 | / | 111 |
| 42 | * | 106 |
| 45 | - | 109 |
| 0 | (Home) | 36 |
| 0 | (up arrow) | 38 |
| 0 | (PgUp) | 33 |
| 0 | (left arrow) | 37 |
| 0 | 12 | |
| 0 | (right arrow) | 39 |
| 0 | (End) | 35 |
| 0 | (down arrow) | 40 |
| 0 | (PgDown) | 34 |
| 0 | (Ins) | 45 |
| 127 | (Del) | 46 |
| 13 | (Enter) | 13 |
| 43 | + | 107 |
| middlekeys | ||
| 0 | (ScrollLock) | 145 |
| 0 | (Pause) | 19 |
| 0 | (Ins) | 45 |
| 0 | (Home) | 36 |
| 0 | (PageUp) | 33 |
| 127 | (Delete) | 46 |
| 0 | (End) | 35 |
| 0 | (PageDown) | 34 |
| arrowkeys | ||
| 0 | (left) | 37 |
| 0 | (up) | 38 |
| 0 | (down) | 40 |
| 0 | (right) | 39 |
Discussed on this page:
finding which key was pressed, getcode, getascii, keylistener, onKeyDown versus onEnterFrame check, make Enter key like button press, table of keycodes