`getHeldKeys()` returns stale state during `HotkeySequenceRecorder` / `HotkeyRecorder` recording
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 719
- Forks
- 47
- Avg merge
- 5d 5h
- Merged PRs (30d)
- 1
Description
TanStack Hotkeys version
@tanstack/hotkeys v0.8.0
Framework/Library version
@tanstack/svelte-hotkeys v0.10.0
Describe the bug and the steps to reproduce it
Summary
When a recorder (HotkeySequenceRecorder or HotkeyRecorder) is actively recording, getHeldKeys() (and the underlying KeyStateTracker.store) does not update for keys that are pressed while recording. Released keys are still removed (because the keyup listener fires after the recorder), but newly pressed keys never appear in heldKeys until recording stops.
This makes it impossible to build a "live held-modifier preview" UI on top of the recorder, which is the documented use case for getHeldKeys() ("Custom shortcut recording for rebinding").
Root cause
Both recorders attach their keydown listener in capture phase and call stopPropagation() on every event:
dist/hotkey-sequence-recorder.js:174 — document.addEventListener("keydown", this.#keydownListener, true);
dist/hotkey-sequence-recorder.js:82-83 — event.preventDefault(); event.stopPropagation();
The same pattern exists in HotkeyRecorder.
KeyStateTracker, however, attaches its keydown/keyup listeners in bubble phase:
dist/key-state-tracker.js:109-110
document.addEventListener("keydown", this.#keydownListener);
document.addEventListener("keyup", this.#keyupListener);
Because the recorder runs first (capture) and stops propagation, the tracker's bubble-phase keydown listener never fires while recording. keyup events are not stopped by the recorder, so releases still propagate, leaving the tracker permanently behind.
Repro
Minimal Svelte 5 example:
<script>
import { createHotkeySequenceRecorder, getHeldKeys } from '@tanstack/svelte-hotkeys';
const recorder = createHotkeySequenceRecorder(() => ({ commitKeys: 'enter' }));
const held = getHeldKeys();
</script>
<button onclick={() => recorder.startRecording()}>Start</button>
<button onclick={() => recorder.commitRecording()}>Stop</button>
<p>Held: {held.keys.join(' + ') || '(empty)'}</p>
- Click Start.
- Press and hold
Ctrl(or any modifier) alone. - Observe:
held.keysstays[]. - Click Stop, press
Ctrlagain — nowheld.keyscorrectly shows['Control'].
Proposed fix
Make KeyStateTracker listen in capture phase, matching the recorders:
-document.addEventListener("keydown", this.#keydownListener);
-document.addEventListener("keyup", this.#keyupListener);
+document.addEventListener("keydown", this.#keydownListener, true);
+document.addEventListener("keyup", this.#keyupListener, true);
…with the matching change in #removeListeners(). The tracker only reads event.key / event.code and never calls preventDefault or stopPropagation, so moving it to capture phase has no effect on application behaviour — it just means the tracker observes events before any listener can stop them.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)
Screenshots or Videos (Optional)
No response
Do you intend to try to help solve this bug with your own PR?
Yes, I am also opening a PR that solves the problem along side this issue
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the source corresponding to dist/key-state-tracker.js and the recorder listeners described in the issue, then reproduce the behavior using the linked Svelte example. Verify that heldKeys updates for keys pressed during recording, releases still clear state, and listener cleanup remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100