Skip to content
Source
Sign inSign up
Open History to see the last commit

GlobalShiftShortcutWorker.ts

36 lines1.1 KB

1// @effect-diagnostics globalTimers:off -- A plain Node child; the poll runs outside any Effect fiber.
2import { SNAP_SHOT_MODIFIERS, type SnapShotModifier } from "@t3tools/contracts";
3​
4import { loadWindowsForegroundApi } from "../electron/WindowsForeground.ts";
5import { WINDOWS_MODIFIER_PAIR_VIRTUAL_KEYS } from "./snapShot.ts";
6​
7const POLL_INTERVAL_MS = 50;
8​
9const requested = process.argv[2];
10if (!(SNAP_SHOT_MODIFIERS as readonly string[]).includes(requested ?? "")) {
11 process.exit(1);
13const [left, right] = WINDOWS_MODIFIER_PAIR_VIRTUAL_KEYS[requested as SnapShotModifier];
14​
15async function poll() {
16 const api = await loadWindowsForegroundApi();
17 let active = false;
18 process.send?.("ready");
19 const timer = setInterval(() => {
20 const pressed = api.isKeyDown(left) && api.isKeyDown(right);
21 if (pressed && !active) {
22 try {
23 process.send?.("trigger");
24 } catch {}
25 }
26 active = pressed;
27 }, POLL_INTERVAL_MS);
28 const shutdown = () => {
29 clearInterval(timer);
30 process.exit(0);
31 };
32 process.once("disconnect", shutdown);
33 process.once("SIGTERM", shutdown);
35​
36void poll().catch(() => process.exit(1));