-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Duplicate activation of hotkey if another key event arrives before keyup #1013
Comments
Hey @isti115 we've observed the same issue. Did you get any luck trying to fix this? |
I didn't go too far, as we didn't end up using this library, but the following patch was usable as a quick and dirty workaround: diff --git a/node_modules/react-hotkeys-hook/dist/react-hotkeys-hook.esm.js b/node_modules/react-hotkeys-hook/dist/react-hotkeys-hook.esm.js
index a3896d0..223765a 100644
--- a/node_modules/react-hotkeys-hook/dist/react-hotkeys-hook.esm.js
+++ b/node_modules/react-hotkeys-hook/dist/react-hotkeys-hook.esm.js
@@ -195,6 +195,11 @@ var isHotkeyMatchingKeyboardEvent = function isHotkeyMatchingKeyboardEvent(e, ho
altKey = e.altKey;
var keyCode = mapKey(code);
var pressedKey = pressedKeyUppercase.toLowerCase();
+
+ if (!keys.includes(keyCode)) {
+ return
+ }
+
if (!ignoreModifiers) {
// We check the pressed keys for compatibility with the keyup event. In keyup events the modifier flags are not set.
if (alt === !altKey && pressedKey !== 'alt') { It essentially just prevents events from participating in the firing of callbacks that don't actually have the event's key among their triggers. |
Thanks a lot @isti115 @JohannesKlauss is that something you would accept merging if I raise a PR? thanks! |
Absolutely. I sadly don't have a lot of time on my hand to contribute much currently. |
Describe the bug
If I understand correctly, the current logic behind the activation of a hotkey is roughly as follows:
keydown
andkeyup
events.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:
ctrl+b
and then thei
key as well without lifting up thectrl
andb
keysPlease 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):
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.
The text was updated successfully, but these errors were encountered: