You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 15:29:54