JohannesKlauss / react-hotkeys-hook

React hook for using keyboard shortcuts in components.
https://react-hotkeys-hook.vercel.app/
MIT License
2.68k stars 114 forks source link

[BUG] v5.0.0-1, "useKey" option also seems to (partially) activate "ignoreModifiers" #1168

Closed benwiley4000 closed 5 months ago

benwiley4000 commented 5 months ago

Describe the bug I'm using the "useKey" option on the v5 release, but when I do this, it's as if I also passed "ignoreModifiers: true". Setting "ignoreModifieres" explicitly to false doesn't fix the problem though.

To Reproduce Steps to reproduce the behavior: 1.

useHotkeys("mod+z", () => console.log("mod+z"), { useKey: true });
useHotkeys("mod+shift+z", () => console.log("mod+shift+z"), { useKey: true });
  1. Observe that mod+shift+z fires for "mod+z".
  2. However, mod+z does NOT fire for "mod+shift+z".

Please try and add a codesandbox or stackblitz to reproduce the bug: https://codesandbox.io/p/devbox/elegant-blackburn-nsmrq8?file=%2Fpackage.json%3A28%2C2

Expected behavior The hotkey hook should only fire for the described key combination

Desktop (please complete the following information):

Additional context I need this in order to support Ctrl/Cmd+Z on AZERTY keyboards, which place the Z in the position where "W" is on a QWERTY keyboard.

JohannesKlauss commented 5 months ago

Using useKey on a mod combination doesn't really make sense. useKey listens to the produced key and not the key code. In case of mod+z and shift+mod+z the produced key will just be z. As explained int here, you want to omit useKey in order to exactly listen to mod+z

benwiley4000 commented 5 months ago

Hey there sorry for the late reply. The problem is that if we don't use useKey, then mod+z actually detects mod+w on an AZERTY (French) keyboard. Which closes the window.. oops! I'm not using "useKey" for anything else, only this.

This behavior is desirable for implementing e.g. WASD nav where the key position is important, but indesirable for implementing shortcuts associated with specific letters e.g. mod+Z.

Ben

Le jeu. 25 avr. 2024, 10 h 45 a.m., Johannes Klauss < @.***> a écrit :

Closed #1168 https://github.com/JohannesKlauss/react-hotkeys-hook/issues/1168 as completed.

— Reply to this email directly, view it on GitHub https://github.com/JohannesKlauss/react-hotkeys-hook/issues/1168#event-12604422764, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADHOD3IQ5JOLSZJ2J5SG5UDY7C7EPAVCNFSM6AAAAABGJVJIUSVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJSGYYDINBSGI3TMNA . You are receiving this because you authored the thread.Message ID: <JohannesKlauss/react-hotkeys-hook/issue/1168/issue_event/12604422764@ github.com>

JohannesKlauss commented 5 months ago

You can use preventDefault: true to prevent the Window closing down. As said since mod plus a key doesn't produce its own key, useKey: true won't be of much help