Flash Key Control Codes Define Class

HP-Keyboard-iconSometimes we need to know or use the Flash key codes in our applications. In AIR runtime, you can find the Keyboard class, which is used to build an interface that can be controlled by a user with a standard keyboard,  the keys which most commonly used to control games are constants represented. And they are only available in the AIR runtime. Following is a key control codes class in Flash, you can simply customize it to cater for your application.

Here is the source code:

package
{
public class KeyControlCode
{
public static var KEY_BACKSPACE:uint=8;
public static var KEY_TAB:uint=9;
public static var KEY_ENTER:uint=13;
public static var KEY_SHIFT:uint=16;
public static var KEY_CONTROL:uint=17;
public static var KEY_PAUSE:uint=19;
public static var KEY_CAPSLOCK:uint=20;
public static var KEY_ESC:uint=27;
public static var KEY_SPACEBAR:uint=32;
public static var KEY_PAGEUP:uint=33;
public static var KEY_PAGEDOWN:uint=34;
public static var KEY_END:uint=35;
public static var KEY_HOME:uint=36;
public static var KEY_LEFT:uint=37;
public static var KEY_UP:uint=38;
public static var KEY_RIGHT:uint=39;
public static var KEY_DOWN:uint=40;
public static var KEY_INSERT:uint=45;
public static var KEY_DELETE:uint=46;
public static var KEY_0:uint=48;
public static var KEY_1:uint=49;
public static var KEY_2:uint=50;
public static var KEY_3:uint=51;
public static var KEY_4:uint=52;
public static var KEY_5:uint=53;
public static var KEY_6:uint=54;
public static var KEY_7:uint=55;
public static var KEY_8:uint=56;
public static var KEY_9:uint=57;
public static var KEY_A:uint=65;
public static var KEY_B:uint=66;
public static var KEY_C:uint=67;
public static var KEY_D:uint=68;
public static var KEY_E:uint=69;
public static var KEY_F:uint=70;
public static var KEY_G:uint=71;
public static var KEY_H:uint=72;
public static var KEY_I:uint=73;
public static var KEY_J:uint=74;
public static var KEY_K:uint=75;
public static var KEY_L:uint=76;
public static var KEY_M:uint=77;
public static var KEY_N:uint=78;
public static var KEY_O:uint=79;
public static var KEY_P:uint=80;
public static var KEY_Q:uint=81;
public static var KEY_R:uint=82;
public static var KEY_S:uint=83;
public static var KEY_T:uint=84;
public static var KEY_U:uint=85;
public static var KEY_V:uint=86;
public static var KEY_W:uint=87;
public static var KEY_X:uint=88;
public static var KEY_Y:uint=89;
public static var KEY_Z:uint=90;
public static var KEY_NUMPAD_0:uint=96;
public static var KEY_NUMPAD_1:uint=97;
public static var KEY_NUMPAD_2:uint=98;
public static var KEY_NUMPAD_3:uint=99;
public static var KEY_NUMPAD_4:uint=100;
public static var KEY_NUMPAD_5:uint=101;
public static var KEY_NUMPAD_6:uint=102;
public static var KEY_NUMPAD_7:uint=103;
public static var KEY_NUMPAD_8:uint=104;
public static var KEY_NUMPAD_9:uint=105;
public static var KEY_NUMPAD_MULTIPLY:uint=106;
public static var KEY_PLUS:uint=107;
public static var KEY_SUBTRACT:uint=109;
public static var KEY_DOT:uint=110;
public static var KEY_DIVISION:uint=111;
public static var KEY_F1:uint=112;
public static var KEY_F2:uint=113;
public static var KEY_F3:uint=114;
public static var KEY_F4:uint=115;
public static var KEY_F5:uint=116;
public static var KEY_F6:uint=117;
public static var KEY_F7:uint=118;
public static var KEY_F8:uint=119;
public static var KEY_F9:uint=120;
public static var KEY_F11:uint=122;
public static var KEY_F12:uint=123;
public static var KEY_F13:uint=124;
public static var KEY_F14:uint=125;
public static var KEY_F15:uint=126;
public static var KEY_NUMLOCK:uint=144;
public static var KEY_SCROLLLOCK:uint=145;
public static var KEY_SEMICOLON:uint=186;
public static var KEY_EQUAL:uint=187;
public static var KEY_COMMA:uint=188;
public static var KEY_MINUS:uint=189;
public static var KEY_PERIOD:uint=190;
public static var KEY_SLASH:uint=191;
public static var KEY_BACKQUOTE:uint=192;
public static var KEY_LEFTBRACKET:uint=219;
public static var KEY_BACKSLASH:uint=220;
public static var KEY_RIGHTBRACKET:uint=221;
public static var KEY_QUOTE:uint=222;
}
}

Of course, you can set up a listener to check for a key press in Flash. Flash will broadcast whenever a key is pressed and which triggers the onKeyDown handler of any listeners set up to listen for Key events. Or you also can refer to Adobe Livedocs Keyboard Keys and Key Code Values, all the keys on a standard keyboard and the corresponding key code values and ASCII key code values that are used to identify the keys in ActionScript be listed there.

key = String.fromCharCode(Key.getAscii()) // which key be pressed

Here is an example of a keyListener and how you can use the ascii keycodes in Flash Actionscript programming:

// key listener..
 keyListener = new Object(); // make key listener object
 // this function gets triggered whenever a key is pressed
 keyListener.onKeyDown = function() {
        var keyCode = Key.getCode(); // get the key code

         if (keyCode == KEY_TAB) { // 9: TAB
                // do something
		     }
        } else if (keyCode == KEY_ENTER) { // 13: Enter
               // do something
        }
 };
Key.addListener(keyListener); // notify Key object about your listener
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

2 Responses to “Flash Key Control Codes Define Class”

  1. [...] Flash就没这么好命了,你只能自己写键盘对应的键值,不过有好人把对应键整理了出来,原地址是在这里:http://ntt.cc/2010/05/03/flash-key-control-codes-define-class.html [...]

  2. Sil says:

    Useful. Thanks!

Leave a Reply