js中获取键盘事件的简单实现方法


Posted in Javascript onOctober 10, 2016
<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){ // 按 Esc 
        //要做的事情
       }
      if(e && e.keyCode==113){ // 按 F2 
         //要做的事情
        }      
       if(e && e.keyCode==13){ // enter 键
         //要做的事情
      }
    }; 
</script>

只要你定义了这些键的动作,你在浏览器里按下这些键就会响应,兼容目前所有浏览器。

js 里面的键盘事件经常用到,所以收集了键盘事件对应的键码来分享下:
keyCode 8 = BackSpace BackSpace
keyCode 9 = Tab 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 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 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 at
keyCode 82 = r R
keyCode 83 = s S
keyCode 84 = t T
keyCode 85 = u U
keyCode 86 = v V
keyCode 87 = w W
keyCode 88 = x X
keyCode 89 = 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 KP_Multiply
keyCode 107 = KP_Add KP_Add
keyCode 108 = KP_Separator KP_Separator
keyCode 109 = KP_Subtract KP_Subtract
keyCode 110 = KP_Decimal KP_Decimal
keyCode 111 = KP_Divide KP_Divide
keyCode 112 = F1
keyCode 113 = F2
keyCode 114 = F3
keyCode 115 = F4
keyCode 116 = F5
keyCode 117 = F6
keyCode 118 = F7
keyCode 119 = F8
keyCode 120 = 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 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
keyCode 189 = minus underscore
keyCode 190 = period colon
keyCode 192 = numbersign apostrophe
keyCode 210 = plusminus hyphen macron
keyCode 211 =
keyCode 212 = copyright registered
keyCode 213 = guillemotleft guillemotright
keyCode 214 = masculine ordfeminine
keyCode 215 = ae AE
keyCode 216 = cent yen
keyCode 217 = questiondown exclamdown
keyCode 218 = onequarter onehalf threequarters
keyCode 220 = less greater bar
keyCode 221 = plus asterisk asciitilde
keyCode 227 = 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 238 = thorn THORN
keyCode 239 = eth ETH
keyCode 240 = 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 Ugrave
keyCode 246 = adiaeresis Adiaeresis
keyCode 247 = ediaeresis Ediaeresis
keyCode 248 = idiaeresis Idiaeresis
keyCode 249 = odiaeresis Odiaeresis
keyCode 250 = udiaeresis Udiaeresis
keyCode 251 = ssharp question backslash
keyCode 252 = asciicircum degree
keyCode 253 = 3 sterling
keyCode 254 = Mode_switch
使用event对象的keyCode属性判断输入的键值
eg:if(event.keyCode==13)alert(“enter!”);
键值对应表
A0X65  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
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

如果要使用组合键,则可以判断是否同时按下了这几个键,比如ctrl键、shift键以及alt键的组合使用就可以判断是否多按下了ctrl键、shift键以及alt键

以上就是小编为大家带来的js中获取键盘事件的简单实现方法全部内容了,希望大家多多支持三水点靠木~

Javascript 相关文章推荐
快速排序 php与javascript的不同之处
Feb 22 Javascript
ajax上传时参数提交不更新等相关问题
Dec 11 Javascript
javascript实现通过表格绘制颜色填充矩形的方法
Apr 21 Javascript
浏览器复制插件zeroclipboard使用指南
Mar 26 Javascript
JS生成不重复的随机数组的简单实例
Jul 10 Javascript
Js得到radiobuttonlist选中值的两种方法(推荐)
Aug 25 Javascript
微信小程序用户自定义模版用法实例分析
Nov 28 Javascript
如何去除vue项目中的#及其ie9兼容性
Jan 11 Javascript
JS document内容及样式操作完整示例
Jan 14 Javascript
微信小程序实现二维码签到考勤系统
Jan 16 Javascript
js实现消灭星星(web简易版)
Mar 24 Javascript
vue treeselect获取当前选中项的label实例
Aug 31 Javascript
轻松实现jquery选项卡切换效果
Oct 10 #Javascript
为jQuery-easyui的tab组件添加右键菜单功能的简单实例
Oct 10 #Javascript
Javascript生成带参数的二维码示例
Oct 10 #Javascript
详细讲解JavaScript中的this绑定
Oct 10 #Javascript
jQuery EasyUI 右键菜单--关闭标签/选项卡的简单实例
Oct 10 #Javascript
jQuery EasyUI右键菜单实现关闭标签/选项卡
Oct 10 #Javascript
JavaScript 判断一个对象{}是否为空对象的简单方法
Oct 09 #Javascript
You might like
PHP 动态随机生成验证码类代码
2010/04/09 PHP
PHP取余函数介绍MOD(x,y)与x%y
2014/05/15 PHP
PHP整数取余返回负数的相关解决方法
2014/05/15 PHP
深入浅析php json 格式控制
2015/12/24 PHP
php入门教程之Zend Studio设置与开发实例
2016/09/09 PHP
PHP读取XML格式文件的方法总结
2017/02/27 PHP
另类调用flash无须激活的方法
2006/12/27 Javascript
javascript 数据类型转换(parseInt,parseFloat)
2010/07/20 Javascript
返回对象在当前级别中是第几个元素的实现代码
2011/01/20 Javascript
限制textbox或textarea输入字符长度的JS代码
2013/10/16 Javascript
jquery+CSS实现的水平布局多级网页菜单效果
2015/08/24 Javascript
15个值得开发人员关注的jQuery开发技巧和心得总结【经典收藏】
2016/05/25 Javascript
javascript鼠标跟随运动3种效果(眼球效果,苹果菜单,方向跟随)
2016/10/27 Javascript
详解webpack打包后如何调试的方法步骤
2018/11/07 Javascript
详解vue中移动端自适应方案
2019/05/05 Javascript
中高级前端必须了解的JS中的内存管理(推荐)
2019/07/04 Javascript
详解node和ES6的模块导出与导入
2020/02/19 Javascript
JavaScript装饰者模式原理与用法实例详解
2020/03/09 Javascript
React中Ref 的使用方法详解
2020/04/28 Javascript
python使用urllib2模块获取gravatar头像实例
2013/12/18 Python
Python数据结构之翻转链表
2017/02/25 Python
基于循环神经网络(RNN)的古诗生成器
2018/03/26 Python
Django数据库类库MySQLdb使用详解
2019/04/28 Python
python使用pandas处理大数据节省内存技巧(推荐)
2019/05/05 Python
Jacobi迭代算法的Python实现详解
2019/06/29 Python
Python实现手机号自动判断男女性别(实例解析)
2019/12/22 Python
关于css兼容性问题及一些常见问题汇总
2016/05/03 HTML / CSS
澳大利亚领先的皮肤诊所:Skin Matrix(抗衰老、痤疮专家、药妆护肤)
2018/05/20 全球购物
英国在线购买马术服装:EQUUS
2019/07/12 全球购物
C++:memset ,memcpy和strcpy的根本区别
2013/04/27 面试题
采购经理岗位职责
2014/02/16 职场文书
健康状况证明书
2014/11/26 职场文书
2016国培学习心得体会
2016/01/08 职场文书
创业计划书详解
2019/07/19 职场文书
浅谈vue2的$refs在vue3组合式API中的替代方法
2021/04/18 Vue.js
html+css实现文字折叠特效实例
2021/06/02 HTML / CSS