-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEventKeyCapture.ts
More file actions
192 lines (165 loc) · 5.91 KB
/
EventKeyCapture.ts
File metadata and controls
192 lines (165 loc) · 5.91 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
import type { Input } from 'electron';
/**
* Extended keyboard event interface with additional properties
*/
interface ExtendedInput extends Input {
ctrlKey?: boolean;
shiftKey?: boolean;
metaKey?: boolean;
altKey?: boolean;
}
/**
* Operating system type
*/
type OperatingSystem = 'darwin' | 'win32' | 'linux';
/**
* Navigator with userAgentData (for modern browsers)
*/
interface NavigatorWithUserAgentData extends Navigator {
userAgentData?: {
platform: string;
};
}
/**
* Gets the accelerator string from a keyboard keydown event
* @param input Keyboard event
* @returns Key combination (e.g. "Ctrl+A")
*/
function getAcceleratorByEvent(input: Input): string {
return getKeysByEvent(input).join('+');
}
/**
* Gets the OS key combination string from a keyboard keydown event
* @param input Keyboard event
* @returns Key combination (e.g. "Alt+A" => "Option+A")
*/
function getOSKeyCombinationByEvent(input: Input): string {
return getKeysByEvent(input)
.map((k) => ACCELERATOR_TO_VIEW[k] ?? k)
.join('+');
}
function getKeysByEvent(input: Input): string[] {
const key = CODE_TO_ACCELERATOR[input.code];
if (!key) {
console.error('Key not found:', input.code);
return [];
}
// Build modifier combination with platform-specific ordering
const keys: string[] = [];
if (((input as ExtendedInput).ctrlKey || input.control) && key !== 'Ctrl') keys.push('Ctrl');
if (((input as ExtendedInput).shiftKey || input.shift) && key !== 'Shift') keys.push('Shift');
if (((input as ExtendedInput).metaKey || input.meta) && key !== 'Meta') {
keys.push('Meta');
}
if (((input as ExtendedInput).altKey || input.alt) && key !== 'Alt' && key !== 'AltGr') keys.push('Alt');
keys.push(key);
return keys;
}
/**
* Normalizes key names for Electron Accelerator compatibility
* @param keyCombination Key combination string (e.g., "Ctrl+A")
* @returns Normalized key combination for Electron Accelerator
* @example Option+Shift+F1 => Alt+Shift+F1
* Cmd/Win+Shift+F1 => Meta+Shift+F1
*/
function parseToAccelerator(keyCombination: string): string {
const keys = keyCombination.split('+').map((k) => k.trim());
return keys.map((k) => VIEW_TO_ACCELERATOR[k] ?? k).join('+');
}
/**
* Normalizes key names for OS-specific compatibility
* @param accelerator Key combination string (e.g., "Ctrl+A")
* @returns parsed key combination for OS
* @example Alt+Shift+F1 => Option+Shift+F1
* Meta+Shift+F1 => Cmd/Win+Shift+F1
*/
function parseToOSKeyCombination(accelerator: string): string {
const keys = accelerator.split('+').map((k) => k.trim());
return keys.map((k) => ACCELERATOR_TO_VIEW[k] ?? k).join('+');
}
/**
* Detects current operating system
* @returns 'darwin', 'win32', 'linux' or 'unknown'
*/
function detectOperatingSystem(): OperatingSystem {
if (typeof process !== 'undefined' && process.platform) {
return process.platform as OperatingSystem;
}
if (typeof navigator !== 'undefined') {
const nav = navigator as NavigatorWithUserAgentData;
if (nav.userAgentData) {
const platform = nav.userAgentData.platform;
if (platform === 'macOS') return 'darwin';
if (platform === 'Windows') return 'win32';
if (platform === 'Linux') return 'linux';
}
const userAgent = navigator.userAgent;
if (/Mac/.test(userAgent)) return 'darwin';
if (/Linux/.test(userAgent)) return 'linux';
if (/Windows/.test(userAgent)) return 'win32';
}
return 'linux';
}
const OS = detectOperatingSystem();
const IS_DARWIN = OS === 'darwin';
const IS_WIN = OS === 'win32';
const CODE_TO_ACCELERATOR: Record<string, string> = {
AltRight: 'AltGr',
AltLeft: 'Alt',
MetaLeft: 'Meta',
MetaRight: 'Meta',
ControlLeft: 'Ctrl',
ControlRight: 'Ctrl',
Escape: 'Esc',
Tab: 'Tab',
Space: 'Space',
Backspace: 'Backspace',
ShiftLeft: 'Shift',
ShiftRight: 'Shift',
NumLock: 'Numlock',
CapsLock: 'Capslock',
ScrollLock: 'ScrollLock',
NumpadEnter: 'Enter',
Enter: 'Enter',
PrintScreen: 'PrintScreen',
Quote: '\'',
Backquote: '`',
Backslash: '\\',
Slash: '/',
Semicolon: ';',
BracketLeft: '[',
BracketRight: ']',
Comma: ',',
Period: '.',
Minus: '-',
Equal: '=',
Insert: 'Insert', Delete: 'Delete', Home: 'Home', End: 'End',
PageUp: 'PageUp', PageDown: 'PageDown',
ArrowUp: 'Up', ArrowDown: 'Down', ArrowLeft: 'Left', ArrowRight: 'Right',
F1: 'F1', F2: 'F2', F3: 'F3', F4: 'F4', F5: 'F5', F6: 'F6',
F7: 'F7', F8: 'F8', F9: 'F9', F10: 'F10', F11: 'F12', F12: 'F12',
Digit0: '0', Digit1: '1', Digit2: '2', Digit3: '3', Digit4: '4',
Digit5: '5', Digit6: '6', Digit7: '7', Digit8: '8', Digit9: '9',
KeyA: 'A', KeyB: 'B', KeyC: 'C', KeyD: 'D', KeyE: 'E', KeyF: 'F', KeyG: 'G',
KeyH: 'H', KeyI: 'I', KeyJ: 'J', KeyK: 'K', KeyL: 'L', KeyM: 'M', KeyN: 'N',
KeyO: 'O', KeyP: 'P', KeyQ: 'Q', KeyR: 'R', KeyS: 'S', KeyT: 'T', KeyU: 'U',
KeyV: 'V', KeyW: 'W', KeyX: 'X', KeyY: 'Y', KeyZ: 'Z',
Numpad0: 'num0', Numpad1: 'num1', Numpad2: 'num2', Numpad3: 'num3', Numpad4: 'num4',
Numpad5: 'num5', Numpad6: 'num6', Numpad7: 'num7', Numpad8: 'num8', Numpad9: 'num9',
NumpadAdd: 'numadd', NumpadDecimal: 'numdec', NumpadDivide: 'numdiv',
NumpadMultiply: 'nummult', NumpadSubtract: 'numsub',
};
const ACCELERATOR_TO_VIEW: Record<string, string> = {
Meta: IS_DARWIN ? 'Cmd' : IS_WIN ? 'Win' : 'Super',
AltGr: IS_DARWIN ? 'ROption' : 'AltGr',
Alt: IS_DARWIN ? 'Option' : 'Alt',
Delete: 'Del',
num0: 'Num 0', num1: 'Num 1', num2: 'Num 2', num3: 'Num 3', num4: 'Num 4',
num5: 'Num 5', num6: 'Num 6', num7: 'Num 7', num8: 'Num 8', num9: 'Num 9',
numdec: 'Num .', numadd: 'Num +', numsub: 'Num -', numdiv: 'Num /', nummult: 'Num *',
};
const VIEW_TO_ACCELERATOR: Record<string, string> = { Command: 'Meta', Windows: 'Meta' };
for (const [key, value] of Object.entries(ACCELERATOR_TO_VIEW)) {
VIEW_TO_ACCELERATOR[value] = key;
}
export { getAcceleratorByEvent, getOSKeyCombinationByEvent, parseToAccelerator, parseToOSKeyCombination };