js捕捉键盘事件和按键键值的方法


Posted in Javascript onOctober 10, 2016

如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript" type="text/javascript">
function test(){
  alert('test');
}
function Hotkey(event, targetObj, ctrlKey, shiftKey, altKey, keycode){
  if (targetObj && event.ctrlKey == ctrlKey && event.shiftKey == shiftKey && event.altKey == altKey && event.keyCode == keycode)
    targetObj.click();
}
function fnKeyup(event){
  var b = document.getElementById("myButton");
  Hotkey(event, b, true, false, false, 13);
}
if (document.addEventListener){
  document.addEventListener("keyup",fnKeyup,true);
}else{
  document.attachEvent("onkeyup",fnKeyup);
}
</script>
<body>
<input type="button" id="myButton" name="Submit" value="按钮" onclick="test()" />
</body>
</html>

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 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
document.onkeydown = keydown;
function keydown(){
 var keycode = event.keyCode;
 if(keycode == 13) {
 alert("test");
 }
}

<body onload="JavaScript:document.getElementById('textfield2').focus();">

以上就是小编为大家带来的js捕捉键盘事件和按键键值的方法全部内容了,希望大家多多支持三水点靠木~

Javascript 相关文章推荐
js直接编辑当前cookie的脚本
Sep 14 Javascript
ext读取两种结构的xml的代码
Nov 05 Javascript
分享8款优秀的 jQuery 加载动画和进度条插件
Oct 24 Javascript
表单验证的完整应用案例探讨
Mar 29 Javascript
JAVASCRIPT模式窗口中下载文件无法接收iframe的流
Oct 11 Javascript
jQuery实现将div中滚动条滚动到指定位置的方法
Aug 10 Javascript
JS制作适用于手机和电脑的通知信息效果
Oct 28 Javascript
在vue中获取dom元素内容的方法
Jul 10 Javascript
在Vue组件化中利用axios处理ajax请求的使用方法
Aug 25 Javascript
angular项目中bootstrap-datetimepicker时间插件的使用示例
Mar 15 Javascript
jQuery实现的简单拖拽功能示例【测试可用】
Aug 14 jQuery
JavaScript Reflect Metadata实现详解
Dec 12 Javascript
js中获取键盘事件的简单实现方法
Oct 10 #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
You might like
PHP漏洞全解(详细介绍)
2012/11/13 PHP
php输出1000以内质数(素数)示例
2014/02/16 PHP
深入解析PHP的Laravel框架中的event事件操作
2016/03/21 PHP
jQuery打印指定区域Html页面并自动分页
2014/07/04 Javascript
javascript实现表单提交后,提交按钮不可用的方法
2015/04/18 Javascript
JavaScript中的toUTCString()方法使用详解
2015/06/12 Javascript
jQuery中的一些常见方法小结(推荐)
2016/06/13 Javascript
jQuery3.0中的buildFragment私有函数详解
2016/08/16 Javascript
AngularJs bootstrap搭载前台框架——js控制部分
2016/09/01 Javascript
js实现目录链接,内容跟着目录滚动显示的简单实例
2016/10/15 Javascript
Jquery表单验证失败后不提交的解决方法
2016/10/18 Javascript
Javascript实现图片懒加载插件的方法
2016/10/20 Javascript
Vue.js实战之组件的进阶
2017/04/04 Javascript
微信小程序利用co处理异步流程的方法教程
2017/05/20 Javascript
AngularJS实现controller控制器间共享数据的方法示例
2017/10/30 Javascript
nodejs取得当前执行路径的方法
2018/05/13 NodeJs
hmac模块生成加入了密钥的消息摘要详解
2018/01/11 Python
Python测试网络连通性示例【基于ping】
2018/08/03 Python
Python文件读写保存操作的示例代码
2018/09/14 Python
Python基础之函数的定义与使用示例
2019/03/23 Python
python写日志文件操作类与应用示例
2019/07/01 Python
python3 自动识别usb连接状态,即对usb重连的判断方法
2019/07/03 Python
Django之提交表单与前后端交互的方法
2019/07/19 Python
python GUI库图形界面开发之PyQt5结合Qt Designer创建信号与槽的详细方法与实例
2020/03/08 Python
浅谈Python协程
2020/06/17 Python
Python使用eval函数执行动态标表达式过程详解
2020/10/17 Python
类的核心特性有哪些
2014/01/01 面试题
用缩写的指针比较"if(p)" 检查空指针是否可靠?如果空指针的内部表达不是0会怎么样?
2014/01/05 面试题
法制宣传实施方案
2014/03/13 职场文书
2014年幼儿园安全工作总结
2014/11/10 职场文书
春节晚会开场白
2015/05/29 职场文书
爱国主义教育基地观后感
2015/06/18 职场文书
关于远足的感想
2015/08/10 职场文书
2016年6月份红领巾广播稿
2015/12/21 职场文书
如何搭建 MySQL 高可用高性能集群
2021/06/21 MySQL
MySQL数据管理操作示例讲解
2022/12/24 MySQL