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] Callback called on each matched key in array #1160

Closed oceandrama closed 5 months ago

oceandrama commented 6 months ago

Describe the bug If user sets multiple keys in one hook like this:

useHotkeys(['Shift+U', 'Shift+U'])

callback will be triggered on each key

Expected behavior Callback triggered once on the first matched key

Additional context It happens because of forEach in this line https://github.com/JohannesKlauss/react-hotkeys-hook/blob/9bfa17c69dc407f10ecfb0aa18d7ccf536da8d4b/src/useHotkeys.ts#L85 instead of finding first matched key

JohannesKlauss commented 5 months ago

Your example contains the same key combinations, so it will trigger on both or none.

oceandrama commented 4 months ago

I made this example for easier reproducing. It happens on Windows if you use different keyboard layouts like

useHotkeys(['Shift+Г', 'Shift+U'])

If you press Г (which on the same place with U) callback calls twice, but if you remove it, it doesn't call on this layout Find first matching key will be very helfull in this situation

JohannesKlauss commented 4 months ago

I think I don't quite understand your use case. What are you trying to achieve? If you listen to the same key twice, the callback will fire twice. So, what are you trying to achieve by listening twice for the same combination in the first place?