Open History to see the last commit
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;
189}
190
191export interface GhosttyKeyboardLayoutMap {
192 get(code: string): string | undefined;
193}
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;
234}
235
236// Browsers do not expose consumed modifiers; treat Shift as consumed for
237// unchorded character input.
238export function ghosttyConsumedMods(
239 event: Pick<KeyboardEvent, "altKey" | "ctrlKey" | "key" | "metaKey" | "shiftKey">,
240): number {
241 if (!event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) return 0;
242 return [...event.key].length === 1 ? 1 : 0;
243}
244
245export function ghosttyUnshiftedCodepoint(
246 event: Pick<KeyboardEvent, "code" | "key" | "shiftKey">,
247 layoutMap?: GhosttyKeyboardLayoutMap,
248): number {
249 if ([...event.key].length !== 1) return 0;
250 const layoutCharacter = layoutMap?.get(event.code);
251 if (layoutCharacter && [...layoutCharacter].length === 1) {
252 return layoutCharacter.codePointAt(0) ?? 0;
253 }
254 if (/^[A-Z]$/u.test(event.key)) return event.key.charCodeAt(0) + 32;
255 if (event.shiftKey) {
256 const unshiftedCharacter = shiftedToUnshiftedCharacter.get(event.key);
257 if (unshiftedCharacter) return unshiftedCharacter.codePointAt(0) ?? 0;
258 const lowercase = event.key.toLowerCase();
259 if (lowercase !== event.key && [...lowercase].length === 1) {
260 return lowercase.codePointAt(0) ?? 0;
261 }
262 // Without layout data the unshifted form of a shifted key is unknowable;
263 // reporting the shifted character as unshifted corrupts Kitty alternate keys.
264 return 0;
265 }
266 return event.key.codePointAt(0) ?? 0;
267}