useHotkeySequence resets progress when modifier key is pressed before a key combo e.g. zA (Vim-like hotkeys) #73
TanStack Hotkeys version#
0.4.3
Framework/Library version#
vite react-ts
Describe the bug and the steps to reproduce it#
I encountered an issue while implementing Vim-like folding hotkeys (zA, zO, not za or zo).
The shortcuts aren't triggering as expected.
Press Z, then Shift+A. The callback is never triggered.
useHotkeySequence(['Z', 'Shift+A'], () => {
window.alert('zA: Doesn\'t work!');
})
Sequence Z, then A (without modifier) works correctly.
useHotkeySequence(['Z', 'A'], () => {
window.alert('za: Works!');
})
Expected behavior: Pressing Z followed by Shift+A triggers the callback. Actual behavior: The sequence is silently reset and the callback never fires.
Root cause:
When the user presses Shift before A, the browser emits two separate keydown events:
{ key: 'Shift', shiftKey: true }— modifier-only event{ key: 'A', shiftKey: true }— the actual key
In SequenceManager.#processTargetEvent, the first event (Shift-only) doesn't match the expected Shift+A (wrong key) and doesn't match the first sequence key Z either — so currentIndex is reset to 0. By the time the A event arrives, the sequence has already been abandoned.
Fix:
Skip modifier-only key events at the start of #processTargetEvent.
// sequence-manager.ts
import { isModifierKey, parseHotkey } from './parse'
#processTargetEvent(event, target, eventType) {
if (isModifierKey(event)) return // <-- add this
// ...rest of logic
}
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
https://github.com/klaskow/bug-react-hotkeys
Do you intend to try to help solve this bug with your own PR?#
I've submitted a PR with the fix and corresponding tests. What do you think?
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
KevinVandy closed this
Sign in to join this conversation.
fixed in #74