askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
shortcuts.ts · 1.8 KB · 40 lines
| 1 | /** |
| 2 | * Browser shortcuts a page cannot take. A page that has the keyboard focus sees keys first, and one that swallows |
| 3 | * them (an editor, a site's own shortcuts) would otherwise stop ⌘⇧] from switching tabs until you clicked the tab bar. |
| 4 | * Like other browsers, the tab keys are the browser's: they are caught before the page sees them. |
| 5 | * |
| 6 | * Electron-free so it is unit-tested. |
| 7 | */ |
| 8 | |
| 9 | export interface KeyInput { |
| 10 | type: string; |
| 11 | key: string; |
| 12 | code: string; |
| 13 | meta: boolean; |
| 14 | control: boolean; |
| 15 | shift: boolean; |
| 16 | alt: boolean; |
| 17 | } |
| 18 | |
| 19 | /** The tab command (as the menu sends them) for a key the browser keeps for itself, or null to leave it to the page. */ |
| 20 | export function reservedShortcut(i: KeyInput, mac: boolean): string | null { |
| 21 | if (i.type !== "keyDown") return null; |
| 22 | // Ctrl+Tab / Ctrl+Shift+Tab on every platform |
| 23 | if (i.control && !i.meta && !i.alt && i.key === "Tab") return i.shift ? "prev" : "next"; |
| 24 | const mod = mac ? i.meta && !i.control : i.control && !i.meta; |
| 25 | if (!mod) return null; |
| 26 | // ⌘⇧] and ⌘⇧[ (reported as } and { on some layouts): by the key's place, so any layout works |
| 27 | if (i.shift && !i.alt && (i.code === "BracketRight" || i.key === "}" || i.key === "]")) return "next"; |
| 28 | if (i.shift && !i.alt && (i.code === "BracketLeft" || i.key === "{" || i.key === "[")) return "prev"; |
| 29 | if (i.alt && !i.shift && i.key === "ArrowRight") return "next"; |
| 30 | if (i.alt && !i.shift && i.key === "ArrowLeft") return "prev"; |
| 31 | if (i.shift || i.alt) return null; |
| 32 | const digit = /^Digit([1-9])$/.exec(i.code); |
| 33 | if (digit) return `tab:${digit[1]}`; |
| 34 | const k = i.key.toLowerCase(); |
| 35 | if (k === "t") return "new"; |
| 36 | if (k === "w") return "close"; |
| 37 | if (k === "l") return "focus-url"; |
| 38 | if (k === "r") return "reload"; // ⌘R / Ctrl+R reloads even while the page has the keyboard |
| 39 | return null; |
| 40 | } |