优化按键判断

master
宇天 2020-11-06 09:46:36 +08:00
parent f1e63fe545
commit 0ea3d62a33
2 changed files with 20 additions and 16 deletions

View File

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

View File

@ -35,8 +35,11 @@ function check(ev) {
} }
if (checked) { if (checked) {
for (let k of this.keys) { for (let k in this.keys) {
checked = ev[`${k}Key`] if (ev[k] !== this.keys[k]) {
checked = false
break
}
} }
} }
@ -133,27 +136,33 @@ export default class Keyboard {
var passed = true // 语法检测通过 var passed = true // 语法检测通过
act.forEach(it => { act.forEach(it => {
var tmp = [] var tmp = {}
dict.push(tmp)
it = it.split('+').map(k => { it = it.split('+').map(k => {
k = k.trim().toLowerCase() k = k.trim().toLowerCase()
if (MULTI_KEYS[k]) { if (MULTI_KEYS[k]) {
if (tmp.includes(k)) { if (tmp[`${k}Key`]) {
passed = false passed = false
console.error('功能键,同组中不能重复。⎣%s⎤', it) console.error('功能键,同组中不能重复。⎣%s⎤', it)
} else { } else {
tmp.push(k) tmp[`${k}Key`] = true
} }
} else { } else {
if (tmp.key) { if (tmp.key) {
passed = false passed = false
console.error('非功能键,同组不能出现多个。⎣%s⎤', it) console.error('非功能键,同组不能出现多个。⎣%s⎤', it)
} else { } else {
tmp.key = KEY_DICT[k] hide(tmp, 'key', KEY_DICT[k])
} }
} }
return k return k
}) })
for (let k in MULTI_KEYS) {
tmp[`${k}Key`] = tmp[`${k}Key`] || false
}
dict.push(tmp)
keys.push(it.join('+')) keys.push(it.join('+'))
}) })