From b68974c6f06dcd8f384fdfe1f38c3b26e9615167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Mon, 2 Nov 2020 08:58:52 +0800 Subject: [PATCH] update --- Readme.md | 1 + src/index.es7 | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) 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