Example of keyboard event usage in JavaScript _javascript tips

Source: Internet
Author: User

This example analyzes keyboard event usage in JavaScript. Share to everyone for your reference. Specifically as follows:

The keyboard event contains the three events onkeydown, onkeypress, and onkeyup

Event initialization

function KeyDown () {} 
document.onkeydown = KeyDown; 
The KeyDown () function is invoked when you press which key on the keyboard. 

Under the DOM standard

function KeyDown (e) { 
var keycode = E.which;//get the corresponding key value (number) 
var realkey = String.fromCharCode (E.which); Gets the real character alert representing the modifier 
("Key code:" + KeyCode + "character:" + Realkey); 
} 
Document.onkeydown = KeyDown 

IE under

function KeyDown () { 
var keycode = Event.keycode//ie The method to get the key value 
var Realkey = String.fromCharCode (Eve      Nt.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 most useful for function keys, and the KeyPress event is most useful for printable keys

The following key values are completely valid only in the text box, and if used in the <body> tag, only the letter keys, numeric keys, and part of the control keys are available, and the key values of the letter keys and numeric keys are the same as the ASCII values

Key code values for letters and numeric keys (keycode)

Keypad 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

Key code value (keycode) function key value (keycode) for keys on numeric keypad

Keypad 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 112
F2 113
F3 114
F4 115
F5 116
F6 117
F7 118
F8 119
F9 120
F10 121
F11 122
F12 123

Control key key code value (KeyCode)

Keypad 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
[{219
}] 221
\| 220
=+ 187
,< 188
' "222
Cape Lock 20
Alt 18

I hope this article will help you with your JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.