Skip to content

[BUG] Duplicate activation of hotkey if another key event arrives before keyup #1013

@isti115

Description

@isti115

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 and keyup 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:

  1. Go to the provided example
  2. Roll your fingers over the 'asdf' keys quickly
  3. See that some of them are repeated in the output
  4. Press ctrl+b and then the i key as well without lifting up the ctrl and b keys
  5. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions