-
Notifications
You must be signed in to change notification settings - Fork 142
Description
Describe the bug
If I understand correctly, the current logic behind the activation of a hotkey is roughly as follows:
- The library keeps track of the currently pressed keys by monitoring
keydown
andkeyup
events. - On each event the registered hotkeys are checked against the currently pressed keys, and if their modifiers match and all the required keys are currently pressed, the handler fires.
The problem I'm facing arises when multiple keys are pressed in rapid succession. In such situations, when the user's fingers sort of "roll" on the keyboard, it can happen, that the next key is already pressed, before the previous one has been released. In such situations the current matching logic fires the handler of the first key multiple times, as the required conditions are still met when the next keydown
event arrives.
I'm not a hundred percent sure what the best approach would be to mitigate the issue, but at least making sure that the key that generated the event in the first place is part of the currently matched hotkey seems like a sensible starting point. If you agree with this, I'll prepare a PR with the required changes.
To Reproduce
Steps to reproduce the behavior:
- Go to the provided example
- Roll your fingers over the 'asdf' keys quickly
- See that some of them are repeated in the output
- Press
ctrl+b
and then thei
key as well without lifting up thectrl
andb
keys - Observe, that the boldness of the text got toggled twice
Please try and add a codesandbox or stackblitz to reproduce the bug:
https://codesandbox.io/s/react-hotkeys-hook-issue-1013-9hgd71
Expected behavior
I would expect a single press of any given key to result in a single invocation of the assigned handler function, no matter what other keys are pressed before or after.
Desktop (please complete the following information):
- OS: Linux
- Browser: Firefox
- Version: 102
Edit: I have by accident meanwhile stumbled upon #995, because it didn't occur to me to search the discussions as well as the issues before posting this, so this is in some sense a duplicate, but a much more detailed one.