moses-palmer / pynput

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

Hotkey (function key) no longer works after update 1.7.2 -> 1.7.6 #476

Closed sp681 closed 2 years ago

sp681 commented 2 years ago

Description After updating pynput from version 1.7.2 to 1.7.6, the function key <f8> hotkey no longer works. Other hotkeys like <ctrl>+q work fine.

Platform and pynput version Windows 10, pynput 1.7.6

To Reproduce

from pynput import keyboard

def on_activate():
    print("Hotkey pressed")

def for_canonical(f):
    return lambda k: f(hotkeylistener.canonical(k))

hotkey_string = '<f8>'
hotkey = keyboard.HotKey(keyboard.HotKey.parse(hotkey_string), on_activate)
hotkeylistener = keyboard.Listener(
    on_press=for_canonical(hotkey.press),
    on_release=for_canonical(hotkey.release))
hotkeylistener.start()

while True:
    pass
xageorges commented 2 years ago

I think I encountered the same bug (W10 / pyinput 1.7.6 also). I have dug a little bit. My findings are below:

In Hotkey.press, the type of the key variable is not the same as its equivalent inside self._keys so they do not compare equal.

Considering the "f1" key:

but trying

Key['f1'] == KeyCode.from_vk(112)

yields the answer False in a Python terminal.

xageorges commented 2 years ago

PR #466 fixes this issue.

moses-palmer commented 2 years ago

Thank you for your report, and thank you @xageorges for pointing out the PR!

I have merged a similar patch, and verified that hotkeys containing function keys are now handled correctly. The fix will be included in pystray 1.7.7.

xageorges commented 2 years ago

@moses-palmer: it is I who thank you for your library!