JS obtains the key code, how Js shields users' keys, and Js obtains the ASII code corresponding to users' keys (compatible with all browsers)

Source: Internet
Author: User

When using JavaScript For WEB keyboard event listening and capturing, we mainly use onkeypress, onkeydown, and onkeyup events. The execution sequence of these three events is onkeydown, onkeypress, onkeyup, generally, three types of keyboard events can be used to effectively respond to keyboard input, but there may be some differences in actual applications. The onkeypress event cannot be used to respond to system function keys (for example, delete, in this case, the system responds normally. Both onkeydown and onkeyup can effectively intercept the system function key, but the event capture location is different, you can select different Keyboard Events as needed.

Because onkeypress cannot capture system function keys. the keyCode attribute of the event object is different from the keyCode attribute obtained in the onkeyup and onkeyup Keyboard Events, mainly because the keyCode of the onkeypress event is case sensitive to letters, while the onkeydown and onkeyup events are not sensitive; the keyCode of the onkeypress event cannot distinguish between the number keys on the primary key disk and the number keys on the pay keyboard. the keyCode of onkeydown and onkeyup is sensitive to the number keys on the master and pay keyboard.

Js judges the keys pressed by the user, and then performs different operations based on the keys pressed:

document.body.onkeydown=window.onkeydown=function(e){var keyCode;if(!e) e=window.event;if(document.all){keyCode=e.keyCode;}else{keyCode=e.which;}if(keyCode==37){window.location.href="http://www.phpernote.com";}if(keyCode==39||keyCode==13){window.location.href="http://www.phpernote.com";}}

How does js shield various buttons entered by users? See the following example:

Function KeyDown () {if (window. event. altKey) & (window. event. keyCode = 37) | (window. event. keyCode = 39) {// block Alt + arrow keys → alert ("You are not allowed to use ALT + arrow keys to move forward or backward the webpage! "); Event. returnValue = false;} if (event. keyCode = 8) | (event. keyCode = 116) {// block the F5 refresh key event. keyCode = 0; event. returnValue = false;} if (event. ctrlKey) & (event. keyCode = 78) {// block Ctrl + nevent. returnValue = false;} if (event. shiftKey) & (event. keyCode = 121) {// block shift + F10event. returnValue = false;} if (event. keyCode = 122) {// block F11event. returnValue = false ;}}

The ASII code corresponding to various keyboard buttons is as follows:

Keycode 8 = BackSpace
Keycode 9 = Tab
Keycode 12 = Clear
Keycode 13 = Enter
Keycode 16 = Shift_L
Keycode 17 = Control_L
Keycode 18 = Alt_L
Keycode 19 = Pause
Keycode 20 = Caps_Lock
Keycode 27 = Escape
Keycode 32 = space
Keycode 33 = Prior
Keycode 34 = Next
Keycode 35 = End
Keycode 36 = Home
Keycode 37 = Left
Keycode 38 = Up
Keycode 39 = Right
Keycode 40 = Down
Keycode 41 = Select
Keycode 42 = Print
Keycode 43 = Execute
Keycode 45 = Insert
Keycode 46 = Delete
Keycode 47 = Help
Keycode 48 = 0 equal braceright
Keycode 49 = 1 exclam onesuperior
Keycode 50 = 2 quotedbl twosuperior
Keycode 51 = 3 section threesuperior
Keycode 52 = 4 dollar
Keycode 53 = 5 percent
Keycode 54 = 6 ampersand
Keycode 55 = 7 slash braceleft
Keycode 56 = 8 parenleft bracketleft
Keycode 57 = 9 parenright bracketright
Keycode 65 = a
Keycode 66 = B B
Keycode 67 = c C
Keycode 68 = d D
Keycode 69 = e E EuroSign
Keycode 70 = f F

Keycode 71 = g G
Keycode 72 = h H
Keycode 73 = I I
Keycode 74 = j J
Keycode 75 = k K
Keycode 76 = l L
Keycode 77 = m M mu
Keycode 78 = n N
Keycode 79 = o O
Keycode 80 = p P
Keycode 81 = q Q
Keycode 82 = r R
Keycode 83 = s
Keycode 84 = t T
Keycode 85 = u U
Keycode 86 = v V
Keycode 87 = w
Keycode 88 = x X
Keycode 89 = y Y y
Keycode 90 = z Z
Keycode 96 = KP_0 KP_0
Keycode 97 = KP_1 KP_1
Keycode 98 = KP_2 KP_2
Keycode 99 = KP_3 KP_3
Keycode 100 = KP_4 KP_4
Keycode 101 = KP_5 KP_5
Keycode 102 = KP_6 KP_6
Keycode 103 = KP_7 KP_7
Keycode 104 = KP_8 KP_8
Keycode 105 = KP_9 KP_9
Keycode 106 = KP_Multiply
Keycode 107 = KP_Add

Key code 108 = KP_Separator
Keycode 109 = KP_Subtract
Key code 110 = KP_Decimal
Keycode 111 = KP_Divide
Key code 112 = F1
Keycode 113 = F2
Key code 114 = F3
Keycode 115 = F4
Key code 116 = F5
Keycode 117 = F6
Key code 118 = F7
Key code 119 = F8
Keycode 120 = F9
Keycode 121 = F10
Keycode 122 = F11
Key Code 123 = F12
Keycode 124 = F13
Keycode 125 = F14
Keycode 126 = F15
Keycode 127 = F16
Keycode 128 = F17
Keycode 129 = F18
Keycode 130 = F19
Keycode 131 = F20
Keycode 132 = F21
Keycode 133 = F22
Keycode 134 = F23
Keycode 135 = F24
Keycode 136 = Num_Lock
Keycode 137 = Scroll_Lock
Keycode 187 = acute grave
Keycode 188 = comma semicolon
Key code 189 = minus underscore
Key code 190 = period colon
Key code 192 = numbersign apostrophe
Key Code 210 = plusminus hyphen macron
Key code 211 =
Key code 212 = copyright registered
Key code 213 = guillemotleft guillemotright
Keycode 214 = masculine ordfeminine
Keycode 215 = AE AE
Keycode 216 = cent yen
Key code 217 = questiondown exclamdown
Key code 218 = onequarter onehalf threequarters
Keycode 220 = less greater bar
Key code 221 = plus asterisk asciitilde
Key code 227 = multiply division

Keycode 228 = acircumflex Acircumflex
Key code 229 = ecircumflex Ecircumflex
Keycode 230 = icircumflex Icircumflex
Key code 231 = ocircumflex Ocircumflex
Keycode 232 = ucircumflex Ucircumflex
Keycode 233 = ntilde Ntilde
Keycode 234 = yacute Yacute
Key code 235 = oslash Ooblique
Key Code 236 = aring Aring
Keycode 237 = ccedilla Ccedilla
Key code 238 = thorn THORN
Keycode 239 = eth ETH
Key code 240 = diaeresis cedilla currency
Key code 241 = agrave Agrave atilde Atilde
Keycode 242 = egrave Egrave
Keycode 243 = igrave Igrave
Key code 244 = ograve Ograve otilde Otilde
Keycode 245 = ugrave Ugrave
Keycode 246 = adiaeresis Adiaeresis
Keycode 247 = ediaeresis Ediaeresis
Keycode 248 = idiaeresis Idiaeresis
Keycode 249 = odiaeresis Odiaeresis
Keycode 250 = udiaeresis Udiaeresis
Key code 251 = ssharp question backslash
Keycode 252 = asciicircum degree
Keycode 253 = 3 sterling
Keycode 254 = Mode_switch

Use the keyCode attribute of the event object to determine the Input key value
Eg: if (event. keyCode = 13) alert ("enter !");
Key value table
A 0X65 U 0X85
B 0X66 V 0X86
C 0X67 W 0X87
D 0X68X0X88
E 0X69 Y 0X89
F 0X70 Z 0X90
G 0X71 0 0X48
H 0X72 1 0X49
I 0X73 2 0X50
J 0X74 3 0X51
K 0X75 4 0X52
L 0X76 5 0X53
M 0X77 6 0X54
N 0X78 7 0X55
O 0X79 8 0X56
P 0X80 9 0X57
Q 0X81 ESC 0X1B
R 0X82 CTRL 0X11
S 0X83 SHIFT 0X10
T 0X84 ENTER 0XD

If you want to use the combination key, you can use event. ctrlKey, event. shiftKey, event. altKey to determine whether ctrl, shift, and alt keys are pressed.

Articles you may be interested in
  • Move the mouse over the text to display the floating layer prompt special effects (compatible with all browsers such as IE and Firefox)
  • Webpage background music setting method, compatible with all browsers
  • The css setting layer is transparent and compatible with all browsers.
  • Jquery pop-up window plug-in (compatible with all browsers)
  • Js shields mouse and keyboard events (including right-click, direction key, backspace key, F5 refresh key, etc.), compatible with IE and firefox
  • How to set the DIV layer to display on a flash object, compatible with browsers such as ie and firefox
  • JS checks whether the scroll bar stops scrolling, and is compatible with IE and Firefox browsers.
  • Javascript determines whether the mouse is left or right-click-compatible with ie, firefox, chrome, and other major browsers

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.