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键盘热键 ## JS键盘热键
> 支持各种按钮组合。 > 支持各种按钮组合。
>> 1.0版 功能键(Shift, Ctrl, Alt/Option, Win/Cmd)不区分左右。
![keyboard](./keyboard.jpg) ![keyboard](./keyboard.jpg)

View File

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