Keydown和keypress事件提供了不同的keyCode

问题描述 投票:0回答:1

我已经阅读了有关堆栈溢出的相关答案和文章,但仍然没有得到:

在Chrome控制台上,如果我添加两个侦听器以在keyCodekeypress事件上输出keydown,则在键为小写字母时会得到不同的keycode

但是为大写时,两个事件的keyCode似乎相同。

示例:

document.addEventListener('keypress', function(e){ console.log('keyPress', e.keyCode); });
document.addEventListener('keydown', function(e){ console.log('keyDown', e.keyCode); });

// Open your console  
// Typing 'a' in the result field outputs 'keyPress 97' 'keyDown 65'
// on chrome 42 console. Activate uppercase, and then typing 'A' outputs 'keyPress 65' and 'keyDown 65'

// Why ? 

那么这是正常行为吗?

javascript dom-events keypress keydown keycode
1个回答
0
投票
What's the difference between KeyDown and KeyPress in .NET?…很好地解释了。 keydown仅跟踪密钥本身,而不跟踪密钥状态。如果按下

A,则KeyDown会生成Keys.A的密钥代码;如果按下shift + A,您还将获得Keys.A的密钥代码。

© www.soinside.com 2019 - 2024. All rights reserved.