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

keyCodes.ts

272 lines5.4 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 function ghosttyKeyForEvent(event: Pick<KeyboardEvent, "code" | "key">): number {
192 // Android browsers can omit the physical code for keys such as Backspace.
193 return ghosttyKeyForCode(event.code) || ghosttyKeyForCode(event.key);
195​
196export interface GhosttyKeyboardLayoutMap {
197 get(code: string): string | undefined;
199​
200const shiftedToUnshiftedCharacter = new Map<string, string>([
201 ["!", "1"],
202 ["@", "2"],
203 ["#", "3"],
204 ["$", "4"],
205 ["%", "5"],
206 ["^", "6"],
207 ["&", "7"],
208 ["*", "8"],
209 ["(", "9"],
210 [")", "0"],
211 ["~", "`"],
212 ["_", "-"],
213 ["+", "="],
214 ["{", "["],
215 ["}", "]"],
216 ["|", "\\"],
217 [":", ";"],
218 ['"', "'"],
219 ["<", ","],
220 [">", "."],
221 ["?", "/"],
222]);
223​
224let keyboardLayoutMapPromise: Promise<GhosttyKeyboardLayoutMap | undefined> | undefined;
225​
226export function loadGhosttyKeyboardLayoutMap(): Promise<GhosttyKeyboardLayoutMap | undefined> {
227 if (keyboardLayoutMapPromise) return keyboardLayoutMapPromise;
228 const browserNavigator = globalThis.navigator as
229 | (Navigator & {
230 readonly keyboard?: {
231 getLayoutMap(): Promise<GhosttyKeyboardLayoutMap>;
232 };
233 })
234 | undefined;
235 const keyboard = browserNavigator?.keyboard;
236 const promise = keyboard?.getLayoutMap().catch(() => undefined) ?? Promise.resolve(undefined);
237 keyboardLayoutMapPromise = promise;
238 return promise;
240​
241// Browsers do not expose consumed modifiers; treat Shift as consumed for
242// unchorded character input.
243export function ghosttyConsumedMods(
244 event: Pick<KeyboardEvent, "altKey" | "ctrlKey" | "key" | "metaKey" | "shiftKey">,
245): number {
246 if (!event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) return 0;
247 return [...event.key].length === 1 ? 1 : 0;
249​
250export function ghosttyUnshiftedCodepoint(
251 event: Pick<KeyboardEvent, "code" | "key" | "shiftKey">,
252 layoutMap?: GhosttyKeyboardLayoutMap,
253): number {
254 if ([...event.key].length !== 1) return 0;
255 const layoutCharacter = layoutMap?.get(event.code);
256 if (layoutCharacter && [...layoutCharacter].length === 1) {
257 return layoutCharacter.codePointAt(0) ?? 0;
258 }
259 if (/^[A-Z]$/u.test(event.key)) return event.key.charCodeAt(0) + 32;
260 if (event.shiftKey) {
261 const unshiftedCharacter = shiftedToUnshiftedCharacter.get(event.key);
262 if (unshiftedCharacter) return unshiftedCharacter.codePointAt(0) ?? 0;
263 const lowercase = event.key.toLowerCase();
264 if (lowercase !== event.key && [...lowercase].length === 1) {
265 return lowercase.codePointAt(0) ?? 0;
266 }
267 // Without layout data the unshifted form of a shifted key is unknowable;
268 // reporting the shifted character as unshifted corrupts Kitty alternate keys.
269 return 0;
270 }
271 return event.key.codePointAt(0) ?? 0;