Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions apps/web/src/shortcutModifierState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,26 @@ describe("shortcutModifierState", () => {
shiftKey: false,
});
});

it("ignores poisoned modifier flags on non-modifier keys", () => {
const state = shortcutModifierStateAfterKeyboardEvent(
emptyState(),
keyboardEventLike("keydown", { key: "Enter", metaKey: true }),
);
expect(state).toEqual(emptyState());
});

it("clears a held modifier when a non-modifier key reports it released", () => {
const heldMeta: ShortcutModifierState = {
metaKey: true,
ctrlKey: false,
altKey: false,
shiftKey: false,
};
const state = shortcutModifierStateAfterKeyboardEvent(
heldMeta,
keyboardEventLike("keydown", { key: "a", metaKey: false }),
);
expect(state).toEqual(emptyState());
});
});
21 changes: 14 additions & 7 deletions apps/web/src/shortcutModifierState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export function useShortcutModifierState(): ShortcutModifierState {
const onKeyboardEvent = (event: KeyboardEvent) => {
setState((current) => shortcutModifierStateAfterKeyboardEvent(current, event));
};
const onWindowBlur = () => {
// Dictation tools can paste with a synthetic modifier shortcut whose
// matching keyup never reaches the page. Reset on paste so jump hints do
// not remain stuck until another modifier event arrives.
const onResetEvent = () => {
setState((current) =>
areShortcutModifierStatesEqual(current, EMPTY_SHORTCUT_MODIFIER_STATE)
? current
Expand All @@ -43,11 +46,13 @@ export function useShortcutModifierState(): ShortcutModifierState {

window.addEventListener("keydown", onKeyboardEvent, true);
window.addEventListener("keyup", onKeyboardEvent, true);
window.addEventListener("blur", onWindowBlur);
window.addEventListener("paste", onResetEvent, true);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve held modifiers across ordinary paste

When a user performs a normal Ctrl/Cmd+V paste and continues holding Ctrl/Cmd, the capture-phase paste handler resets the state even though the modifier remains physically held. The following V keyup or number keydown cannot restore it because non-modifier events now only clear flags, so sidebar jump hints stay hidden until the user releases and presses the modifier again.

Useful? React with 👍 / 👎.

window.addEventListener("blur", onResetEvent);
return () => {
window.removeEventListener("keydown", onKeyboardEvent, true);
window.removeEventListener("keyup", onKeyboardEvent, true);
window.removeEventListener("blur", onWindowBlur);
window.removeEventListener("paste", onResetEvent, true);
window.removeEventListener("blur", onResetEvent);
};
}, []);

Expand Down Expand Up @@ -84,11 +89,13 @@ export function shortcutModifierStateAfterKeyboardEvent(
[normalizedModifierKey]: event.type === "keydown",
};
} else {
// Non-modifier events can clear stale browser flags, but only a real
// modifier keydown may mark a modifier as held.
nextState = {
metaKey: event.metaKey,
ctrlKey: event.ctrlKey,
altKey: event.altKey,
shiftKey: event.shiftKey,
metaKey: currentState.metaKey && event.metaKey,
ctrlKey: currentState.ctrlKey && event.ctrlKey,
altKey: currentState.altKey && event.altKey,
Comment on lines +114 to +115

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve AltGraph's reported Alt state

On Windows/Linux layouts where AltGraph is emitted as a Control keydown followed by an AltGraph event carrying both ctrlKey and altKey, AltGraph is not normalized as a modifier here. This conjunction therefore retains Ctrl but refuses to set Alt, making the state look like an exact Ctrl-only chord; downstream shouldShowThreadJumpHintsForModifiers can consequently display the default mod+1..9 jump hints while the user is typing AltGr characters.

Useful? React with 👍 / 👎.

shiftKey: currentState.shiftKey && event.shiftKey,
};
}

Expand Down
Loading