// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // on windows the app takes chromium's reserved shortcuts (see shortcuts.ts) as hotkeys, through user32. a hotkey takes // the key press before any window gets it, its release still reaches chromium. hotkeys are for the whole system, so // they're only registered while the app's window is in front. holding the key repeats the hotkey import { KEYS, send_key } from "./shortcuts.ts"; const MOD_CONTROL = 0x2; const MOD_SHIFT = 0x4; const WM_HOTKEY = 0x0312; const PM_REMOVE = 0x1; // MSG on 64 bit windows: hwnd, then the message, wParam (the hotkey's id) and lParam (its modifiers and virtual key) const MSG_SIZE = 48; const MSG_MESSAGE = 8; const MSG_LPARAM = 24; // how often it looks for hotkeys and which window is in front, in milliseconds const POLL_MS = 4; const USER32 = { RegisterHotKey: { parameters: ["pointer", "i32", "u32", "u32"], result: "i32" }, UnregisterHotKey: { parameters: ["pointer", "i32"], result: "i32" }, PeekMessageW: { parameters: ["buffer", "pointer", "u32", "u32", "u32"], result: "i32" }, GetForegroundWindow: { parameters: [], result: "pointer" }, GetWindowThreadProcessId: { parameters: ["pointer", "buffer"], result: "u32" }, } as const; export function guard_shortcuts_windows(win: Deno.BrowserWindow) { let lib: Deno.DynamicLibrary; try { lib = Deno.dlopen("user32.dll", USER32); } catch (e) { console.error(`Not guarding shortcuts, no user32: ${(e as Error).message}`); return; } const user32 = lib.symbols; // every key with ctrl and with ctrl+shift, by their ids. without a window, WM_HOTKEY goes to this thread's queue const hotkeys = KEYS.flatMap((key) => [MOD_CONTROL, MOD_CONTROL | MOD_SHIFT].map((modifiers) => ({ key, modifiers })) ); let registered = false; const register = () => { if (registered) return; // one already taken by another app just isn't guarded hotkeys.forEach(({ key, modifiers }, id) => user32.RegisterHotKey(null, id + 1, modifiers, key.vk)); registered = true; }; const unregister = () => { if (!registered) return; hotkeys.forEach((_, id) => user32.UnregisterHotKey(null, id + 1)); registered = false; }; win.addEventListener("close", unregister); // the cef backend never sends focus or blur on windows, so it checks whether the window in front is the app's. // chromium's windows are this process's own const owner = new Uint32Array(1); const in_front = () => { const window = user32.GetForegroundWindow(); if (!window) return false; owner[0] = 0; user32.GetWindowThreadProcessId(window, owner); return owner[0] === Deno.pid; }; const msg = new Uint8Array(MSG_SIZE); const view = new DataView(msg.buffer); setInterval(() => { if (in_front()) register(); else unregister(); while (user32.PeekMessageW(msg, null, WM_HOTKEY, WM_HOTKEY, PM_REMOVE)) { if (view.getUint32(MSG_MESSAGE, true) !== WM_HOTKEY) continue; const lparam = view.getUint32(MSG_LPARAM, true); const modifiers = lparam & 0xffff; const vk = lparam >>> 16; const key = KEYS.find((k) => k.vk === vk); if (key) send_key(win, "keydown", key, (modifiers & MOD_CONTROL) !== 0, (modifiers & MOD_SHIFT) !== 0); } }, POLL_MS); }