-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathkeyboard.class.ts
115 lines (107 loc) · 3.34 KB
/
keyboard.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
import { Key } from "./key.enum";
import { sleep } from "./sleep.function";
import { ProviderRegistry } from "./provider/provider-registry.class";
type StringOrKey = string[] | Key[];
const inputIsString = (input: (string | Key)[]): input is string[] => {
return input.every((elem: string | Key) => typeof elem === "string");
};
/**
* Config object for {@link KeyboardClass} class
*/
export interface KeyboardConfig {
/**
* Configures the delay between single key events
*/
autoDelayMs: number;
}
/**
* {@link KeyboardClass} class provides methods to emulate keyboard input
*/
export class KeyboardClass {
/**
* Config object for {@link KeyboardClass} class
*/
public config: KeyboardConfig = {
autoDelayMs: 300,
};
/**
* {@link KeyboardClass} class constructor
* @param providerRegistry
*/
constructor(private providerRegistry: ProviderRegistry) {
this.providerRegistry
.getKeyboard()
.setKeyboardDelay(this.config.autoDelayMs);
}
/**
* {@link type} types a sequence of {@link String} or single {@link Key}s via system keyboard
* @example
* ```typescript
* await keyboard.type(Key.A, Key.S, Key.D, Key.F);
* await keyboard.type("Hello, world!");
* ```
*
* @param input Sequence of {@link String} or {@link Key} to type
*/
public type(...input: StringOrKey): Promise<KeyboardClass> {
return new Promise<KeyboardClass>(async (resolve, reject) => {
try {
if (inputIsString(input)) {
for (const char of input.join(" ")) {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getKeyboard().type(char);
}
} else {
await this.providerRegistry.getKeyboard().click(...(input as Key[]));
}
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link pressKey} presses and holds a single {@link Key} for {@link Key} combinations
* Modifier {@link Key}s are to be given in "natural" ordering, so first modifier {@link Key}s, followed by the {@link Key} to press
* @example
* ```typescript
* // Will press and hold key combination STRG + V
* await keyboard.pressKey(Key.STRG, Key.V);
* ```
*
* @param keys Array of {@link Key}s to press and hold
*/
public pressKey(...keys: Key[]): Promise<KeyboardClass> {
return new Promise<KeyboardClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getKeyboard().pressKey(...keys);
resolve(this);
} catch (e) {
reject(e);
}
});
}
/**
* {@link pressKey} releases a single {@link Key} for {@link Key} combinations
* Modifier {@link Key}s are to be given in "natural" ordering, so first modifier {@link Key}s, followed by the {@link Key} to press
* @example
* ```typescript
* // Will release key combination STRG + V
* await keyboard.releaseKey(Key.STRG, Key.V);
* ```
*
* @param keys Array of {@link Key}s to release
*/
public releaseKey(...keys: Key[]): Promise<KeyboardClass> {
return new Promise<KeyboardClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getKeyboard().releaseKey(...keys);
resolve(this);
} catch (e) {
reject(e);
}
});
}
}