jaywcjlove / hotkeys-js

➷ A robust Javascript library for capturing keyboard input. It has no dependencies.

Home Page:https://jaywcjlove.github.io/hotkeys-js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

YuxuanWeng opened this issue · comments

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

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

image

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

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

hotkeys-js/src/index.js

Lines 231 to 237 in 031bc4d

/**
* Collect bound keys
* If an Input Method Editor is processing key input and the event is keydown, return 229.
* https://stackoverflow.com/questions/25043934/is-it-ok-to-ignore-keydown-events-with-keycode-229
* http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html
*/
if (_downKeys.indexOf(key) === -1 && key !== 229) _downKeys.push(key);

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