moses-palmer / pynput

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

Hotkeys <space> and <tab> not working #565

Open JanBeelte opened 11 months ago

JanBeelte commented 11 months ago

Description Keys like <ctrl> and all "normal ascii characters" are working, while <space> and <tab> are broken. Tested on the following Keyboard-Layouts:

the behavior is the same on both.

Platform and pynput version Linux jan-desktop 5.15.122-1-MANJARO #1 SMP PREEMPT Tue Jul 25 07:09:46 UTC 2023 x86_64 GNU/Linux pynput==1.7.6

To Reproduce

from pynput import keyboard

# Minimal example to reproduce the <space> and <tab> bug:
def trigger_working():
    print("working")

def trigger_not_working():
    print("never seen")

listener = keyboard.GlobalHotKeys(
    {
        "<space>": trigger_not_working,
        "<tab>": trigger_not_working,
        "a": trigger_working,
        "<ctrl>": trigger_working,
    }
)

with listener:
    listener.join()
abelkadii commented 9 months ago

the space and tab keys are not added to _MODIFIER_KEYS in https://github.com/moses-palmer/pynput/tree/master/lib/pynput/keyboard/init.py line 42

JanBeelte commented 9 months ago

Thanks for the input! Not sure this is the correct explaination, in my use-case I want to listen for e.g. "<ctrl>+<space>" or "<ctrl>+<tab>". While debugging the issue I realised that even "<space>" and "<tab>" are not working. Instead of having them as modifier keys I expect to have them as "normal keys" that behave like "a" but this does not seem to be the case. Any pointer on why this could be the case?

moses-palmer commented 8 months ago

Thank you for your report.

When I run your script and press a, ctrl, space and tab in that order, I get the following printout:

working
working
never seen
never seen

I changed the space hotkey to ctrl + space, and that also worked. This, however, was when running under Wayland, so it only worked while an application running under XWayland had focus. The fact that it works indicates that the state managing code is not to blame.

Could you please add logging to pynput.keyboard.HotKey.press, just after self._state.add(key)? The value of self._state compared to self._keys might show what is wrong.