master
宇天 2020-11-05 22:39:52 +08:00
parent b1b60bee67
commit f1e63fe545
3 changed files with 28 additions and 5 deletions

View File

@ -59,6 +59,10 @@ kb.on(['ctrl + c', 'ctrl + v'], ev => {
### API
+ .disabled
> 禁止属性, 允许临时暂时热键.
+ .on(actions`<Array>`, callback`<Function>`)
> 监听键盘动作组合, 支持单组或双组。
>> 双组时, 2组按键前后时差不能超过`300毫秒`, 否则视为2次独立的操作。

View File

@ -1,13 +1,18 @@
{
"name": "@bytedo/keyboard",
"version": "1.1.0",
"version": "1.2.0",
"description": "支持各种按钮组合。原生js开发, 无任何依赖(不到2KB)。使用也非常简单。",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/bytedo/keyboard.git"
},
"keywords": ["keyboard", "hotkey", "shortcut", "yutent"],
"keywords": [
"keyboard",
"hotkey",
"shortcut",
"yutent"
],
"author": "yutent",
"license": "MIT",
"dependencies": {}

View File

@ -45,14 +45,20 @@ function check(ev) {
export default class Keyboard {
constructor(elem) {
console.log(elem)
this.$elem = elem
hide(this, '__EVENTS__', {})
hide(this, 'paused', false)
hide(
this,
'_keydown',
bind(elem, ev => {
// 允许暂停
if (this.paused) {
return
}
// 屏蔽纯辅助键的监听
if (MULTI_KEY_CODES.includes(ev.keyCode)) {
return
}
@ -74,7 +80,7 @@ export default class Keyboard {
if (tmp) {
end = true
// 第2组一定有回调,无需判断
next.fn.forEach(function(fn) {
next.fn.forEach(function (fn) {
fn(ev)
})
break
@ -92,7 +98,7 @@ export default class Keyboard {
}
// 有回调就触发
if (item.fn) {
item.fn.forEach(function(fn) {
item.fn.forEach(function (fn) {
fn(ev)
})
}
@ -107,6 +113,14 @@ export default class Keyboard {
)
}
get disabled() {
return this.paused
}
set disabled(v) {
this.paused = !!v
}
destroy() {
unbind(this.$elem, this._keydown)
delete this.__EVENTS__