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
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
shortcuts.ts · 1.7 KB · 39 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 | return null; |
| 39 | } |