diff --git a/Readme.md b/Readme.md index c14ccea..7d6e149 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,6 @@ ## JS键盘热键 > 支持各种按钮组合。 +>> 1.0版 功能键(Shift, Ctrl, Alt/Option, Win/Cmd)不区分左右。 ![keyboard](./keyboard.jpg) \ No newline at end of file diff --git a/src/index.es7 b/src/index.es7 index bf096a3..e4ed748 100644 --- a/src/index.es7 +++ b/src/index.es7 @@ -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