moses-palmer / pynput

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

hotkeys cannot be executed correctly #529

Closed HWTsang closed 1 year ago

HWTsang commented 1 year ago

I press ctrl+e, ctrl+r and ctrl+t respectively. My expectation is

1
1 end
2
2 end
3
3 end

But the actual result is

1
1 end
2
2 end
2
2 end
3
3 end

windows11 pynput1.7.6

import time

from pynput import keyboard

def replay(sec: int):
    def wrap():
        print(sec)
        time.sleep(sec)
        print(sec, 'end')

    return wrap

keys = {'<ctrl>+e': replay(1), '<ctrl>+r': replay(2), '<ctrl>+t': replay(3)}

global_hotkeys = keyboard.GlobalHotKeys(keys)
global_hotkeys.start()
while True:
    time.sleep(1)
moses-palmer commented 1 year ago

Thank you for your report.

Please consult the documentation; long running tasks must not be executed in the event callbacks. Your script works if you remove the time.sleep call.