-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyHandler.js
64 lines (59 loc) · 1.84 KB
/
keyHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let exec = require("child_process").exec,
config = require("./config.js"),
lastTime = {},
windowID = "unfilled",
throttledCommands = config.throttledCommands,
regexThrottle = new RegExp("^(" + throttledCommands.join("|") + ")$", "i"),
regexFilter = new RegExp("^(" + config.filteredCommands.join("|") + ")$","i");
let isWindows = process.platform === "win32";
(function setWindowID() {
if (!isWindows & windowID === "unfilled") {
exec("xdotool search --onlyvisible --name " + config.programName, function (
error,
stdout
) {
windowID = stdout.trim();
// console.log(key, windowID);
});
}
})();
for (let i = 0; i < throttledCommands.length; i++) {
lastTime[throttledCommands[i]] = new Date().getTime();
}
function sendKey(command, holdTime) {
//if doesn't match the filtered words
if (!command.match(regexFilter)) {
let allowKey = true;
let key = config.keymap[command];
//throttle certain commands (not individually though)
if (key.match(regexThrottle)) {
// TODO: get the hold time
let newTime = new Date().getTime();
if (newTime - lastTime[key] < config.timeToWait) {
allowKey = false;
} else {
lastTime = newTime;
}
}
if (allowKey) {
if (isWindows) {
//use python on windows
// "VisualBoyAdvance"
// "DeSmuME 0.9.10 x64"
console.log('issuing command: ', "python key.py" + " " + config.programName + " " + key + " " + holdTime)
exec("python key.py" + " " + config.programName + " " + key + " " + holdTime);
} else {
//Send to preset window under non-windows systems
exec(
"xdotool key --window " +
windowID +
" --delay " +
config.delay +
" " +
key
);
}
}
}
}
exports.sendKey = sendKey;