/
keyboard.function.ts
41 lines (40 loc) · 1.53 KB
/
keyboard.function.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
import { clipboard, Key as NutKey, keyboard } from "@nut-tree/nut-js";
import { pasteShortcut } from "./shortcut.function";
import { withEncryption } from "../secrets.function";
import { Key } from "../key.class";
import { LegacyProjectProperties } from "../../../loader/legacy-project-properties.class";
export const createKeyboardApi = (props: LegacyProjectProperties) => {
keyboard.config.autoDelayMs = props.typeDelay;
return {
async paste(text: string) {
await clipboard.copy(text);
await keyboard.pressKey(...(pasteShortcut() as NutKey[]));
await keyboard.releaseKey(...(pasteShortcut() as NutKey[]));
},
async pasteAndDecrypt(key: string, text: string) {
return withEncryption(key, text, async (decrypted) => {
await this.paste(decrypted);
});
},
async type(text: string | Key, ...optModifiers: Key[]) {
await keyboard.pressKey(...(optModifiers as NutKey[]));
if (typeof text === "string") {
await keyboard.type(text);
} else {
await keyboard.type(text as NutKey);
}
await keyboard.releaseKey(...(optModifiers as NutKey[]));
},
async typeAndDecrypt(key: string, text: string, ...optModifiers: Key[]) {
return withEncryption(key, text, async (decrypted) => {
await this.type(decrypted, ...optModifiers);
});
},
async pressKey(...keys: Key[]) {
await keyboard.pressKey(...(keys as NutKey[]));
},
async releaseKey(...keys: Key[]) {
await keyboard.releaseKey(...(keys as NutKey[]));
},
};
};