![](/img/trans.png)
[英]event.keyCode is different in keydown/keyup and keypress
[英]Keydown and keypress events gives different keyCode
我已經閱讀了有關 Stack Overflow 的相關答案和文章,但仍然沒有明白:
在 Chrome 控制台上,如果我添加兩個偵聽器以在keypress
和keydown
事件上輸出keyCode
,當鍵為小寫時,我會得到不同的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 ?
那么這是正常行為嗎?
.NET 中的 KeyDown 和 KeyPress 有什么區別? ……解釋得很好。 keydown 只跟蹤密鑰本身,而不是密鑰的狀態。 如果您按A KeyDown 會生成 Keys.A 的 KeyCode,如果您按shift + A,您還會得到 Keys.A 的 KeyCode。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.