Introduction to Javascript detection of keyboard key information and key code values _ javascript tips-js tutorial

Source: Internet
Author: User
Javascript has three event handles in the input state of the corresponding keyboard: The Key is pressed (the key is pressed but not yet lifted), click the button (Press and raise the key) when the buttons are lifted (after the buttons are lifted), we will introduce them in detail. If you are interested, you can see that there are three event handles in Javascript in the input states of the corresponding keyboard: keydown, keypress, and keyup.
The corresponding meaning is: the button is pressed (the button is pressed but not yet lifted), click the button (Press and raise the button), the button is lifted (after the button is lifted)
Key Category
Buttons can be divided into "real key" and "virtual key"
The real key can be understood as the buttons that we can see and print, such as the letter "A", number "1", and character "?" And so on.
Virtual keys are keys that cannot be printed out for control, such as Ctrl, Alt, Shift, and arrow keys.
IE has a special case when processing the virtual key: the virtual key does not generate a keypress event and must be captured using keydown or keyup.
Key code and Verification Code
The key code is used by a computer to identify the encoding of different keys. Each key has a key code.
The escape code is unique to printable keys and corresponds to the characters displayed on the keyboard.
You can use String. fromCharCode () to convert the key code to a verification code.
The table corresponding to the key code and secret code can be found at the end of this article.
Obtain the real key

The Code is as follows:


Function getKeyCode (e ){
Var keyCode = 0;
Var e = e | window. event;
KeyCode = e. keyCode | e. which | e. charCode;
Alert (keyCode );
}


To explain this
1. e is a JS Hidden variable supported by Firefox and other standard browsers, indicating an "Event". In the IE series, there is no "e", but window. event to indicate "event"; so var e = e | window. event.
2. e. keyCode, e. which, and e. charCode indicate that the key code is obtained, but different browsers support different writing methods.
Obtain the virtual key

The Code is as follows:


Function getKeyCode (e ){
Var keyCode = 0;
Var e = e | window. event;
If (e. ctrlKey) alert ("ctrlKey pressed ");
If (e. altKey) alert ("altKey pressed ");
If (e. shiftKey) alert ("shiftKey pressed ");
}


Key value for letters and numbers)
Buttons Key code Buttons Key code Buttons Key code Buttons Key code
A 65 J 74 S 83 1 49
B 66 K 75 T 84 2 50
C 67 L 76 U 85 3 51
D 68 M 77 V 86 4 52
E 69 N 78 W 87 5 53
F 70 O 79 X 88 6 54
G 71 P 80 Y 89 7 55
H 72 Q 81 Z 90 8 56
I 73 R 82 0 48 9 57

  

KeyCode) Function key value (keyCode)
Buttons Key code Buttons Key code Buttons Key code Buttons Key code
0 96 8 104 F1 112 F7 118
1 97 9 105 F2 113 F8 119
2 98 * 106 F3 114 F9 120
3 99 + 107 F4 115 F10 121
4 100 Enter 108 F5 116 F11 122
5 101 - 109 F6 117 F12 123
6 102 . 110
7 103 / 111

  

Key Value)
Buttons Key code Buttons Key code Buttons Key code Buttons Key code
BackSpace 8 Esc 27 Right Arrow 39 -_ 189
Tab 9 Spacebar 32 Dw Arrow 40 .> 190
Clear 12 Page Up 33 Insert 45 /? 191
Enter 13 Page Down 34 Delete 46 '~ 192
Shift 16 End 35 Num Lock 144 [{ 219
Control 17 Home 36 ;: 186 \ | 220
Alt 18 Left Arrow 37 = + 187 ]} 221
Cape Lock 20 Up Arrow 38 , < 188 '" 222

Multimedia key value (keyCode)
Buttons Key code Buttons Key code Buttons Key code Buttons Key code
Volume increase 175
Volume reduction 174
Stop 179
Mute 173
Browser 172
Email 180
Search 170
Favorites 171
Related Article

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.