博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js中获取键盘事件
阅读量:5266 次
发布时间:2019-06-14

本文共 5618 字,大约阅读时间需要 18 分钟。

只要你定义了这些键的动作,你在浏览器里按下这些键就会响应,兼容目前所有浏览器。
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


onKeyDown onKeyPress onKeyUp
Key Codes 0 0 0
Char Codes (IE5/Mac; NN6) 0 0 0
Modifier Keys Shift Shift Shift
Ctrl Ctrl Ctrl
Alt Alt Alt

 

转载于:https://www.cnblogs.com/jiangxiaobo/p/5953863.html

你可能感兴趣的文章
如何设置映射网络驱动器的具体步骤和方法
查看>>
ASP.NET WebApi 基于OAuth2.0实现Token签名认证
查看>>
283. Move Zeroes把零放在最后面
查看>>
Visual Studio Code 打开.py代码报Linter pylint is not installed解决办法
查看>>
Python 数据类型
查看>>
S5PV210根文件系统的制作(一)
查看>>
centos下同时启动多个tomcat
查看>>
slab分配器
查看>>
【读书笔记】C#高级编程 第三章 对象和类型
查看>>
针对sl的ICSharpCode.SharpZipLib,只保留zip,gzip的流压缩、解压缩功能
查看>>
【转】代码中特殊的注释技术——TODO、FIXME和XXX的用处
查看>>
【SVM】libsvm-python
查看>>
Jmeter接口压力测试,Java.net.BindException: Address already in use: connect
查看>>
Leetcode Balanced Binary Tree
查看>>
九.python面向对象(双下方法内置方法)
查看>>
go:channel(未完)
查看>>
[JS]递归对象或数组
查看>>
LeetCode(17) - Letter Combinations of a Phone Number
查看>>
Linux查找命令对比(find、locate、whereis、which、type、grep)
查看>>
路由器外接硬盘做nas可行吗?
查看>>