react-hotkeys-hook icon indicating copy to clipboard operation
react-hotkeys-hook copied to clipboard

[BUG] Scoped hotkeys still trigger when scope is not active

Open ckpearson opened this issue 2 years ago • 5 comments

Describe the bug When creating a hotkey and setting the scopes option to restrict the hotkey to a specific scope, the hotkey triggers regardless of what the active scope is.

In fact, I'm not even sure the scope is being set against the hotkey as when the context's hotkeys collection is dumped there isn't any scopes value against it.

To Reproduce Repro codesandbox here: https://codesandbox.io/s/react-hotkeys-hook-scopes-not-working-yny6kl?file=/src/App.tsx

Expected behavior I would expect the scoped hotkey to only trigger when one of the scopes it is set to use is active

Screenshots N/A

Desktop (please complete the following information): N/A - happens across windows & osx in chrome & safari

ckpearson avatar Jul 28 '23 11:07 ckpearson

After a little bit of experimentation I think this might be a case of scope behaviour being a little unintuitive.

When setting the initiallyActiveScopes to {["none"]} I can see that the a hotkey (unscoped) still triggers, but the m hotkey (scoped to "tens") no longer does.

I think what's happening is that even though the wildcard scope * is no longer in the active scopes collection, any hotkey that is in the wildcard scope still runs, this kind of makes sense but is a little unintuitive.

From this perspective I can sort of see why my scoped hotkey was running when the wildcard scope was active (no initiallyActiveScopes set), because the wildcard scope matches all hotkeys.

I guess I was under the impression that the wildcard scope behaved just like every other scope, and therefore when it wasn't active, hotkeys that were unscoped would also be disabled.

I suppose then, the way to achieve the behaviour I'm after would be to ensure that none of my hotkeys are in the wildcard scope (unless I truly want them available at all times), and to ensure that I set the initiallyActiveScopes to be the ones I consider to be "global".

tl;dr:

  • The wildcard scope is always technically active, even if it doesn't appear in the active scopes collection
  • Hotkeys that aren't explicitly scoped belong to the wildcard scope, and therefore are always available
  • Scoped hotkeys are also in the wildcard scope, but only in the specific circumstance where the wildcard scope is in the active scopes collection
    • This is where the unintuitive behaviour comes from

ckpearson avatar Jul 31 '23 10:07 ckpearson

Yes this behavior will change in version 5.

JohannesKlauss avatar Aug 01 '23 08:08 JohannesKlauss