fixed combo capture / reset

This commit is contained in:
Daniel Sommer 2022-03-28 16:08:33 +02:00
parent eccbc863b3
commit c13b123423
4 changed files with 31 additions and 28 deletions

4
.vscode/launch.json vendored
View file

@ -10,7 +10,9 @@
"<node_internals>/**"
],
"program": "${workspaceFolder}/ninwa.js",
"args": []
"args": [
"example_config.json"
]
}
]
}

View file

@ -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",

View file

@ -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() {

View file

@ -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);