moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.73k stars 243 forks source link

Additional keys can still trigger events #601

Closed Pandaft closed 2 months ago

Pandaft commented 2 months ago

Description Additional keys can still trigger events.

Platform and pynput version

To Reproduce The code comes from: https://pynput.readthedocs.io/en/latest/keyboard.html#global-hotkeys

from pynput import keyboard

def on_activate_h():
    print('<ctrl>+<alt>+h pressed')

def on_activate_i():
    print('<ctrl>+<alt>+i pressed')

with keyboard.GlobalHotKeys({
        '<ctrl>+<alt>+h': on_activate_h,
        '<ctrl>+<alt>+i': on_activate_i}) as h:
    h.join()

The expected result is that only pressing Ctrl+Alt+h will trigger it, but actually pressing Ctrl+Shift+Alt+h will also trigger it.

moses-palmer commented 2 months ago

Thank you for your report.

If your expectation is that pressing the hotkey combination and additional keys should not trigger the hotkey, I think that I will have to update the documentation. More specifically the description of the GlobalHotKeys class should explain that it is a convenience class built on top of a key listener, so it only has access to the keyboard events that arrive serially and gradually build its internal state; it does not have access to the keyboard state.

As a side note, please be aware that many keyboards are only able to distinguish three pressed key simultaneously, so separating ctrl+alt+h and ctrl+shift+alt+h may not be possible.

Pandaft commented 2 months ago

Thank you for your patient reply.

I think this is beyond my abilities, and I cannot achieve it on my own. Fortunately, I tried another library called 'keyboard' that meets my requirements. At the moment, I neither need to control the mouse nor the keyboard; I only need to monitor the keyboard, making the 'keyboard' library more suitable for my needs.

However, I still appreciate your help very much.