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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:15:00  来源:igfitidea点击:

jQuery .keypress & .keydown .which

javascriptjquerykeyboardkeypresskeydown

提问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 keydownand releasing it fires a keyup. The keypressusually comes between those two.

如果你按下一个按钮它会触发 akeydown并释放它会触发 a keyup。的keypress通常是那些介于两者之间。

keydownand keyuptalk about which keyhas been changed. keypresstells which characterthat key represents.

keydownkeyup讨论更改了哪个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 keyCodeand charCode. Put (too) simply, keyCodesays something about the actual keyboard key the user pressed, while charCodegives 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 same keyCode, because the user presses the same key, but a different charCodebecause the resulting character is different.

Explorer and Opera do notsupport charCode. However, they give the character information in keyCode, but only with onkeypress. onkeydownand -upkeyCodecontains key information."

找到更多信息:

http://www.quirksmode.org/js/keys.html

“这两个属性是keyCodecharCode。简单地keyCode说(太),说明用户按下的实际键盘键,同时charCode给出结果字符的 ASCII 值。这些信息位不必相同;例如,小写'a' 和大写的 'A' 具有相同的keyCode,因为用户按下了相同的键,但是不同的charCode因为产生的字符不同。

Explorer 和 Opera支持charCode. 但是,它们在 中提供字符信息keyCode,但仅使用onkeypressonkeydown-upkeyCode包含关键信息。”

回答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:

原因:

  1. keydownkeeps firing when user holds the keys down, while keypressand keyupfire only once.
  2. keypressdoesn't detect special keys (e.g. SHIFT), keydownand keyupdo.
  1. keydown当用户按住键时保持发射,而keypresskeyup仅发射一次。
  2. 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 来禁止该字符。