• Stick Nodes - 100% Complete Go animate some stickfigures. Google Play links: [free] or [pro]. App Store links: [free] or [pro].
Oil Spill Escape
Stick Nodes

Get it from: App Store | Google Play | SlideMe | Amazon | Opera

AS3 – Get Actual Keyboard Key From KeyCode

Comments Off on AS3 – Get Actual Keyboard Key From KeyCode

This is a little something I was having trouble with just now.  I thought there was some built-in functionality for this somewhere, but apparently there is not.  Also Google searches trying to find an existing solution turned up nothing, so I’m putting it here for anyone who may need this in their projects.

This simply returns the actual key value given a certain keyCode.  (For example, pass in 32 and get “Spacebar” in return.)  There is a built-in function, String.fromCharCode(), but I found this only works with the letter / number keys and not “special” keys like escape, shift, control, and most importantly – the arrow keys.

Create the Dictionary object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var _keyCodeDictionary:Dictionary = new Dictionary();
_keyCodeDictionary[8] = "Backspace";
_keyCodeDictionary[9] = "Tab";
_keyCodeDictionary[13] = "Enter";
_keyCodeDictionary[16] = "Shift";
_keyCodeDictionary[17] = "Control";
_keyCodeDictionary[20] = "Caps Lock";
_keyCodeDictionary[27] = "Esc";
_keyCodeDictionary[32] = "Spacebar";
_keyCodeDictionary[33] = "Page Up";
_keyCodeDictionary[34] = "Page Down";
_keyCodeDictionary[35] = "End";
_keyCodeDictionary[36] = "Home";
_keyCodeDictionary[37] = "Left Arrow";
_keyCodeDictionary[38] = "Up Arrow";
_keyCodeDictionary[39] = "Right Arrow";
_keyCodeDictionary[40] = "Down Arrow";
_keyCodeDictionary[45] = "Insert";
_keyCodeDictionary[46] = "Delete";
_keyCodeDictionary[144] = "Num Lock";
_keyCodeDictionary[145] = "Scroll Lock";
_keyCodeDictionary[19] = "Pause/Break";
_keyCodeDictionary[65] = "A";
_keyCodeDictionary[66] = "B";
_keyCodeDictionary[67] = "C";
_keyCodeDictionary[68] = "D";
_keyCodeDictionary[69] = "E";
_keyCodeDictionary[70] = "F";
_keyCodeDictionary[71] = "G";
_keyCodeDictionary[72] = "H";
_keyCodeDictionary[73] = "I";
_keyCodeDictionary[74] = "J";
_keyCodeDictionary[75] = "K";
_keyCodeDictionary[76] = "L";
_keyCodeDictionary[77] = "M";
_keyCodeDictionary[78] = "N";
_keyCodeDictionary[79] = "O";
_keyCodeDictionary[80] = "P";
_keyCodeDictionary[81] = "Q";
_keyCodeDictionary[82] = "R";
_keyCodeDictionary[83] = "S";
_keyCodeDictionary[84] = "T";
_keyCodeDictionary[85] = "U";
_keyCodeDictionary[86] = "V";
_keyCodeDictionary[87] = "W";
_keyCodeDictionary[88] = "X";
_keyCodeDictionary[89] = "Y";
_keyCodeDictionary[90] = "Z";
_keyCodeDictionary[48] = "0";
_keyCodeDictionary[49] = "1";
_keyCodeDictionary[50] = "2";
_keyCodeDictionary[51] = "3";
_keyCodeDictionary[52] = "4";
_keyCodeDictionary[53] = "5";
_keyCodeDictionary[54] = "6";
_keyCodeDictionary[55] = "7";
_keyCodeDictionary[56] = "8";
_keyCodeDictionary[57] = "9";
_keyCodeDictionary[186] = ";";
_keyCodeDictionary[187] = "=";
_keyCodeDictionary[189] = "-";
_keyCodeDictionary[191] = "/";
_keyCodeDictionary[192] = "`";
_keyCodeDictionary[219] = "[";
_keyCodeDictionary[220] = "\\";
_keyCodeDictionary[221] = "]";
_keyCodeDictionary[222] = "'";
_keyCodeDictionary[188] = ",";
_keyCodeDictionary[190] = ".";
_keyCodeDictionary[96] = "Numpad 0";
_keyCodeDictionary[97] = "Numpad 1";
_keyCodeDictionary[98] = "Numpad 2";
_keyCodeDictionary[99] = "Numpad 3";
_keyCodeDictionary[100] = "Numpad 4";
_keyCodeDictionary[101] = "Numpad 5";
_keyCodeDictionary[102] = "Numpad 6";
_keyCodeDictionary[103] = "Numpad 7";
_keyCodeDictionary[104] = "Numpad 8";
_keyCodeDictionary[105] = "Numpad 9";
_keyCodeDictionary[106] = "Numpad Multiply";
_keyCodeDictionary[107] = "Numpad Add";
_keyCodeDictionary[109] = "Numpad Subtract";
_keyCodeDictionary[110] = "Numpad Decimal";
_keyCodeDictionary[111] = "Numpad Divide";
_keyCodeDictionary[112] = "F1";
_keyCodeDictionary[113] = "F2";
_keyCodeDictionary[114] = "F3";
_keyCodeDictionary[115] = "F4";
_keyCodeDictionary[116] = "F5";
_keyCodeDictionary[117] = "F6";
_keyCodeDictionary[118] = "F7";
_keyCodeDictionary[119] = "F8";
_keyCodeDictionary[120] = "F9";
_keyCodeDictionary[122] = "F11";
_keyCodeDictionary[123] = "F12";
_keyCodeDictionary[124] = "F13";
_keyCodeDictionary[125] = "F14";
_keyCodeDictionary[126] = "F15";

Function to retrieve from the Dictionary object:

1
2
3
4
5
6
7
8
9
public function getKeyboardFromKeyCode(keyCode:int):String
{
var string:String = null;

if (_keyCodeDictionary[keyCode])
string = (_keyCodeDictionary[keyCode] as String);

return string;
}

That’s all there is to it.  Sure it was tedious, but I’m sure this will be useful for displaying what keys are pressed on the screen, etc…