Javascript jQuery .keypress & .keydown .which
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10191621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery .keypress & .keydown .which
提问by ChrisMJ
Ok so what is the difference in .keypress and .keydown/.keyup? At present I am using .keydown which returns a .which value of 38 for my key, now if i change it to .keypress it returns a value of 109 for that same key. What is the difference and why are the values different for the same key?
好的,那么 .keypress 和 .keydown/.keyup 有什么区别?目前我正在使用 .keydown ,它为我的密钥返回 .which 值 38,现在如果我将其更改为 .keypress ,它会为同一个键返回 109 的值。有什么区别,为什么同一个键的值不同?
回答by RvdK
If you press a button it fires a keydown
and releasing it fires a keyup
. The keypress
usually comes between those two.
如果你按下一个按钮它会触发 akeydown
并释放它会触发 a keyup
。的keypress
通常是那些介于两者之间。
keydown
and keyup
talk about which keyhas been changed. keypress
tells which characterthat key represents.
keydown
并keyup
讨论更改了哪个键。keypress
告诉键代表哪个字符。
Note that this is all browser-dependent!
请注意,这完全取决于浏览器!
See this article about the differences between the key events as implemented on various browsers.
回答by SpYk3HH
I'll be d$%^@d, there really is a difference with keypress and all this time I never realized. lol
我会是 d$%^@d,按键真的有区别,而且我一直没有意识到。哈哈
See my fiddle and try something like the letter "r"
查看我的小提琴并尝试使用字母“r”之类的东西
http://jsfiddle.net/SpYk3/NePCm/
http://jsfiddle.net/SpYk3/NePCm/
Somehow I never paid attention to this
不知怎的,我从来没有关注过这个
Found more info:
http://www.quirksmode.org/js/keys.html
"The two properties are
keyCode
andcharCode
. Put (too) simply,keyCode
says something about the actual keyboard key the user pressed, whilecharCode
gives the ASCII value of the resulting character. These bits of information need not be the same; for instance, a lower case 'a' and an upper case 'A' have the samekeyCode
, because the user presses the same key, but a differentcharCode
because the resulting character is different.Explorer and Opera do notsupport
charCode
. However, they give the character information inkeyCode
, but only withonkeypress
.onkeydown
and-up
keyCode
contains key information."
找到更多信息:
http://www.quirksmode.org/js/keys.html
“这两个属性是
keyCode
和charCode
。简单地keyCode
说(太),说明用户按下的实际键盘键,同时charCode
给出结果字符的 ASCII 值。这些信息位不必相同;例如,小写'a' 和大写的 'A' 具有相同的keyCode
,因为用户按下了相同的键,但是不同的charCode
因为产生的字符不同。Explorer 和 Opera不支持
charCode
. 但是,它们在 中提供字符信息keyCode
,但仅使用onkeypress
。onkeydown
并-up
keyCode
包含关键信息。”
回答by jbl
You should read the following post : http://javascript.info/tutorial/keyboard-events
您应该阅读以下帖子:http: //javascript.info/tutorial/keyboard-events
Keydown triggers on any key press and gives scan-code. Keypress triggers after keydown and gives char-code, but it is guaranteed for character keys only.
Keydown 会在任何按键按下时触发并提供扫描码。Keypress 在 keydown 后触发并提供字符代码,但仅保证字符键。
回答by Lucia
In normal cases, go for keyup
:
在正常情况下,请执行以下操作keyup
:
$(document).keyup(function(e){
console.log(e.which);
});
Reasons:
原因:
keydown
keeps firing when user holds the keys down, whilekeypress
andkeyup
fire only once.keypress
doesn't detect special keys (e.g.SHIFT
),keydown
andkeyup
do.
keydown
当用户按住键时保持发射,而keypress
和keyup
仅发射一次。keypress
不检测特殊键(例如SHIFT
),keydown
并且keyup
做。
回答by Denzyl Dick
KeyPress happens after KeyDown. So you can use KeyDown to determine what key it is, then KeyPress to disallow that character.
KeyPress 发生在 KeyDown 之后。因此,您可以使用 KeyDown 来确定它是什么键,然后使用 KeyPress 来禁止该字符。