A simple way to achieve keyboard events in JS _javascript tips

Source: Internet
Author: User
<script type= "Text/javascript" Language=javascript charset= "UTF-8" >
   document.onkeydown=function (Event) {
      var e = Event | | window.event | | arguments.callee.caller.arguments[0];
      if (e && e.keycode==27) {//press ESC 
        //What to do
       }
      if (e && e.keycode==113) {//press F2 
         //What to do
        }      
       if (e && e.keycode==13) {//Enter key
         //What to do
      }
    }; 
</script>

As long as you define the actions of these keys, you will be able to respond to these keys in your browser, compatible with all browsers currently in the browser.

JS inside the keyboard event is often used, so the key to the collection of keyboard events corresponding to share: keycode 8 = BackSpace BackSpace keycode 9 = tab Tab keycode = Clear keycode = En ter keycode = shift_l KeyCode = control_l KeyCode = alt_l KeyCode = Pause keycode = Caps_lock keycode 27 = 
Escape Escape keycode = Space KeyCode = Prior keycode (Next keycode) = end keycode MB = Home KeyCode Panax Notoginseng = Left KeyCode = up keycode-keycode = down keycode = Select KeyCode = Print keycode = Execute keycode = Insert KeyCode = Delete KeyCode = Help keycode = 0 equal braceright keycode $ = 1 Exclam onesuperior keycod  E = 2 Quotedbl twosuperior keycode = 3 section Threesuperior keycode-4 dollar keycode 54 = 5 percent KeyCode
= 6 Ampersand keycode = 7 slash braceleft keycode-8 parenleft 9 Bracketleft keycode KeyCode = a A keycode (b b keycode) = c c KeyCode (d) keycode (e e eurosign keycode) = f F KeyCode 71 = G g keycode-H KEycode = i i keycode (J j keycode) = k K KeyCode the "l l keycode" = m m mu keycode p = n n keycode p = o k Eycode = p KeyCode i = q q at keycode (r r keycode m = s s keycode) = t T keycode (u u keycode) = v V k Eycode = w keycode (x x keycode) = y y keycode = z keycode-kp_0 kp_0 keycode  8 = kp_2 kp_2 keycode = kp_3 kp_3 keycode = kp_4 kp_4 keycode 103 = kp_5 Kp_5-keycode 102 = kp_6 Kp_6 keycode = Kp_7 kp_7 keycode (kp_8 kp_8 keycode) = Kp_9 kp_9 keycode-kp_multiply kp_multiply DD KeyCode 108 = kp_separator kp_separator keycode 109 = kp_subtract kp_subtract keycode = kp_decimal KP_Decimal keyCo de-kp_divide = kp_divide keycode 112 = F1 KeyCode 113 = F2 KeyCode 114 = F3 KeyCode-F4 keycode 117 = F6 KeyCode 118 = F7 KeyCode 119 = F8 KeyCode = F9 keycode 121 = F10 KeyCode 122 = F11 KeyCode 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 13  2 = F21 KeyCode F22 keycode 134 = F23 KeyCode 135 = F24 KeyCode 136 = num_lock KeyCode 137 = scroll_lock KeyCode 187 = Acute grave keycode 188 = comma semicolon keycode 189 = minus underscore keycode 190 = period Colon KeyCode-numbe Rsign apostrophe keycode 210 = plusminus hyphen Macron KeyCode-211 = keycode 212 = Copyright Registered KeyCode = 213 Guillemotright keycode 214 = masculine ordfeminine keycode 215 = AE AE keycode 216 = cent yen KeyCode 217 = ques Lemotleft Tiondown exclamdown keycode 218 = onequarter onehalf threequarters keycode = less Greater bar keycode 221 = plus aster 
ISK Asciitilde keycode = Multiply Division KeyCode 228 = Acircumflex Acircumflex keycode 229 = Ecircumflex Ecircumflex  keyCode 230 = icircumflex Icircumflex
keyCode 231 = ocircumflex Ocircumflex
keyCode 232 = ucircumflex Ucircumflex
keyCode 233 = Ntilde Ntilde keyCode 234 = Yacute Yacute keycode 235 = Oslash ooblique keycode 236 = aring aring keycode 237 = Ccedilla Ccedilla KeyCode 2
Thorn keycode 239 = ETH eth keycode thorn = diaeresis cedilla currency keycode 241 = Agrave Agrave Atilde Atilde keyCode 242 = egrave Egrave
keyCode 243 = igrave Igrave
keyCode 244 = ograve Ograve otilde Otilde
keyCode 245 = ugrave Ugr Ave KeyCode 246 = adiaeresis adiaeresis keycode 247 = ediaeresis ediaeresis keycode 248 = idiaeresis idiaeresis KeyCode 24 9 = odiaeresis odiaeresis keycode udiaeresis udiaeresis keycode = 251 ssharp question backslash KeyCode = 252 Ircum degree KeyCode 253 = 3 Sterling KeyCode 254 = Mode_switch Use the Event object's KeyCode property to determine the input key value eg:if (event.keycode==13) alert ("
Enter! ");  Key value corresponds to table A 0x65 U 0x85 B 0x66 V 0x86 C 0x67 W 0X87 D 0x68 X 0x88 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 0
X78 7 0x55O 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 a combination of keys, you can determine whether the keys are pressed at the same time, such as CTRL, SHIFT, and the combination of the ALT key to determine whether to press the CTRL key, the SHIFT key, and the ALT key.

The above is a small series of JS to get the keyboard event to obtain a simple way to achieve the full content, I hope that we support cloud-Habitat Community ~

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.