Skip to content
Source
Sign inSign up
TanStack/hotkeys

`useHotkey` callback fire during active `useHotkeySequence` matching #116

Closedkrehwell(GitHub profile, opens in a new tab) opened this issue 2 comments

TanStack Hotkeys version

v0.9.1

Framework/Library version

next v16.1.6

Describe the bug and the steps to reproduce it

When a single-key hotkey (e.g. C) and a sequence starting with a different key but ending with that same key (e.g. G → C) are both registered, pressing the sequence fires both the sequence callback and the single-key hotkey callback.

To Reproduce

// Register a single-key hotkey
useHotkey("C", () => {
    console.log("C hotkey fired");
});

// Register a sequence that ends with the same key
useHotkeySequence(["G", "C"], () => {
    console.log("G → C sequence fired");
});

// now press G, then press C
// notice that both log fired

.

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

None

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.
  1. I have the same issue, I solved it like this.

      // ref184318553 - order matters, this must go before the 'M' `useHotkey`,
      // so it can set the ref first before the 'M' hotkey can check it. The
      // callback of both trigger after hitting "gm" as they both end in "m". And
      // the callbacks trigger in the order they were registered.
      useHotkeySequence(
        ['G', 'M'],
        () => {
          gSequenceTriggered.current = true
    
         // do stuff
        }
      )
    
      // See ref184318553 - order matters
      useHotkey(
        'M',
        (e) => {
          if (gSequenceTriggered.current) {
            gSequenceTriggered.current = false
    
            // Cancel hits hotkey callback as 'gm' was triggered
            return
          }
    
          // do other stuff
        }
      )
  2. IanWorley(GitHub profile, opens in a new tab)commented

    Hey I have a reproduction of this issue: stackblitz Github

    Version:

    • @tanstack/react-hotkeys: ^0.10.0
    • React ^19.2.8
  3. KevinVandy closed this

Sign in to join this conversation.