-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
keyboard.ts
41 lines (38 loc) · 1.3 KB
/
keyboard.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 {delay} from './async'
export const Keyboard = {
// Todo come up with a way to autogenerate the methods from the interface and the code
LEFT_ARROW: 37,
UP_ARROW: 38,
RIGHT_ARROW: 39,
DOWN_ARROW: 40,
BASE_DELAY: 20,
async simulateKey(code: number, delayOverride: number = 0, opts?: KeyboardEventInit) {
;['keydown', 'keyup'].forEach(eventType =>
document?.activeElement?.dispatchEvent(getKeyboardEvent(eventType, code, opts))
)
return delay(delayOverride || this.BASE_DELAY)
},
async pressEnter(delayOverride: number = 0) {
return this.simulateKey(13, delayOverride)
},
async pressEsc(delayOverride: number = 0) {
return this.simulateKey(27, delayOverride)
},
async pressBackspace(delayOverride: number = 0) {
return this.simulateKey(8, delayOverride)
},
async pressTab(delayOverride: number = 0) {
return this.simulateKey(9, delayOverride)
},
async pressShiftTab(delayOverride: number = 0) {
return this.simulateKey(9, delayOverride, {shiftKey: true})
},
}
const getKeyboardEvent = (type: string, code: number, opts: any) =>
new KeyboardEvent(type, {
bubbles: true,
cancelable: true,
// @ts-ignore
keyCode: code,
...opts,
})