jaywcjlove / hotkeys-js

➷ A robust Javascript library for capturing keyboard input. It has no dependencies.
https://jaywcjlove.github.io/hotkeys-js
MIT License
6.57k stars 405 forks source link

下一版可以用event.code代替event.keyCode吗? #445

Open YuxuanWeng opened 11 months ago

YuxuanWeng commented 11 months ago

目前keyCode已经废弃蛮久的了(https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/keyCode) keyCode目前发现,在input中使用shift+数字/字母的快捷键是,会遇到keyCode===229的情况,以及shift+小键盘按键的时候也会出现识别问题

jaywcjlove commented 11 months ago

@YuxuanWeng 下一个版本会舍弃一些兼容代码,但是 event.code 代替 event.keyCode 还需要斟酌

image
YuxuanWeng commented 11 months ago

哦哦,好的,那有没有在event.code空的时候再使用keyCode呢?我目前是单对229的场景做了下兼容

  var key = event.keyCode || event.which || event.charCode;
  if (key === 229) {
    key = codeToKeyCodeMap[event.code] || key;
  }
jaywcjlove commented 11 months ago

https://github.com/jaywcjlove/hotkeys-js/blob/031bc4d5da583ca07f1e7b26bce83d3da409c6ad/src/index.js#L231-L237

@YuxuanWeng 是不是上面的问题,目前没有办法测试数字键的问题