-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
93 lines (77 loc) · 1.83 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const { app, ipcMain, BrowserWindow } = require("electron");
// two ways to control keyboard
const robot = require("robotjs");
const { keyboard, Key } = require("@nut-tree/nut-js");
let win = null;
app.whenReady().then(() => {
win = createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", function () {
console.log("Bye");
app.quit();
});
function createWindow() {
const win = new BrowserWindow({
width: 640,
height: 480,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
win.loadFile("index.html");
return win;
}
ipcMain.on("send-keys", async () => {
await sendKeys();
});
async function sendKeys() {
// Wait a bit to change focus
const delay = 3;
win.webContents.send("sending-keys");
await sleep(delay);
// send keys using desired module
//await typeRobot();
//await pressRobot();
//await typeNutJS();
await pressNutJS();
win.webContents.send("sent-keys");
}
async function sleep(seconds) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000.0));
}
async function typeRobot() {
robot.setKeyboardDelay(10);
robot.keyTap("r");
robot.keyTap("o");
robot.keyTap("b");
robot.keyTap("o");
robot.keyTap("t");
}
// this doesn't actually hold the key down
async function pressRobot() {
robot.keyToggle("w", "down");
await sleep(2);
robot.keyToggle("w", "up");
robot.keyToggle("s", "down");
await sleep(2);
robot.keyToggle("s", "up");
}
async function typeNutJS() {
keyboard.config.autoDelayMs = 10;
await keyboard.type("nut.js");
}
// this doesn't actually hold the key down
async function pressNutJS() {
await keyboard.pressKey(Key.W);
await sleep(2);
await keyboard.releaseKey(Key.W);
await keyboard.pressKey(Key.S);
await sleep(2);
await keyboard.releaseKey(Key.S);
}