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

shortcutModifierState.ts

109 lines3.5 KB

1import { useEffect, useState } from "react";
2​
3export interface ShortcutModifierState {
4 metaKey: boolean;
5 ctrlKey: boolean;
6 altKey: boolean;
7 shiftKey: boolean;
8}
9​
10const EMPTY_SHORTCUT_MODIFIER_STATE: ShortcutModifierState = {
11 metaKey: false,
12 ctrlKey: false,
13 altKey: false,
14 shiftKey: false,
15};
16​
17export function areShortcutModifierStatesEqual(
18 left: ShortcutModifierState,
19 right: ShortcutModifierState,
20): boolean {
21 return (
22 left.metaKey === right.metaKey &&
23 left.ctrlKey === right.ctrlKey &&
24 left.altKey === right.altKey &&
25 left.shiftKey === right.shiftKey
26 );
28​
29export function useShortcutModifierState(): ShortcutModifierState {
30 const [state, setState] = useState(EMPTY_SHORTCUT_MODIFIER_STATE);
31​
32 useEffect(() => {
33 const onKeyboardEvent = (event: KeyboardEvent) => {
34 setState((current) => shortcutModifierStateAfterKeyboardEvent(current, event));
35 };
36 // Dictation tools (Wispr Flow) paste with a synthetic ⌘V whose Meta keyup
37 // never reaches the page, so the tracked state stays "⌘ held" forever and
38 // the thread jump hints stick on screen. A paste is never jump intent, so
39 // treat it like a blur and reset. A physically held modifier re-registers
40 // on the next real key event.
41 const onResetEvent = () => {
42 setState((current) =>
43 areShortcutModifierStatesEqual(current, EMPTY_SHORTCUT_MODIFIER_STATE)
44 ? current
45 : EMPTY_SHORTCUT_MODIFIER_STATE,
46 );
47 };
48​
49 window.addEventListener("keydown", onKeyboardEvent, true);
50 window.addEventListener("keyup", onKeyboardEvent, true);
51 window.addEventListener("paste", onResetEvent, true);
52 window.addEventListener("blur", onResetEvent);
53 return () => {
54 window.removeEventListener("keydown", onKeyboardEvent, true);
55 window.removeEventListener("keyup", onKeyboardEvent, true);
56 window.removeEventListener("paste", onResetEvent, true);
57 window.removeEventListener("blur", onResetEvent);
58 };
59 }, []);
60​
61 return state;
63​
64function normalizeModifierKey(key: string): keyof ShortcutModifierState | null {
65 switch (key) {
66 case "Meta":
67 case "OS":
68 case "Command":
69 return "metaKey";
70 case "Control":
71 return "ctrlKey";
72 case "Alt":
73 case "Option":
74 return "altKey";
75 case "Shift":
76 return "shiftKey";
77 default:
78 return null;
79 }
81​
82export function shortcutModifierStateAfterKeyboardEvent(
83 currentState: ShortcutModifierState,
84 event: KeyboardEvent,
85): ShortcutModifierState {
86 const normalizedModifierKey = normalizeModifierKey(event.key);
87 let nextState: ShortcutModifierState;
88 if (normalizedModifierKey) {
89 nextState = {
90 ...currentState,
91 [normalizedModifierKey]: event.type === "keydown",
92 };
93 } else {
94 // Flags on non-modifier keys may only clear a bit, never set one. After a
95 // dictation tool's synthetic ⌘V (Wispr Flow), the browser can keep
96 // reporting metaKey=true on real key events (Enter to submit) until the
97 // user physically taps ⌘. Trusting that flag would mark ⌘ as held and
98 // stick the thread jump hints. Setting a bit requires a real modifier
99 // keydown, handled above.
100 nextState = {
101 metaKey: currentState.metaKey && event.metaKey,
102 ctrlKey: currentState.ctrlKey && event.ctrlKey,
103 altKey: currentState.altKey && event.altKey,
104 shiftKey: currentState.shiftKey && event.shiftKey,
105 };
106 }
107​
108 return areShortcutModifierStatesEqual(currentState, nextState) ? currentState : nextState;