From c13b123423d7464bc6fd4baf1bc2121d2a7b0451 Mon Sep 17 00:00:00 2001 From: velvettear Date: Mon, 28 Mar 2022 16:08:33 +0200 Subject: [PATCH] fixed combo capture / reset --- .vscode/launch.json | 4 +++- example_config.json | 6 +++--- libs/keyfilter.js | 39 +++++++++++++++++++++------------------ ninwa.js | 10 ++++------ 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index df55f29..59c45d7 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,9 @@ "/**" ], "program": "${workspaceFolder}/ninwa.js", - "args": [] + "args": [ + "example_config.json" + ] } ] } \ No newline at end of file diff --git a/example_config.json b/example_config.json index c3574bb..bafaf44 100644 --- a/example_config.json +++ b/example_config.json @@ -16,7 +16,7 @@ "key": "key_f1", "combo": [ "key_f2", - "key_f4" + "key_f2" ], "event": "EV_KEY", "type": "keydown", @@ -25,8 +25,8 @@ { "key": "key_f1", "combo": [ - "key_f1", - "key_f2" + "key_f2", + "key_f3" ], "event": "EV_KEY", "type": "keydown", diff --git a/libs/keyfilter.js b/libs/keyfilter.js index 186d93e..ecf271d 100644 --- a/libs/keyfilter.js +++ b/libs/keyfilter.js @@ -1,6 +1,3 @@ -const comboConfig = require('../config.json').combos; -const commandConfig = require('../config.json').commands; - const LINE_START = 'Event: time'; const ACTION_KEYUP = { id: 0, action: 'keyup' }; @@ -19,10 +16,10 @@ class Keyfilter { this.currentCombo = undefined; } getCommand(command) { - if (command === undefined || commandConfig === undefined) { + if (command === undefined || global.config?.commands === undefined) { return; } - const result = commandConfig[command]; + const result = global.config.commands[command]; if (result === undefined) { return; } @@ -119,11 +116,22 @@ class Keyfilter { } } isParsedEventValid(key, value, parsed) { - let keyCheck = key === parsed.key; - if (value.combo !== undefined && value.combo.length > 0) { - keyCheck = key.includes(parsed.key); + if (value.event !== parsed.event || value.type.id !== parsed.type.id) { + return false; } - return keyCheck && (value.event === undefined || value.event === parsed.event) && value.type.id === parsed.type.id; + if (value.combo === undefined || value.combo.length === 0) { + return key === parsed.key; + } + if (this.currentCombo === undefined) { + return key.startsWith(parsed.key); + } + for (let index = 0; index < this.currentCombo.possibilities.length; index++) { + if (this.currentCombo.possibilities[index].combo[0].toUpperCase() === parsed.key) { + this.resetCurrentCombo(); + return true; + } + } + return false; } setComboResult(result) { if (result === undefined || this.currentCombo === undefined) { @@ -201,10 +209,6 @@ class Keyfilter { if (this.currentCombo === undefined || this.currentCombo.type.id !== parsedEvent.type.id || this.currentCombo.event !== parsedEvent.event) { return false; } - // if (this.currentCombo.done.includes(parsedEvent.key)) { - // parsedEvent.ignore = true; - // return true; - // } let possibilities = []; for (let index = 0; index < this.currentCombo.possibilities.length; index++) { const possibility = this.currentCombo.possibilities[index]; @@ -236,13 +240,12 @@ class Keyfilter { } hasComboTimedOut() { return false; - return comboConfig !== undefined && - comboConfig.delay !== undefined && - this.currentCombo !== undefined && - this.currentCombo.timestamp !== undefined && - new Date().getTime() - this.currentCombo.timestamp > comboConfig.delay; + return global.config?.combos?.delay !== undefined && + this.currentCombo?.timestamp !== undefined && + new Date().getTime() - this.currentCombo.timestamp > global.config?.combos?.dela; } resetCurrentCombo() { + console.debug('resetting current combo...'); this.currentCombo = undefined; } isValid() { diff --git a/ninwa.js b/ninwa.js index 95ed5ff..795109e 100644 --- a/ninwa.js +++ b/ninwa.js @@ -5,24 +5,22 @@ const packageJSON = require('./package.json'); const INTERRUPTS = ['SIGINT', 'SIGTERM']; -let config; - main(); async function main() { handleInterrupts(); try { let configFile = await util.getFileInfo(process.argv[2] || __dirname + '/config.json'); - config = require(configFile.path); - config.path = config.path; + global.config = require(configFile.path); + global.config.path = configFile.path; } catch (err) { console.error(err); process.exit(1); } try { - logger.initialize(config.log.level, config.log.timestamp); + logger.initialize(global.config.log.level, global.config.log.timestamp); logger.info(packageJSON.name + ' ' + packageJSON.version + ' starting...'); - await watchers.initialize(config.watchers); + await watchers.initialize(global.config.watchers); await watchers.start(); } catch (err) { logger.error(err);