master
宇天 2020-11-02 08:58:52 +08:00
parent 618d40b9fc
commit b68974c6f0
2 changed files with 22 additions and 7 deletions

View File

@ -1,5 +1,6 @@
## JS键盘热键
> 支持各种按钮组合。
>> 1.0版 功能键(Shift, Ctrl, Alt/Option, Win/Cmd)不区分左右。
![keyboard](./keyboard.jpg)

View File

@ -8,23 +8,34 @@ import { KEY_DICT, MULTI_KEYS } from './key.dict.js'
var log = console.log
function keydown(ev) {
var { code, keyCode } = ev
log(ev.keyCode, ev.code, ev.target)
function bind(fn) {
document.addEventListener('keydown', fn, false)
return fn
}
function createCheckFunc(act) {}
function unbind(fn) {
document.removeEventListener('keydown', fn, false)
}
export default class Keyboard {
constructor() {
this.__EVENTS__ = {}
document.addEventListener('keydown', keydown, false)
this._keydown = bind(ev => {
for (let k in this.__EVENTS__) {
var item = this.__EVENTS__[k]
if (item.check(ev)) {
item.fn.forEach(function(fn) {
fn(ev)
})
break
}
}
})
}
destroy() {
delete this.__EVENTS__
document.removeEventListener('keydown', keydown, false)
unbind(this._keydown)
}
on(act, callback) {
@ -56,6 +67,8 @@ export default class Keyboard {
var action = actions.shift()
var checked = false
log('check: ', keyCode, code, ev)
while (action) {
if (now === 0) {
} else {
@ -66,6 +79,7 @@ export default class Keyboard {
}
now = _now
}
action = actions.shift()
}
return checked