Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

keyCodes.ts

258 lines4.8 KB

1// This order mirrors GhosttyKey in ghostty/vt/key/event.h. The values are
2// intentionally derived from the official W3C-aligned enum instead of
3// maintaining a second keyboard protocol.
4const ghosttyKeyboardCodes = [
5 "Unidentified",
6 "Backquote",
7 "Backslash",
8 "BracketLeft",
9 "BracketRight",
10 "Comma",
11 "Digit0",
12 "Digit1",
13 "Digit2",
14 "Digit3",
15 "Digit4",
16 "Digit5",
17 "Digit6",
18 "Digit7",
19 "Digit8",
20 "Digit9",
21 "Equal",
22 "IntlBackslash",
23 "IntlRo",
24 "IntlYen",
25 "KeyA",
26 "KeyB",
27 "KeyC",
28 "KeyD",
29 "KeyE",
30 "KeyF",
31 "KeyG",
32 "KeyH",
33 "KeyI",
34 "KeyJ",
35 "KeyK",
36 "KeyL",
37 "KeyM",
38 "KeyN",
39 "KeyO",
40 "KeyP",
41 "KeyQ",
42 "KeyR",
43 "KeyS",
44 "KeyT",
45 "KeyU",
46 "KeyV",
47 "KeyW",
48 "KeyX",
49 "KeyY",
50 "KeyZ",
51 "Minus",
52 "Period",
53 "Quote",
54 "Semicolon",
55 "Slash",
56 "AltLeft",
57 "AltRight",
58 "Backspace",
59 "CapsLock",
60 "ContextMenu",
61 "ControlLeft",
62 "ControlRight",
63 "Enter",
64 "MetaLeft",
65 "MetaRight",
66 "ShiftLeft",
67 "ShiftRight",
68 "Space",
69 "Tab",
70 "Convert",
71 "KanaMode",
72 "NonConvert",
73 "Delete",
74 "End",
75 "Help",
76 "Home",
77 "Insert",
78 "PageDown",
79 "PageUp",
80 "ArrowDown",
81 "ArrowLeft",
82 "ArrowRight",
83 "ArrowUp",
84 "NumLock",
85 "Numpad0",
86 "Numpad1",
87 "Numpad2",
88 "Numpad3",
89 "Numpad4",
90 "Numpad5",
91 "Numpad6",
92 "Numpad7",
93 "Numpad8",
94 "Numpad9",
95 "NumpadAdd",
96 "NumpadBackspace",
97 "NumpadClear",
98 "NumpadClearEntry",
99 "NumpadComma",
100 "NumpadDecimal",
101 "NumpadDivide",
102 "NumpadEnter",
103 "NumpadEqual",
104 "NumpadMemoryAdd",
105 "NumpadMemoryClear",
106 "NumpadMemoryRecall",
107 "NumpadMemoryStore",
108 "NumpadMemorySubtract",
109 "NumpadMultiply",
110 "NumpadParenLeft",
111 "NumpadParenRight",
112 "NumpadSubtract",
113 "NumpadSeparator",
114 "NumpadArrowUp",
115 "NumpadArrowDown",
116 "NumpadArrowRight",
117 "NumpadArrowLeft",
118 "NumpadBegin",
119 "NumpadHome",
120 "NumpadEnd",
121 "NumpadInsert",
122 "NumpadDelete",
123 "NumpadPageUp",
124 "NumpadPageDown",
125 "Escape",
126 "F1",
127 "F2",
128 "F3",
129 "F4",
130 "F5",
131 "F6",
132 "F7",
133 "F8",
134 "F9",
135 "F10",
136 "F11",
137 "F12",
138 "F13",
139 "F14",
140 "F15",
141 "F16",
142 "F17",
143 "F18",
144 "F19",
145 "F20",
146 "F21",
147 "F22",
148 "F23",
149 "F24",
150 "F25",
151 "Fn",
152 "FnLock",
153 "PrintScreen",
154 "ScrollLock",
155 "Pause",
156 "BrowserBack",
157 "BrowserFavorites",
158 "BrowserForward",
159 "BrowserHome",
160 "BrowserRefresh",
161 "BrowserSearch",
162 "BrowserStop",
163 "Eject",
164 "LaunchApp1",
165 "LaunchApp2",
166 "LaunchMail",
167 "MediaPlayPause",
168 "MediaSelect",
169 "MediaStop",
170 "MediaTrackNext",
171 "MediaTrackPrevious",
172 "Power",
173 "Sleep",
174 "AudioVolumeDown",
175 "AudioVolumeMute",
176 "AudioVolumeUp",
177 "WakeUp",
178 "Copy",
179 "Cut",
180 "Paste",
181] as const;
182​
183const codeToGhosttyKey = new Map<string, number>(
184 ghosttyKeyboardCodes.map((code, index) => [code, index]),
185);
186​
187export function ghosttyKeyForCode(code: string): number {
188 return codeToGhosttyKey.get(code) ?? 0;
190​
191export interface GhosttyKeyboardLayoutMap {
192 get(code: string): string | undefined;
194​
195const shiftedToUnshiftedCharacter = new Map<string, string>([
196 ["!", "1"],
197 ["@", "2"],
198 ["#", "3"],
199 ["$", "4"],
200 ["%", "5"],
201 ["^", "6"],
202 ["&", "7"],
203 ["*", "8"],
204 ["(", "9"],
205 [")", "0"],
206 ["~", "`"],
207 ["_", "-"],
208 ["+", "="],
209 ["{", "["],
210 ["}", "]"],
211 ["|", "\\"],
212 [":", ";"],
213 ['"', "'"],
214 ["<", ","],
215 [">", "."],
216 ["?", "/"],
217]);
218​
219let keyboardLayoutMapPromise: Promise<GhosttyKeyboardLayoutMap | undefined> | undefined;
220​
221export function loadGhosttyKeyboardLayoutMap(): Promise<GhosttyKeyboardLayoutMap | undefined> {
222 if (keyboardLayoutMapPromise) return keyboardLayoutMapPromise;
223 const browserNavigator = globalThis.navigator as
224 | (Navigator & {
225 readonly keyboard?: {
226 getLayoutMap(): Promise<GhosttyKeyboardLayoutMap>;
227 };
228 })
229 | undefined;
230 const keyboard = browserNavigator?.keyboard;
231 const promise = keyboard?.getLayoutMap().catch(() => undefined) ?? Promise.resolve(undefined);
232 keyboardLayoutMapPromise = promise;
233 return promise;
235​
236export function ghosttyUnshiftedCodepoint(
237 event: Pick<KeyboardEvent, "code" | "key" | "shiftKey">,
238 layoutMap?: GhosttyKeyboardLayoutMap,
239): number {
240 if ([...event.key].length !== 1) return 0;
241 const layoutCharacter = layoutMap?.get(event.code);
242 if (layoutCharacter && [...layoutCharacter].length === 1) {
243 return layoutCharacter.codePointAt(0) ?? 0;
244 }
245 if (/^[A-Z]$/u.test(event.key)) return event.key.charCodeAt(0) + 32;
246 if (event.shiftKey) {
247 const unshiftedCharacter = shiftedToUnshiftedCharacter.get(event.key);
248 if (unshiftedCharacter) return unshiftedCharacter.codePointAt(0) ?? 0;
249 const lowercase = event.key.toLowerCase();
250 if (lowercase !== event.key && [...lowercase].length === 1) {
251 return lowercase.codePointAt(0) ?? 0;
252 }
253 // Without layout data the unshifted form of a shifted key is unknowable;
254 // reporting the shifted character as unshifted corrupts Kitty alternate keys.
255 return 0;
256 }
257 return event.key.codePointAt(0) ?? 0;