JavaScript键盘事件异常:按下Y键时event.code返回KeyZ而非KeyY
JavaScript键盘事件异常:event.code与物理按键不匹配
按下Ctrl+Shift+Y组合键时,event.key属性正确返回Y,但event.code属性错误返回KeyZ;单独按Y键时也会出现该问题,实际按下的是物理Y键。
测试代码
document.addEventListener('keydown', function(event) { console.log('Key:', event.key); // Character produced by the key console.log('Code:', event.code); // Physical key on the keyboard console.log('Ctrl:', event.ctrlKey); // Boolean for Ctrl key console.log('Shift:', event.shiftKey); // Boolean for Shift key });
测试环境
- 浏览器:Chrome、Firefox、Edge
- 操作系统:Windows
测试结果
在在线键盘测试工具中按下Ctrl+Shift+Y,得到以下输出:
Key: Y Code: KeyZ Ctrl: true Shift: true
预期输出
Key: Y Code: KeyY Ctrl: true Shift: true
内容的提问来源于stack exchange,提问作者smolo
相关产品推荐
相关产品推荐

