keypress和keydown事件及keyCode和keyChar
二:keypress和keydown事件:
现只在IE10、chrome(版本 32.0.1700.107 m)、firefox(25.0.1)中测试了。
IE8 | chrome | firefox | |
$(document).keydown() | yes | yes | yes |
$('window').keydown() | no | yes | yes |
$('body').keydown() | no | yes | yes |
keypress的表现与上表一致。
不仅keypress和keydown得到的按键值不同,不同浏览器的返回值event.keyCode、event.charCode也不同:
Q键 | q键 | Caps Lock键 | Tab键 | 左方向键 | 数字1键 | Enter键 | 空格键 | F5键 | |||||||||||
key down |
key press |
key down |
key press |
key down |
key press |
key down |
key press |
key down |
key press |
key down |
key press |
key down |
key press |
key down |
key press |
key down |
key press |
||
IE10 | keyCode | 81 | 81 | 81 | 113 | 20 | -- | 9 | -- | 37 | -- | 49 | 49 | 13 | 13 | 32 | 32 | 116 | -- |
charCode | 0 | 81 | 0 | 113 | 0 | -- | 0 | -- | 0 | -- | 0 | 49 | 0 | 13 | 0 | 32 | 0 | -- | |
chrome | keyCode | 81 | 81 | 81 | 113 | 20 | -- | 9 | 9 | 37 | -- | 49 | 49 | 13 | 13 | -- | 32 | 116 | -- |
charCode | 0 | 81 | 0 | 113 | 0 | -- | 0 | 9 | 0 | -- | 0 | -- | 0 | 13 | -- | 32 | 0 | -- | |
firefox | keyCode | 81 | 0 | 81 | 0 | 20 | -- | 9 | 9 | 37 | 37 | 49 | 0 | 13 | 13 | 32 | 0 | 116 | 116 |
charCode | 0 | 81 | 0 | 113 | 0 | -- | 0 | 0 | 0 | 0 | 0 | 49 | 0 | 0 | 0 | 32 | 0 | 0 |
(还有一个event.whice:A non-standard property, the hybrid of charCode and keyCode, with the sole purpose to confuse a developer.
But in the zoo of key events it also plays a good role. Particulary, it helps to get the character. )
还有一些属性:shiftKey, ctrlKey, altKey, metaKey,布尔类型的值,检测按键是否是shift、ctrl、alt、Command(Mac only).
可以看出对于keyCode:
keydown几乎对所有按键都有返回值,且对大小写字母的返回值相同;
kepress似乎对只对字母按键有返回值,且对大小写字母的返回值不一样.
关于keydown、keypress、keyup的区别,参考http://javascript.info/tutorial/keyboard-events,上面写的很详细,摘录如下:
对于keydown,任何按键事件都能触发它,并返回scan-code(按键本身固有的数字代码,所有同一个键的scan-code肯定相同);
对于keypress,只保证character keys(字符按键)事件能触发它,并返回char-code(按键的unicode字符,区分大小写按键,charCode只在keypress中返回。js中的str.charCodeAt() 方法就可返回指定位置的字符的 Unicode 编码)。
For all keys except ';', '='
and '-'
different browsers use same key code.。
看了上面的表是不是觉得有点......,参考网页中还给出了keypress的事件代码
如何获取键入的字符值和数字值:
// only keypress event
function getChar(event) {
if (event.which == null) {
return String.fromCharCode(event.keyCode) // IE
} else if (event.which!=0 && event.charCode!=0) {
return String.fromCharCode(event.which) // the rest
} else {
return null // special key
}
}
测试IE10、chrome、firefox有效。
由于String.fromCharCode对special keys 会返回weird results.所有不能直接return String.fromCharCode(event.keyCode||event.charCode)。
如何获取包含特殊键在内的组合键:
以ctrl+e为例:
document.onkeydown = function(e) {
e = e || event;
if ((e.ctrlKey && e.keyCode == 'E'.charCodeAt(0)) ) {
//do something
}
由于keyCode对大小写字符返回的都是大写字母值,所以e.keyCode == 'E'.charCodeAt(0)对e和E都能识别。
测试IE10、chrome、firefox有效。(如果先按住E,再同时按ctrl,则都无效)
keypress和keydown事件及keyCode和keyChar的更多相关文章
- javascript 中 keyup、keypress和keydown事件
keyup.keypress和keydown事件都是有关于键盘的事件 1. keydown事件在键盘的键被按下的时候触发,keyup 事件在按键被释放的时候触发 keydown.keypress ...
- js keyup、keypress和keydown事件
js keyup.keypress和keydown事件都是有关于键盘的事件 当一个按键被pressed 或released在每一个现代浏览器中,都可能有三种客户端事件. keydown event k ...
- js keyup、keypress和keydown事件 详解
js keyup.keypress和keydown事件 详解 js keyup.keypress和keydown事件都是有关于键盘的事件 当一个按键被pressed 或released在每一个现代浏 ...
- 转载关于KeyPress和KeyDown事件的区别和联系
KeyDown:在控件有焦点的情况下按下键时发生. KeyPress:在控件有焦点的情况下按下键时发生. KeyUp:在控件有焦点的情况下释放键时发生. 1.KeyPress主要用来接收字母.数字等A ...
- C#学习笔记-KeyDown、KeyPress、KeyUp事件以及KeyCode、KeyData、KeyValue、KeyChar属性
本来没打算单独写的,但是在自己弄测试小程序的时候,越写发现不清楚的东西越多,所以实践又一次证明:纸上得来终觉浅,绝知此事要躬行! 直接贴代码了: //发生顺序:KeyDown->KeyPress ...
- js获取浏览器的keydown事件(附keycode码)
<script type="text/javascript" language=JavaScript charset="UTF-8"> docume ...
- C# 如何捕获键盘按钮和组合键以及KeyPress/KeyDown事件之间的区别 (附KeyChar/KeyCode值)
1. 首先将窗口属性KeyPreview设为true,如果属性对话框中找不到,就直接在代码里添加: 2. 添加KeyPress / KeyDown事件: 1.KeyPress 和KeyDown .Ke ...
- Winform下KeyDown,KeyPress,KeyUp事件的总结(转)
原文: http://www.cnblogs.com/xiashengwang/archive/2011/09/15/2578798.html 在winform程序中,经常会用到这几个事件用于控制数字 ...
- C#控件中的KeyDown、KeyPress 与 KeyUp事件浅谈
研究了一下KeyDown,KeyPress 和 KeyUp 的学问.让我们带着如下问题来说明: 1.这三个事件的顺序是怎么样的? 2.KeyDown 触发后,KeyUp是不是一定触发? 3.三个事件的 ...
随机推荐
- Linux_jdk path (execute and install)
作者:潇湘隐者 出处:http://www.cnblogs.com/kerrycode/ 1:echo $JAVA_HOME 使用$JAVA_HOME的话能定位JDK的安装路径的前提是配置了环境变量$ ...
- iOS7——图像资源Images Assets
iOS7初体验(3)——图像资源Images Assets 分类: iOS开发2013-06-18 16:02 17583人阅读 评论(2) 收藏 举报 ios7Images xcassets图像资源 ...
- 当fixed元素相互嵌套时chrome下父元素会影响子元素的层叠关系
问题:fixed元素被另一个fixed元素包含的时候在chrome下fixed子元素的定位会受到父元素的影响. demo(http://jsbin.com/qumah/1): <!DOCTYPE ...
- Java: for(;;) vs. while(true)
What is the difference between a standard while(true) loop and for(;;)? Is there any, or will both b ...
- BZOJ1639: [Usaco2007 Mar]Monthly Expense 月度开支
1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 529 Solved: ...
- cf509B Painting Pebbles
B. Painting Pebbles time limit per test 1 second memory limit per test 256 megabytes input standard ...
- UITableView的刷新
UITableView的刷新1> 数据刷新的总体步骤* 修改模型数据* 刷新表格(刷新界面) 2> 刷新表格(刷新界面)的方法* 全局刷新(每一行都会重新刷新)- (void)reload ...
- 关于IE8导航串行的问题
1.概述: 作为一个前端人员,多浏览器兼容是必须必备的技能,现在一般要求是兼容IE8及以上,如果兼容IE6的话,会麻烦一些,这里介绍的是在IE8状态下我们导航条错位的问题. 2.导航错位代码 < ...
- 经常使用ASCII码表(方便查找)
经常使用ASCII码表(方便查找) 键盘 ASCII码 键盘 ASCII码 键盘 ASCII码 键盘 ASCII码 ESC 27 7 55 O 79 g 103 SPACE 32 8 56 P 80 ...
- 三、Solr多核心及分词器(IK)配置
多核心的概念 多核心说白了就是多索引库.也可以理解为多个"数据库表" 说一下使用multicore的真实场景,比若说,产品搜索和会员信息搜索,不使用多核也没问题,这样带来的问题是 ...