|
1 | 1 | import {Config, Instance} from '../setup'
|
2 |
| -import {keyboardAction, KeyboardAction, releaseAllKeys} from './keyboardAction' |
| 2 | +import {keyboardKey} from '../system/keyboard' |
| 3 | +import {wait} from '../utils' |
3 | 4 | import {parseKeyDef} from './parseKeyDef'
|
4 |
| -import type {keyboardState, keyboardKey} from './types' |
5 | 5 |
|
6 |
| -export {releaseAllKeys} |
7 |
| -export type {keyboardKey, keyboardState} |
| 6 | +interface KeyboardAction { |
| 7 | + keyDef: keyboardKey |
| 8 | + releasePrevious: boolean |
| 9 | + releaseSelf: boolean |
| 10 | + repeat: number |
| 11 | +} |
8 | 12 |
|
9 | 13 | export async function keyboard(this: Instance, text: string): Promise<void> {
|
10 | 14 | const actions: KeyboardAction[] = parseKeyDef(this[Config].keyboardMap, text)
|
11 | 15 |
|
12 |
| - return keyboardAction(this[Config], actions) |
| 16 | + for (let i = 0; i < actions.length; i++) { |
| 17 | + await wait(this[Config]) |
| 18 | + |
| 19 | + await keyboardAction(this[Config], actions[i]) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +async function keyboardAction( |
| 24 | + config: Config, |
| 25 | + {keyDef, releasePrevious, releaseSelf, repeat}: KeyboardAction, |
| 26 | +) { |
| 27 | + const {system} = config |
| 28 | + |
| 29 | + // Release the key automatically if it was pressed before. |
| 30 | + if (system.keyboard.isKeyPressed(keyDef)) { |
| 31 | + await system.keyboard.keyup(config, keyDef) |
| 32 | + } |
| 33 | + |
| 34 | + if (!releasePrevious) { |
| 35 | + for (let i = 1; i <= repeat; i++) { |
| 36 | + await system.keyboard.keydown(config, keyDef) |
| 37 | + |
| 38 | + if (i < repeat) { |
| 39 | + await wait(config) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Release the key only on the last iteration on `state.repeatKey`. |
| 44 | + if (releaseSelf) { |
| 45 | + await system.keyboard.keyup(config, keyDef) |
| 46 | + } |
| 47 | + } |
13 | 48 | }
|
14 | 49 |
|
15 |
| -export function createKeyboardState(): keyboardState { |
16 |
| - return { |
17 |
| - activeElement: null, |
18 |
| - pressed: [], |
19 |
| - carryChar: '', |
20 |
| - modifiers: { |
21 |
| - Alt: false, |
22 |
| - AltGraph: false, |
23 |
| - Control: false, |
24 |
| - CapsLock: false, |
25 |
| - Fn: false, |
26 |
| - FnLock: false, |
27 |
| - Meta: false, |
28 |
| - NumLock: false, |
29 |
| - ScrollLock: false, |
30 |
| - Shift: false, |
31 |
| - Symbol: false, |
32 |
| - SymbolLock: false, |
33 |
| - }, |
34 |
| - modifierPhase: {}, |
| 50 | +export async function releaseAllKeys(config: Config) { |
| 51 | + for (const k of config.system.keyboard.getPressedKeys()) { |
| 52 | + await config.system.keyboard.keyup(config, k) |
35 | 53 | }
|
36 | 54 | }
|
0 commit comments