-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathindex.ts
56 lines (46 loc) · 1.41 KB
/
index.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
import {type Instance} from '../setup'
import {type keyboardKey} from '../system/keyboard'
import {wait} from '../utils'
import {parseKeyDef} from './parseKeyDef'
interface KeyboardAction {
keyDef: keyboardKey
releasePrevious: boolean
releaseSelf: boolean
repeat: number
}
export async function keyboard(this: Instance, text: string): Promise<void> {
const actions: KeyboardAction[] = parseKeyDef(this.config.keyboardMap, text)
for (let i = 0; i < actions.length; i++) {
if (i > 0) {
await wait(this.config)
}
await keyboardAction(this, actions[i])
}
}
async function keyboardAction(
instance: Instance,
{keyDef, releasePrevious, releaseSelf, repeat}: KeyboardAction,
) {
const {system} = instance
// Release the key automatically if it was pressed before.
if (system.keyboard.isKeyPressed(keyDef)) {
await system.keyboard.keyup(instance, keyDef)
}
if (!releasePrevious) {
for (let i = 1; i <= repeat; i++) {
await system.keyboard.keydown(instance, keyDef)
if (i < repeat) {
await wait(instance.config)
}
}
// Release the key only on the last iteration on `state.repeatKey`.
if (releaseSelf) {
await system.keyboard.keyup(instance, keyDef)
}
}
}
export async function releaseAllKeys(instance: Instance) {
for (const k of instance.system.keyboard.getPressedKeys()) {
await instance.system.keyboard.keyup(instance, k)
}
}