-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathkeyboard.ts
More file actions
211 lines (185 loc) Β· 4.76 KB
/
keyboard.ts
File metadata and controls
211 lines (185 loc) Β· 4.76 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {type Instance} from '../setup'
import {getActiveElementOrBody} from '../utils'
import {type System} from '.'
export enum DOM_KEY_LOCATION {
STANDARD = 0,
LEFT = 1,
RIGHT = 2,
NUMPAD = 3,
}
export interface keyboardKey {
/** Physical location on a keyboard */
code?: string
/** Character or functional key descriptor */
key?: string
/** Location on the keyboard for keys with multiple representation */
location?: DOM_KEY_LOCATION
/** Does the character in `key` require/imply AltRight to be pressed? */
altGr?: boolean
/** Does the character in `key` require/imply a shiftKey to be pressed? */
shift?: boolean
}
const modifierKeys = [
'Alt',
'AltGraph',
'Control',
'Fn',
'Meta',
'Shift',
'Symbol',
] as const
type ModififierKey = typeof modifierKeys[number]
function isModifierKey(key?: string): key is ModififierKey {
return modifierKeys.includes(key as ModififierKey)
}
const modifierLocks = [
'CapsLock',
'FnLock',
'NumLock',
'ScrollLock',
'SymbolLock',
] as const
type ModififierLockKey = typeof modifierLocks[number]
function isModifierLock(key?: string): key is ModififierLockKey {
return modifierLocks.includes(key as ModififierLockKey)
}
export class KeyboardHost {
readonly system: System
constructor(system: System) {
this.system = system
}
readonly modifiers = {
Alt: false,
AltGraph: false,
CapsLock: false,
Control: false,
Fn: false,
FnLock: false,
Meta: false,
NumLock: false,
ScrollLock: false,
Shift: false,
Symbol: false,
SymbolLock: false,
}
private readonly pressed = new (class {
registry: {
[k in string]?: {
keyDef: keyboardKey
unpreventedDefault: boolean
}
} = {}
add(code: string, keyDef: keyboardKey) {
this.registry[code] ??= {
keyDef,
unpreventedDefault: false,
}
}
has(code: string) {
return !!this.registry[code]
}
setUnprevented(code: string) {
const o = this.registry[code]
if (o) {
o.unpreventedDefault = true
}
}
isUnprevented(code: string) {
return !!this.registry[code]?.unpreventedDefault
}
delete(code: string) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.registry[code]
}
values() {
return Object.values(this.registry) as NonNullable<
typeof this.registry[string]
>[]
}
})()
carryChar = ''
private lastKeydownTarget: Element | undefined = undefined
private readonly modifierLockStart: Record<string, boolean> = {}
isKeyPressed(keyDef: keyboardKey) {
return this.pressed.has(String(keyDef.code))
}
getPressedKeys() {
return this.pressed.values().map(p => p.keyDef)
}
/** Press a key */
async keydown(instance: Instance, keyDef: keyboardKey) {
const key = String(keyDef.key)
const code = String(keyDef.code)
const target = getActiveElementOrBody(instance.config.document)
this.setKeydownTarget(target)
this.pressed.add(code, keyDef)
if (isModifierKey(key)) {
this.modifiers[key] = true
}
const unprevented = instance.dispatchUIEvent(target, 'keydown', {
key,
code,
})
if (isModifierLock(key) && !this.modifiers[key]) {
this.modifiers[key] = true
this.modifierLockStart[key] = true
}
if (unprevented) {
this.pressed.setUnprevented(code)
}
if (unprevented && this.hasKeyPress(key)) {
instance.dispatchUIEvent(
getActiveElementOrBody(instance.config.document),
'keypress',
{
key,
code,
charCode:
keyDef.key === 'Enter' ? 13 : String(keyDef.key).charCodeAt(0),
},
)
}
}
/** Release a key */
async keyup(instance: Instance, keyDef: keyboardKey) {
const key = String(keyDef.key)
const code = String(keyDef.code)
const unprevented = this.pressed.isUnprevented(code)
this.pressed.delete(code)
if (
isModifierKey(key) &&
!this.pressed.values().find(p => p.keyDef.key === key)
) {
this.modifiers[key] = false
}
instance.dispatchUIEvent(
getActiveElementOrBody(instance.config.document),
'keyup',
{
key,
code,
},
!unprevented,
)
if (isModifierLock(key) && this.modifiers[key]) {
if (this.modifierLockStart[key]) {
this.modifierLockStart[key] = false
} else {
this.modifiers[key] = false
}
}
}
private setKeydownTarget(target: Element) {
if (target !== this.lastKeydownTarget) {
this.carryChar = ''
}
this.lastKeydownTarget = target
}
private hasKeyPress(key: string) {
return (
(key.length === 1 || key === 'Enter') &&
!this.modifiers.Control &&
!this.modifiers.Alt
)
}
}