javascript 为什么“keydown”事件像“keypress”事件一样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15310647/
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
Why 'keydown' event works like 'keypress' event?
提问by sergzach
The next sample code outputs 'keydown' message many times while I hold a button down. The docs saysthat the keydown event happens once for one push of the button. So, the keydown event works like the keypress event in the next example.
下一个示例代码在我按住按钮时多次输出“keydown”消息。文档说,按一下按钮,keydown 事件就会发生一次。因此,keydown 事件的工作方式类似于下一个示例中的 keypress 事件。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title></title>
<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript'>
function onLoad()
{
$( '#text' ).on( 'keydown', function() { console.info( 'keydown' ) } );
}
</script>
</head>
<body onload='onLoad()'>
<input type='text' id='text'>
</body>
</html>
I tested it on Windows, Firefox 19.0.2 and Google Chrome 25.0.1364.152. Also I created a fiddle(the problem can be reproduced). JQuery versions for which problem is reproduced: 1.8.2, 1.9.1.
我在 Windows、Firefox 19.0.2 和 Google Chrome 25.0.1364.152 上对其进行了测试。我还创建了一个小提琴(问题可以重现)。重现问题的 JQuery 版本:1.8.2、1.9.1。
Update.
更新。
I did realize the problem: How can I avoid autorepeated keydown events in JavaScript?.
我确实意识到了这个问题:如何避免 JavaScript 中的自动重复按键事件?.
回答by deadlock
The
keydown
event occurs when the key is pressed, followed immediately by thekeypress
event. Then thekeyup
event is generated when the key is released.In order to understand the difference between
keydown
andkeypress
, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button. In theory, thekeydown
andkeyup
events represent keys being pressed or released, while thekeypress
event represents a character being typed. The implementation of the theory is not same in all browsers.
keydown
当按键被按下时发生该事件,紧接着是该keypress
事件。然后在keyup
释放键时生成事件。为了了解之间的区别
keydown
和keypress
,它是有用的了解“性格”和“重点”之间的区别。“键”是计算机键盘上的物理按钮,而“字符”是按按钮键入的符号。理论上,keydown
和keyup
事件代表按键被按下或释放,而keypress
事件代表正在输入一个字符。该理论的实现在所有浏览器中并不相同。
回答by Umair Saleem
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.
KeyPress 事件不是由非字符键引发的;但是,非字符键确实会引发 KeyDown 和 KeyUp 事件。
Key events occur in the following order:
关键事件按以下顺序发生:
- KeyDown
- KeyPress
- KeyUp
- 按键按下
- 按键
- 上键
Are these events available on the Window, Document, Form, focusable elements
Test Page
回答by Mario Araque
The keydown event occurs when the key is pressed, followed immediately by the keypress event.
keydown 事件在按键被按下时发生,紧接着是 keypress 事件。
回答by Saad Laggoune
i have answer here, following me slowly:
keydown
: working when you start typing key or start to change something and keydown give you one result for one press but give you the result before the last one not last result.
keyup
: working when you stop typing a key or go back (change something like remove character) and it's give give you last result.
keypress
: working when you start typing key but not start to change something and keydpress give you one result for one press but give you the result before the last ,which (lastResult - 1)
我在这里有答案,慢慢地跟着我::
keydown
当您开始键入 key 或开始更改某些内容和 keydown 时工作,按一下会给您一个结果,但在最后一个结果之前给您一个结果,而不是最后一个结果。
keyup
:当您停止键入键或返回(更改诸如删除字符之类的内容)时工作,它会给您最后的结果。
keypress
:当您开始键入 key 但不开始更改某些内容时工作,并且 keydpress 会为您提供一次按一次的结果,但会在最后一次之前为您提供结果,其中 (lastResult - 1)