pynput icon indicating copy to clipboard operation
pynput copied to clipboard

Fix for HotKey false activations with lower modifiers

Open chris-beedie opened this issue 3 years ago • 3 comments

If you have multiple hotkeys defined with the same key, but different modifiers you can have false activations. e.g. if we define hotkeys:

<ctrl>+a
<shift>+a
<ctrl>+<shift>+a

All three will be triggered when pressing ctrl+shift+a. This is due to state being held locally in the hotkey and keys only added to the state if they exist as part of the hotkey combo.

I've removed the key in self._keys check before adding to state as a quick fix, but maintaining state globally may be a better solution.

chris-beedie avatar Sep 24 '20 16:09 chris-beedie

Thank you for your contribution!

However, I'm not entirely certain that the current behaviour is a bug. It is easy to reason about; a hot-key will be triggered once its keys are pressed. If I press and hold A, Shift, Ctrl or Ctrl, Shift, A, the hot-keys will be triggered, albeit in different order.

A possible modification of the current behaviour, that I think you would agree on, is to special-case modifiers; trigger hot-keys only when the non-modifier key is pressed, and only the registered modifiers are simultaneously pressed.

moses-palmer avatar Sep 24 '20 20:09 moses-palmer

ok, I see your point. I had only really considered the hotkeys in the more traditional form of modifiers followed by action key. The current implementation does allow for something a bit more exotic. A special case to handle the more traditional use case would suit me, and I'd imagine the majority of use cases

chris-beedie avatar Sep 24 '20 20:09 chris-beedie

I was about to open a bug report for this when I noticed this pull request. I can't think of any application that allows for a hotkey like +a to activate when you press +a. It's counter-intuitive to have it work otherwise IMHO

btonasse avatar Feb 18 '22 06:02 btonasse