-
Notifications
You must be signed in to change notification settings - Fork 118
/
parseHotkeys.ts
63 lines (54 loc) · 1.48 KB
/
parseHotkeys.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
57
58
59
60
61
62
63
import { Hotkey, KeyboardModifiers } from './types'
const reservedModifierKeywords = ['shift', 'alt', 'meta', 'mod', 'ctrl']
const mappedKeys: Record<string, string> = {
esc: 'escape',
return: 'enter',
'.': 'period',
',': 'comma',
'-': 'slash',
' ': 'space',
'`': 'backquote',
'#': 'backslash',
'+': 'bracketright',
ShiftLeft: 'shift',
ShiftRight: 'shift',
AltLeft: 'alt',
AltRight: 'alt',
MetaLeft: 'meta',
MetaRight: 'meta',
OSLeft: 'meta',
OSRight: 'meta',
ControlLeft: 'ctrl',
ControlRight: 'ctrl',
}
export function mapKey(key: string): string {
return (mappedKeys[key] || key)
.trim()
.toLowerCase()
.replace(/key|digit|numpad|arrow/, '')
}
export function isHotkeyModifier(key: string) {
return reservedModifierKeywords.includes(key)
}
export function parseKeysHookInput(keys: string, splitKey = ','): string[] {
return keys.split(splitKey)
}
export function parseHotkey(hotkey: string, combinationKey = '+', description?: string): Hotkey {
const keys = hotkey
.toLocaleLowerCase()
.split(combinationKey)
.map((k) => mapKey(k))
const modifiers: KeyboardModifiers = {
alt: keys.includes('alt'),
ctrl: keys.includes('ctrl') || keys.includes('control'),
shift: keys.includes('shift'),
meta: keys.includes('meta'),
mod: keys.includes('mod'),
}
const singleCharKeys = keys.filter((k) => !reservedModifierKeywords.includes(k))
return {
...modifiers,
keys: singleCharKeys,
description,
}
}