ninwa/libs/keyfilter.js

196 lines
6.7 KiB
JavaScript
Raw Normal View History

2022-03-03 03:35:34 +01:00
const LINE_START = 'Event: time';
2022-02-15 04:33:19 +01:00
const ACTION_KEYUP = { id: 0, action: 'keyup' };
const ACTION_KEYDOWN = { id: 1, action: 'keydown' };
const ACTION_KEYHOLD = { id: 2, action: 'keyhold' };
const VARIABLE_KEY = '{{ key }}';
const VARIABLE_TYPE = '{{ type }}';
2022-02-15 04:33:19 +01:00
class Keyfilter {
2022-03-03 03:35:34 +01:00
constructor(keys, combos) {
if ((keys === undefined || keys.length === 0) && (combos === undefined || combos.length === 0)) {
2022-02-15 04:33:19 +01:00
return;
}
this.actions = new Map();
2022-03-03 03:35:34 +01:00
for (var index = 0; index < keys.length; index++) {
this.setAction(keys[index]);
2022-02-15 04:33:19 +01:00
}
2022-03-03 03:35:34 +01:00
this.currentCombo = undefined;
2022-02-15 04:33:19 +01:00
}
2022-03-03 03:35:34 +01:00
setAction(config) {
var type = ACTION_KEYDOWN;
switch (config.type.toLowerCase()) {
case ACTION_KEYUP.action:
type = ACTION_KEYUP;
break;
case ACTION_KEYHOLD.action:
type = ACTION_KEYHOLD;
break;
2022-02-15 04:33:19 +01:00
}
2022-03-03 03:35:34 +01:00
this.actions.set(config.key.toUpperCase(),
{
type: type,
event: config.event,
command: config.command,
args: config.args,
combo: config.combo,
delay: function () {
if (config.combo === undefined) {
return config.delay;
}
return config.delay || 1000;
}(),
}
);
2022-02-15 04:33:19 +01:00
}
filter(input) {
2022-03-03 03:35:34 +01:00
if (input === undefined || input.length === 0) {
2022-02-15 04:33:19 +01:00
return;
}
input = input.toString();
var lines = input.split("\n");
for (var index = 0; index < lines.length; index++) {
var line = lines[index];
2022-03-03 03:35:34 +01:00
if (line.length === 0) {
continue;
}
if (!line.startsWith(LINE_START)) {
2022-02-15 04:33:19 +01:00
continue;
}
2022-03-03 03:35:34 +01:00
const parsedEvent = this.parseLine(line);
if (parsedEvent === undefined) {
continue;
}
for (var [key, event] of this.actions) {
if (this.currentCombo === undefined && !this.isParsedEventValid(key, event, parsedEvent)) {
2022-02-15 04:33:19 +01:00
continue;
}
2022-03-03 03:35:34 +01:00
if (this.isStartOfCombo(key, event)) {
return this.getFilterResult(key, event, 'combo', this.currentCombo);
}
if (this.isPartOfCombo(parsedEvent)) {
if (parsedEvent.ignore) {
continue;
}
const result = this.getFilterResult(key, event, 'combo', this.currentCombo);
if (this.hasComboFinished()) {
this.resetCurrentCombo();
}
return result;
}
if (!this.isParsedEventValid(key, event, parsedEvent)) {
2022-02-15 04:33:19 +01:00
continue;
}
2022-03-03 03:35:34 +01:00
if (this.shouldBeDelayed(event)) {
return this.getFilterResult(key, event, 'delayed', true);
2022-02-15 04:33:19 +01:00
}
2022-03-03 03:35:34 +01:00
event.captured = new Date().getTime();
return this.getFilterResult(key, event);
2022-02-15 04:33:19 +01:00
}
}
}
2022-03-03 03:35:34 +01:00
isParsedEventValid(key, value, parsed) {
return key === parsed.key && (value.event === undefined || value.event === parsed.event) && value.type.id === parsed.type;
}
getFilterResult(key, event, extraName, extra) {
if (key === undefined || event === undefined) {
return;
}
let result = this.replaceVariables(
{
key: key,
type: event.type.action,
command: event.command,
args: event.args,
delay: event.delay
}
)
if (extraName !== undefined && extra !== undefined) {
result[extraName] = extra;
}
return result;
}
parseLine(line) {
try {
const parts = line.split(',');
const event = parts[1].substring(parts[1].indexOf('(') + 1, parts[1].lastIndexOf(')'));
const key = parts[2].substring(parts[2].indexOf('(') + 1, parts[2].indexOf(')'));
const type = parseInt(parts[3].split(' ').pop());
return { event: event, key: key, type: type };
} catch (err) {
return;
}
}
replaceVariables(filtered) {
for (var index = 0; index < filtered.args.length; index++) {
filtered.args[index] = filtered.args[index].replace(VARIABLE_KEY, filtered.key);
filtered.args[index] = filtered.args[index].replace(VARIABLE_TYPE, filtered.type);
}
return filtered;
}
2022-03-03 03:35:34 +01:00
shouldBeDelayed(event) {
if (event.delay === undefined || event.delay === 0 || event.captured === undefined) {
return false;
}
return new Date().getTime() - event.captured < event.delay;
}
isStartOfCombo(key, event) {
if (this.currentCombo !== undefined || event.combo === undefined) {
return false;
}
this.currentCombo = {
key: key,
type: event.type,
start: new Date().getTime(),
done: [key],
remaining: JSON.parse(JSON.stringify(event.combo)),
finished: this.hasComboFinished()
};
return true;
}
isPartOfCombo(parsedEvent) {
if (this.hasComboTimedOut()) {
this.resetCurrentCombo();
}
if (this.currentCombo === undefined) {
return false;
}
if (this.currentCombo.done.includes(parsedEvent.key)) {
parsedEvent.ignore = true;
return true;
}
if (this.currentCombo.type.id !== parsedEvent.type) {
2022-02-15 04:33:19 +01:00
return false;
}
2022-03-03 03:35:34 +01:00
if (this.currentCombo.remaining[0].toUpperCase() !== parsedEvent.key) {
return false;
}
this.currentCombo.key = parsedEvent.key;
this.currentCombo.done.push(parsedEvent.key);
this.currentCombo.remaining.shift();
this.currentCombo.finished = this.hasComboFinished();
return true;
}
hasComboFinished() {
return this.currentCombo !== undefined &&
this.currentCombo.remaining !== undefined &&
this.currentCombo.remaining.length === 0;
}
hasComboTimedOut() {
return this.currentCombo !== undefined &&
this.currentCombo.delay !== undefined &&
this.currentCombo.start !== undefined &&
new Date().getTime() - this.currentCombo.start > this.currentCombo.delay;
}
resetCurrentCombo() {
this.currentCombo = undefined;
2022-02-15 04:33:19 +01:00
}
isValid() {
return this.actions != undefined && this.actions.size > 0;
}
2022-02-15 04:33:19 +01:00
}
2022-03-03 03:35:34 +01:00
2022-02-15 04:33:19 +01:00
module.exports = Keyfilter;