只要你定义了这些键的动作,你在浏览器里按下这些键就会响应,兼容目前所有浏览器。
js 里面的键盘事件经常用到,所以收集了键盘事件对应的键码来分享下: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使用event对象的keyCode属性判断输入的键值eg:if(event.keyCode==13)alert(“enter!”);键值对应表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如果要使用组合键,则可以判断是否同时按下了这几个键,比如ctrl键、shift键以及alt键的组合使用就可以判断是否多按下了ctrl键、shift键以及alt键
字母和数字键的键码值(keyCode) | |||||||
---|---|---|---|---|---|---|---|
按键 | 键码 | 按键 | 键码 | 按键 | 键码 | 按键 | 键码 |
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) | 功能键键码值(keyCode) | ||||||
---|---|---|---|---|---|---|---|
按键 | 键码 | 按键 | 键码 | 按键 | 键码 | 按键 | 键码 |
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 |
控制键键码值(keyCode) | |||||||
---|---|---|---|---|---|---|---|
按键 | 键码 | 按键 | 键码 | 按键 | 键码 | 按键 | 键码 |
BackSpace | 8 | Esc | 27 | Right Arrow | 39 | -_ | 189 |
Tab | 9 | Spacebar | 32 | Down 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 A |
另附上一段代码,
可以根据按的键盘上的键,显示键盘动作,并显示keyCode值等
代码:
Keyboard Event Handler Lab