-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathmouse.class.ts
264 lines (246 loc) · 7.71 KB
/
mouse.class.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { Button } from "./button.enum";
import { isPoint, Point } from "./point.class";
import { busyWaitForNanoSeconds, sleep } from "./sleep.function";
import {
calculateMovementTimesteps,
EasingFunction,
linear,
} from "./mouse-movement.function";
import { ProviderRegistry } from "./provider/provider-registry.class";
/**
* Config object for {@link MouseClass} class
*/
export interface MouseConfig {
/**
* Configures the delay between single mouse events
*/
autoDelayMs: number;
/**
* Configures the speed in pixels/second for mouse movement
*/
mouseSpeed: number;
}
/**
* {@link MouseClass} class provides methods to emulate mouse input
*/
export class MouseClass {
public config: MouseConfig = {
autoDelayMs: 100,
mouseSpeed: 1000,
};
/**
* {@link MouseClass} class constructor
* @param providerRegistry
*/
constructor(private providerRegistry: ProviderRegistry) {
this.providerRegistry.getMouse().setMouseDelay(0);
}
/**
* {@link setPosition} instantly moves the mouse cursor to a given {@link Point}
* @param target {@link Point} to move the cursor to
*/
public async setPosition(target: Point): Promise<MouseClass> {
if (!isPoint(target)) {
throw Error(
`setPosition requires a Point, but received ${JSON.stringify(target)}`
);
}
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await this.providerRegistry.getMouse().setMousePosition(target);
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link getPosition} returns a {@link Point} representing the current mouse position
*/
public getPosition(): Promise<Point> {
return this.providerRegistry.getMouse().currentMousePosition();
}
/**
* {@link move} moves the mouse cursor along a given path of {@link Point}s, according to a movement type
* @param path Array of {@link Point}s to follow
* @param movementType Defines the type of mouse movement. Would allow to configured acceleration etc. (Default: {@link linear}, no acceleration)
*/
public async move(
path: Point[] | Promise<Point[]>,
movementType: EasingFunction = linear
): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
const pathSteps = await path;
const timeSteps = calculateMovementTimesteps(
pathSteps.length,
this.config.mouseSpeed,
movementType
);
for (let idx = 0; idx < pathSteps.length; ++idx) {
const node = pathSteps[idx];
const minTime = timeSteps[idx];
await busyWaitForNanoSeconds(minTime);
await this.setPosition(node);
}
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link leftClick} performs a click with the left mouse button
*/
public async leftClick(): Promise<MouseClass> {
return this.click(Button.LEFT);
}
/**
* {@link rightClick} performs a click with the right mouse button
*/
public async rightClick(): Promise<MouseClass> {
return this.click(Button.RIGHT);
}
/**
* {@link scrollDown} scrolls down for a given amount of "steps"
* Please note that the actual scroll distance of a single "step" is OS dependent
* @param amount The amount of "steps" to scroll
*/
public async scrollDown(amount: number): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().scrollDown(amount);
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link scrollUp} scrolls up for a given amount of "steps"
* Please note that the actual scroll distance of a single "step" is OS dependent
* @param amount The amount of "steps" to scroll
*/
public async scrollUp(amount: number): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().scrollUp(amount);
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link scrollLeft} scrolls left for a given amount of "steps"
* Please note that the actual scroll distance of a single "step" is OS dependent
* @param amount The amount of "steps" to scroll
*/
public async scrollLeft(amount: number): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().scrollLeft(amount);
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link scrollRight} scrolls right for a given amount of "steps"
* Please note that the actual scroll distance of a single "step" is OS dependent
* @param amount The amount of "steps" to scroll
*/
public async scrollRight(amount: number): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().scrollRight(amount);
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link drag} drags the mouse along a certain path
* In summary, {@link drag} presses and holds the left mouse button, moves the mouse and releases the left button
* @param path The path of {@link Point}s to drag along
*/
public async drag(path: Point[] | Promise<Point[]>): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().pressButton(Button.LEFT);
await this.move(path);
await this.providerRegistry.getMouse().releaseButton(Button.LEFT);
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link pressButton} presses and holds a mouse button
* @param btn The {@link Button} to press and hold
*/
public async pressButton(btn: Button): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().pressButton(btn);
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link releaseButton} releases a mouse button previously pressed via {@link pressButton}
* @param btn The {@link Button} to release
*/
public async releaseButton(btn: Button): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().releaseButton(btn);
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link click} clicks a mouse button
* @param btn The {@link Button} to click
*/
public async click(btn: Button): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().click(btn);
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link doubleClick} performs a double click on a mouse button
* @param btn The {@link Button} to click
*/
public async doubleClick(btn: Button): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().doubleClick(btn);
resolve(this);
} catch (e) {
reject(e);
}
});
}
}