这三件事有什么不同?在谷歌上我发现:
当用户按下一个键时,onKeyDown事件被触发。 当用户释放密钥时触发onKeyUp事件。 当用户按下并释放一个键时触发onKeyPress事件 (onKeyDown紧接着onKeyUp)。
我理解前两个,但不是onKeyPress相同的onKeyUp?是否有可能释放一个键(onKeyUp)而不按它(onKeyDown)?
这有点混乱,有人能帮我解释一下吗?
这三件事有什么不同?在谷歌上我发现:
当用户按下一个键时,onKeyDown事件被触发。 当用户释放密钥时触发onKeyUp事件。 当用户按下并释放一个键时触发onKeyPress事件 (onKeyDown紧接着onKeyUp)。
我理解前两个,但不是onKeyPress相同的onKeyUp?是否有可能释放一个键(onKeyUp)而不按它(onKeyDown)?
这有点混乱,有人能帮我解释一下吗?
当前回答
只是想分享一个好奇心:
当使用onkeydown事件来激活一个JS方法时,该事件的charcode与你使用onkeypress得到的不一样!
例如,当使用onkeypress时,numpad键将返回与字母键上方的数字键相同的字符代码,但当使用onkeydown时则不是!
我花了相当多的时间来弄清楚为什么我的脚本在使用onkeydown时检查某些字符码失败!
演示:https://www.w3schools.com/code/tryit.asp?filename=FMMBXKZLP1MK
是的。我知道方法的定义是不同的。但非常令人困惑的是,在这两个方法中,事件的结果是使用event. keycode ..但是它们不会返回相同的值。不是一个非常声明性的实现。
其他回答
当键按下时,Onkeydown被触发(就像在快捷键;例如,在Ctrl+A中,按下Ctrl键。
Onkeyup在按键释放时被触发(包括modifier/etc按键)
Onkeypress被触发为onkeydown和onkeyup的组合,或者取决于键盘重复(当onkeyup没有被触发时)。(我还没有测试过这种重复行为。如果要测试,请添加注释!)
textInput(仅webkit)在输入一些文本时被触发(例如,Shift+A将输入大写的“A”,但Ctrl+A将选择文本而不输入任何文本输入。在这种情况下,所有其他事件被触发)
一些实际的事实可能有助于决定要处理哪个事件(运行下面的脚本并关注输入框):
“输入”(美元)。上(“钥匙键盘”,e=>控制台。打字,e.keyCode, e. yg, e.key) <剧本剧本src = " https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js " > < / > 输入< - >
压:
非插入/键入键(例如Shift, Ctrl)将不会触发按键。按Ctrl并释放它: 按键17 17控制 按键17 17控制 将字符转换应用到其他字符的键盘上的键可能会导致死键和重复的“键”(例如~,´)。按´并释放它,以显示双重´´: 死亡192 按键192 192´´ 按键180°180° 按键180°180° 死了192
此外,非键入输入(例如,rang <input type="range">)仍然会根据按下的键触发所有的keyup, keydown和keypress事件。
“KeyPress”现已弃用。请使用KeyDown键。
KeyPress, KeyUp和KeyDown分别类似于:Click, MouseUp和MouseDown。
先下 第二个按键发生(当输入文本时) Up发生在最后(当文本输入完成时)。
webkit是个例外,它有一个额外的事件:
keydown
keypress
textInput
keyup
下面是一个片段,你可以自己看看什么时候事件被触发:
窗口。addEventListener(“弹起”,日志); 窗口。addEventListener(“键盘按键”,日志); 窗口。addEventListener(“keydown”、日志); 对数函数(事件){ console.log(事件。类型); }
只是想分享一个好奇心:
当使用onkeydown事件来激活一个JS方法时,该事件的charcode与你使用onkeypress得到的不一样!
例如,当使用onkeypress时,numpad键将返回与字母键上方的数字键相同的字符代码,但当使用onkeydown时则不是!
我花了相当多的时间来弄清楚为什么我的脚本在使用onkeydown时检查某些字符码失败!
演示:https://www.w3schools.com/code/tryit.asp?filename=FMMBXKZLP1MK
是的。我知道方法的定义是不同的。但非常令人困惑的是,在这两个方法中,事件的结果是使用event. keycode ..但是它们不会返回相同的值。不是一个非常声明性的实现。
这篇由Jan Wolter撰写的文章是我所见过的最好的文章,如果链接失效,你可以在这里找到存档副本。
它很好地解释了所有浏览器键事件,
The keydown event occurs when the key is pressed, followed immediately by the keypress event. Then the keyup event is generated when the key is released. To understand the difference between keydown and keypress, it is useful to distinguish between characters and keys. A key is a physical button on the computer's keyboard. A character is a symbol typed by pressing a button. On a US keyboard, hitting the 4 key while holding down the Shift key typically produces a "dollar sign" character. This is not necessarily the case on every keyboard in the world. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. In practice, this is not always the way it is implemented. For a while, some browers fired an additional event, called textInput, immediately after keypress. Early versions of the DOM 3 standard intended this as a replacement for the keypress event, but the whole notion was later revoked. Webkit supported this between versions 525 and 533, and I'm told IE supported it, but I never detected that, possibly because Webkit required it to be called textInput while IE called it textinput. There is also an event called input, supported by all browsers, which is fired just after a change is made to to a textarea or input field. Typically keypress will fire, then the typed character will appear in the text area, then input will fire. The input event doesn't actually give any information about what key was typed - you'd have to inspect the textbox to figure it out what changed - so we don't really consider it a key event and don't really document it here. Though it was originally defined only for textareas and input boxes, I believe there is some movement toward generalizing it to fire on other types of objects as well.