Hopefully fix ctrl W for desktop apps
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// 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 has focus. 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, 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" },
|
||||
} as const;
|
||||
|
||||
export function guard_shortcuts_windows(win: Deno.BrowserWindow) {
|
||||
let lib: Deno.DynamicLibrary<typeof USER32>;
|
||||
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("focus", register);
|
||||
win.addEventListener("blur", unregister);
|
||||
win.addEventListener("close", unregister);
|
||||
|
||||
const msg = new Uint8Array(MSG_SIZE);
|
||||
const view = new DataView(msg.buffer);
|
||||
setInterval(() => {
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user