How to Get and judge the keyboard buttons in js, and get the keyboard buttons in js

Source: Internet
Author: User

How to Get and judge the keyboard buttons in js, and get the keyboard buttons in js
This article mainly introduces how to obtain and judge the keyboard buttons in JavaScript. It involves the acquisition of JavaScript Keyboard Events and key value determination techniques and provides some reference values. For more information, see

This article describes how to obtain and judge the keyboard keys in js. We will share this with you for your reference. The details are as follows:

The Keyboard Events in js are often used, so the key codes corresponding to the keyboard events are collected for sharing:

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 keyCode 8 = BackSpace BackSpacekeyCode 9 = Tab TabkeyCode 12 = ClearkeyCode 13 = EnterkeyCode 16 = Shift_LkeyCode 17 = Control_LkeyCode 18 = Alt_LkeyCode 19 = PausekeyCode 20 = Caps_LockkeyCode 27 = Escape EscapekeyCode 32 = spacekeyCode 33 = PriorkeyCode 34 = NextkeyCode 35 = EndkeyCode 36 = HomekeyCode 37 = LeftkeyCode 38 = UpkeyCode 39 = RightkeyCode 40 = DownkeyCode 41 = SelectkeyCode 42 = PrintkeyCode 43 = ExecutekeyCode 45 = InsertkeyCode 46 = DeletekeyCode 47 = HelpkeyCode 48 = 0 equal bracerightkeyCode 49 = 1 exclam onesuperiorkeyCode 50 = 2 quotedbl twosuperiorkeyCode 51 = 3 section threesuperiorkeyCode 52 = 4 dollarkeyCode 53 = 5 percentkeyCode 54 = 6 ampersandkeyCode 55 = 7 slash braceleftkeyCode 56 = 8 parenleft bracketleftkeyCode 57 = 9 parenright bracketrightkeyCode 65 = a AkeyCode 66 = b BkeyCode 67 = c CkeyCode 68 = d DkeyCode 69 = e E EuroSignkeyCode 70 = f FkeyCode 71 = g GkeyCode 72 = h HkeyCode 73 = i IkeyCode 74 = j JkeyCode 75 = k KkeyCode 76 = l LkeyCode 77 = m M mukeyCode 78 = n NkeyCode 79 = o OkeyCode 80 = p PkeyCode 81 = q Q atkeyCode 82 = r RkeyCode 83 = s SkeyCode 84 = t TkeyCode 85 = u UkeyCode 86 = v VkeyCode 87 = w WkeyCode 88 = x XkeyCode 89 = y YkeyCode 90 = z ZkeyCode 96 = KP_0 KP_0keyCode 97 = KP_1 KP_1keyCode 98 = KP_2 KP_2keyCode 99 = KP_3 KP_3keyCode 100 = KP_4 KP_4keyCode 101 = KP_5 KP_5keyCode 102 = KP_6 KP_6keyCode 103 = KP_7 KP_7keyCode 104 = KP_8 KP_8keyCode 105 = KP_9 KP_9keyCode 106 = KP_Multiply KP_MultiplykeyCode 107 = KP_Add KP_AddkeyCode 108 = KP_Separator KP_SeparatorkeyCode 109 = KP_Subtract KP_SubtractkeyCode 110 = KP_Decimal KP_DecimalkeyCode 111 = KP_Divide KP_DividekeyCode 112 = F1keyCode 113 = F2keyCode 114 = F3keyCode 115 = F4keyCode 116 = F5keyCode 117 = F6keyCode 118 = F7keyCode 119 = F8keyCode 120 = F9keyCode 121 = F10keyCode 122 = F11keyCode 123 = F12keyCode 124 = F13keyCode 125 = F14keyCode 126 = F15keyCode 127 = F16keyCode 128 = F17keyCode 129 = F18keyCode 130 = F19keyCode 131 = F20keyCode 132 = F21keyCode 133 = F22keyCode 134 = F23keyCode 135 = F24keyCode 136 = Num_LockkeyCode 137 = Scroll_LockkeyCode 187 = acute gravekeyCode 188 = comma semicolonkeyCode 189 = minus underscorekeyCode 190 = period colonkeyCode 192 = numbersign apostrophekeyCode 210 = plusminus hyphen macronkeyCode 211 =keyCode 212 = copyright registeredkeyCode 213 = guillemotleft guillemotrightkeyCode 214 = masculine ordfemininekeyCode 215 = ae AEkeyCode 216 = cent yenkeyCode 217 = questiondown exclamdownkeyCode 218 = onequarter onehalf threequarterskeyCode 220 = less greater barkeyCode 221 = plus asterisk asciitildekeyCode 227 = multiply divisionkeyCode 228 = acircumflex AcircumflexkeyCode 229 = ecircumflex EcircumflexkeyCode 230 = icircumflex IcircumflexkeyCode 231 = ocircumflex OcircumflexkeyCode 232 = ucircumflex UcircumflexkeyCode 233 = ntilde NtildekeyCode 234 = yacute YacutekeyCode 235 = oslash OobliquekeyCode 236 = aring AringkeyCode 237 = ccedilla CcedillakeyCode 238 = thorn THORNkeyCode 239 = eth ETHkeyCode 240 = diaeresis cedilla currencykeyCode 241 = agrave Agrave atilde AtildekeyCode 242 = egrave EgravekeyCode 243 = igrave IgravekeyCode 244 = ograve Ograve otilde OtildekeyCode 245 = ugrave UgravekeyCode 246 = adiaeresis AdiaeresiskeyCode 247 = ediaeresis EdiaeresiskeyCode 248 = idiaeresis IdiaeresiskeyCode 249 = odiaeresis OdiaeresiskeyCode 250 = udiaeresis UdiaeresiskeyCode 251 = ssharp question backslashkeyCode 252 = asciicircum degreekeyCode 253 = 3 sterlingkeyCode 254 = Mode_switch

Use the keyCode attribute of the event object to determine the Input key value

Eg:

Copy the Code as follows: if (event. keyCode = 13) alert ("enter! ");

 

Key value table

?
1234567891011121314151617181920 A  0X65  U   0X85B  0X66  V   0X86C  0X67  W   0X87D  0X68  X   0X88E  0X69  Y   0X89F  0X70  Z   0X90G  0X71  0   0X48H  0X72  1   0X49I  0X73  2   0X50J  0X74  3   0X51K  0X75  4   0X52L  0X76  5   0X53M  0X77  6   0X54N  0X78  7   0X55O  0X79  8   0X56P  0X80  9   0X57Q  0X81 ESC  0X1BR  0X82 CTRL  0X11S  0X83 SHIFT 0X10T  0X84 ENTER 0XD

If you want to use the key combination, you can determine whether these keys are simultaneously pressed, for example, you can use the ctrl key, shift key, and alt key combination to determine whether you have pressed the ctrl key, shift key, and alt key.

Js Keyboard Events:

?
1234567891011121314 <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 // What to do }}; </script>

As long as you define the actions of these keys, you press these keys in the browser to respond, compatible with all current browsers.

JS judgment keyboard buttons

?
123456789 window.document.onkeydown = disableRefresh;function disableRefresh(evt){evt = (evt) ? evt : window.eventif (evt.keyCode) { if(evt.keyCode == 116){ //do something }}}

The keycode is shown in the preceding example.

Use the keyCode attribute of the event object to determine the Input key value

Eg:

?
12345 if(event.keyCode==13){var ieKey=event.keyCode;// Obtain the key value in the numeric formatvar realkey=String.fromCharCode(ieKey) // Convert a numeric key value to a real keyalert(realkey);}

The table corresponding to the key value is shown in the preceding example.

I hope this article will help you design JavaScript programs.

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.