Skip to content

Commit 268ef21

Browse files
committed
feat: robotjs to nut.js
1 parent b55a555 commit 268ef21

3 files changed

Lines changed: 1386 additions & 449 deletions

File tree

electron/mapi/manager/automation/index.ts

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import {Button, Key, keyboard, mouse} from "@nut-tree-fork/nut-js";
12
import {activeWindow, Result} from "get-windows";
23
import {windowManager} from "node-window-manager";
4+
import {Window} from "node-window-manager/src/classes/window";
35
import {ActiveWindow} from "../../../../src/types/Manager";
4-
import robot, {Keys} from "@hurdlegroup/robotjs";
56
import {Log} from "../../log/main";
6-
import {Window} from "node-window-manager/src/classes/window";
77

88
export const ManagerAutomation = {
99
init() {
@@ -63,18 +63,109 @@ export const ManagerAutomation = {
6363
return win;
6464
},
6565
async typeString(text: string): Promise<void> {
66-
robot.typeString(text);
66+
await keyboard.type(text);
6767
},
68-
async typeKey(key: Keys): Promise<void> {
69-
robot.keyTap(key);
68+
async typeKey(key: string): Promise<void> {
69+
const keyMap: { [key: string]: Key } = {
70+
'a': Key.A,
71+
'b': Key.B,
72+
'c': Key.C,
73+
'd': Key.D,
74+
'e': Key.E,
75+
'f': Key.F,
76+
'g': Key.G,
77+
'h': Key.H,
78+
'i': Key.I,
79+
'j': Key.J,
80+
'k': Key.K,
81+
'l': Key.L,
82+
'm': Key.M,
83+
'n': Key.N,
84+
'o': Key.O,
85+
'p': Key.P,
86+
'q': Key.Q,
87+
'r': Key.R,
88+
's': Key.S,
89+
't': Key.T,
90+
'u': Key.U,
91+
'v': Key.V,
92+
'w': Key.W,
93+
'x': Key.X,
94+
'y': Key.Y,
95+
'z': Key.Z,
96+
'0': Key.Num0,
97+
'1': Key.Num1,
98+
'2': Key.Num2,
99+
'3': Key.Num3,
100+
'4': Key.Num4,
101+
'5': Key.Num5,
102+
'6': Key.Num6,
103+
'7': Key.Num7,
104+
'8': Key.Num8,
105+
'9': Key.Num9,
106+
'space': Key.Space,
107+
'enter': Key.Enter,
108+
'tab': Key.Tab,
109+
'backspace': Key.Backspace,
110+
'delete': Key.Delete,
111+
'escape': Key.Escape,
112+
'shift': Key.LeftShift,
113+
'control': Key.LeftControl,
114+
'alt': Key.LeftAlt,
115+
'command': Key.LeftSuper,
116+
'left': Key.Left,
117+
'right': Key.Right,
118+
'up': Key.Up,
119+
'down': Key.Down,
120+
'f1': Key.F1,
121+
'f2': Key.F2,
122+
'f3': Key.F3,
123+
'f4': Key.F4,
124+
'f5': Key.F5,
125+
'f6': Key.F6,
126+
'f7': Key.F7,
127+
'f8': Key.F8,
128+
'f9': Key.F9,
129+
'f10': Key.F10,
130+
'f11': Key.F11,
131+
'f12': Key.F12
132+
};
133+
const nutKey = keyMap[key.toLowerCase()];
134+
if (nutKey) {
135+
await keyboard.pressKey(nutKey);
136+
}
70137
},
71138
async mouseToggle(type: 'down' | 'up', button: 'left' | 'right' | 'middle'): Promise<void> {
72-
robot.mouseToggle(type, button);
139+
const buttonMap: { [key: string]: Button } = {
140+
'left': Button.LEFT,
141+
'right': Button.RIGHT,
142+
'middle': Button.MIDDLE
143+
};
144+
const nutButton = buttonMap[button];
145+
if (nutButton) {
146+
if (type === 'down') {
147+
await mouse.pressButton(nutButton);
148+
} else {
149+
await mouse.releaseButton(nutButton);
150+
}
151+
}
73152
},
74153
async moveMouse(x: number, y: number): Promise<void> {
75-
robot.moveMouse(x, y);
154+
await mouse.setPosition({x, y});
76155
},
77156
async mouseClick(button: 'left' | 'right' | 'middle', double: boolean = false): Promise<void> {
78-
robot.mouseClick(button, double);
157+
const buttonMap: { [key: string]: Button } = {
158+
'left': Button.LEFT,
159+
'right': Button.RIGHT,
160+
'middle': Button.MIDDLE
161+
};
162+
const nutButton = buttonMap[button];
163+
if (nutButton) {
164+
if (double) {
165+
await mouse.doubleClick(nutButton);
166+
} else {
167+
await mouse.click(nutButton);
168+
}
169+
}
79170
}
80171
};

0 commit comments

Comments
 (0)