Javascript Keyboard Events
Zookeeper
Keyboard Events include onkeydown, onkeypress, and onkeyup events.
Event Initialization
Function keyDown () {} document. onkeydown = keyDown; // The KeyDown () function is called when any key on the keyboard is pressed.
Under the DOM Standard
Function keyDown (e) {var keycode = e. which; // obtain the corresponding key value (number) var realkey = String. fromCharCode (e. which); // obtain the real character alert ("key code:" + keycode + "character:" + realkey);} document. onkeydown = keyDown
IE
Function keyDown () {var keycode = event. keyCode; // Method for retrieving key values in IE var realkey = String. fromCharCode (event. keyCode); alert ("key code:" + keycode + "character:" + realkey);} document. onkeydown = keyDown
Compatible methods
Function keyUp (e) {var currKey = 0, e = e | event; currKey = e. keyCode | e. which | e. charCode; var keyName = String. fromCharCode (currKey); alert ("key code:" + currKey + "character:" + keyName);} document. onkeyup = keyUp;
Usage principle: the keydown event is the most useful for functional buttons, and the keypress event is the most useful for printable buttons.
The following key value is valid only in the text box.Only the primary key, number key, and some control keys are available. The key value of the letter key and number key is the same as the ASCII value.
Key value for letters and numbers)
Key code
A 65
B 66
C 67
D 68
E 69
F 70
G 71
H 72
I 73
J 74
K 75
L 76
M 77
N 78
O 79
P 80
Q 81
R 82
S 83
T 84
U 85
V 86
W 87
X 88
Y 89
Z 90
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
KeyCode function keyCode)
Key code
0 96
1 97
2 98
3 99
4 100
5 101
6 102
7 103
8 104
9 105
* 106
+ 107
Enter 108
-109
. 110
/111
F1 1, 112
F2 1, 113
F3 1, 114
F4. 115
F5 116
F6 117
F7 118
F8119
F9, 120
F10 121
F11 122
F12 123
Key Value)
Key code
BackSpace 8
Esc 27
Right Arrow 39
Left Arrow 37
Down Arrow 40
Up Arrow 38
-_ 189
.> 190
Spacebar 32
Tab 9
Clear 12
Page Up 33
Page Down 34
Enter 13
Insert 45
;: 186
Delete 46
'~ 192
/? 191
Num Lock 144
Control 17
Home 36
End 35
Shift 16
<G id = "1"> [{219]
}] 221
\| 220
= More than 187
, <1, 188
'"222
Cape Lock 20
Alt 18