53 lines
2.2 KiB
TypeScript
53 lines
2.2 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// chromium's reserved shortcuts, like ctrl+w closing the window, happen before the page can stop them. sprinting is
|
|
// ctrl and walking is w, so the app takes those keys from the os before chromium sees them and hands them to the page.
|
|
// how depends on the os, see shortcuts_x11.ts and shortcuts_windows.ts. macs close with cmd+w, which isn't a game key
|
|
import { guard_shortcuts_x11 } from "./shortcuts_x11.ts";
|
|
import { guard_shortcuts_windows } from "./shortcuts_windows.ts";
|
|
|
|
// the keys taken with ctrl and with ctrl+shift: their X11 key symbols, windows virtual keys, and how they reach the page
|
|
export const KEYS: { keysym: number; vk: number; code: string; key: string; shifted_key: string }[] = [
|
|
// close the window, ctrl+shift+w too
|
|
{ keysym: 0x77, vk: 0x57, code: "KeyW", key: "w", shifted_key: "W" },
|
|
// new tab, reopen a closed one
|
|
{ keysym: 0x74, vk: 0x54, code: "KeyT", key: "t", shifted_key: "T" },
|
|
// new window
|
|
{ keysym: 0x6e, vk: 0x4e, code: "KeyN", key: "n", shifted_key: "N" },
|
|
// ctrl+shift+q quits on linux
|
|
{ keysym: 0x71, vk: 0x51, code: "KeyQ", key: "q", shifted_key: "Q" },
|
|
// switching tabs
|
|
{ keysym: 0xff09, vk: 0x09, code: "Tab", key: "Tab", shifted_key: "Tab" },
|
|
{ keysym: 0xff55, vk: 0x21, code: "PageUp", key: "PageUp", shifted_key: "PageUp" },
|
|
{ keysym: 0xff56, vk: 0x22, code: "PageDown", key: "PageDown", shifted_key: "PageDown" },
|
|
// ctrl+f4 closes the tab on windows
|
|
{ keysym: 0xffc1, vk: 0x73, code: "F4", key: "F4", shifted_key: "F4" },
|
|
];
|
|
|
|
export type Key = typeof KEYS[number];
|
|
|
|
export function guard_shortcuts(win: Deno.BrowserWindow) {
|
|
switch (Deno.build.os) {
|
|
case "linux":
|
|
guard_shortcuts_x11(win);
|
|
break;
|
|
case "windows":
|
|
guard_shortcuts_windows(win);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// to the page as if chromium had sent it, see client/input_manager.ts
|
|
export function send_key(win: Deno.BrowserWindow, type: "keydown" | "keyup", key: Key, ctrl: boolean, shift: boolean) {
|
|
const init = {
|
|
code: key.code,
|
|
key: shift ? key.shifted_key : key.key,
|
|
ctrlKey: ctrl,
|
|
shiftKey: shift,
|
|
bubbles: true,
|
|
cancelable: true,
|
|
};
|
|
win.executeJs(`dispatchEvent(new KeyboardEvent(${JSON.stringify(type)}, ${JSON.stringify(init)}))`).catch(() => {});
|
|
}
|