Open History to see the last commit
1import { useEffect, useRef, 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 );
27}
28
29export function useShortcutModifierState(): ShortcutModifierState {
30 const [state, setState] = useState(EMPTY_SHORTCUT_MODIFIER_STATE);
31 const stateRef = useRef(EMPTY_SHORTCUT_MODIFIER_STATE);
32
33 useEffect(() => {
34 const updateState = (next: ShortcutModifierState) => {
35 // Even a no-op state dispatch can cost work in the sidebar's large tree.
36 // Ordinary typing must return before dispatching a React update.
37 if (areShortcutModifierStatesEqual(stateRef.current, next)) return;
38 stateRef.current = next;
39 setState(next);
40 };
41 const onKeyboardEvent = (event: KeyboardEvent) => {
42 updateState(shortcutModifierStateAfterKeyboardEvent(stateRef.current, event));
43 };
44 // Dictation tools (Wispr Flow) paste with a synthetic ⌘V whose Meta keyup
45 // never reaches the page, so the tracked state stays "⌘ held" forever and
46 // the thread jump hints stick on screen. A paste is never jump intent, so
47 // treat it like a blur and reset. A physically held modifier re-registers
48 // on the next real key event.
49 const onResetEvent = () => {
50 updateState(EMPTY_SHORTCUT_MODIFIER_STATE);
51 };
52
53 window.addEventListener("keydown", onKeyboardEvent, true);
54 window.addEventListener("keyup", onKeyboardEvent, true);
55 window.addEventListener("paste", onResetEvent, true);
56 window.addEventListener("blur", onResetEvent);
57 return () => {
58 window.removeEventListener("keydown", onKeyboardEvent, true);
59 window.removeEventListener("keyup", onKeyboardEvent, true);
60 window.removeEventListener("paste", onResetEvent, true);
61 window.removeEventListener("blur", onResetEvent);
62 };
63 }, []);
64
65 return state;
66}
67
68function normalizeModifierKey(key: string): keyof ShortcutModifierState | null {
69 switch (key) {
70 case "Meta":
71 case "OS":
72 case "Command":
73 return "metaKey";
74 case "Control":
75 return "ctrlKey";
76 case "Alt":
77 case "Option":
78 return "altKey";
79 case "Shift":
80 return "shiftKey";
81 default:
82 return null;
83 }
84}
85
86export function shortcutModifierStateAfterKeyboardEvent(
87 currentState: ShortcutModifierState,
88 event: KeyboardEvent,
89): ShortcutModifierState {
90 const normalizedModifierKey = normalizeModifierKey(event.key);
91 let nextState: ShortcutModifierState;
92 if (normalizedModifierKey) {
93 nextState = {
94 ...currentState,
95 [normalizedModifierKey]: event.type === "keydown",
96 };
97 } else {
98 // Flags on non-modifier keys may only clear a bit, never set one. After a
99 // dictation tool's synthetic ⌘V (Wispr Flow), the browser can keep
100 // reporting metaKey=true on real key events (Enter to submit) until the
101 // user physically taps ⌘. Trusting that flag would mark ⌘ as held and
102 // stick the thread jump hints. Setting a bit requires a real modifier
103 // keydown, handled above.
104 nextState = {
105 metaKey: currentState.metaKey && event.metaKey,
106 ctrlKey: currentState.ctrlKey && event.ctrlKey,
107 altKey: currentState.altKey && event.altKey,
108 shiftKey: currentState.shiftKey && event.shiftKey,
109 };
110 }
111
112 return areShortcutModifierStatesEqual(currentState, nextState) ? currentState : nextState;
113}