boppreh/keyboard

hot_key not triggered in multi_threading environment

latesnow opened this issue · 0 comments

I'm trying to write a multi-threaded program where one thread is dong a task on the background. Once a hotkey is pressed, the whole program should be terminated. The script I tested is as below:

keyboard.add_hotkey('p', key_pressed)
while flag:
    time.sleep(1)

function key_pressed prints a message and sets flag to False on invocation. The above script works. But if I change it to:

t = Thread(target=show_position)
t.run()
keyboard.add_hotkey('p', key_pressed)
while flag:
    time.sleep(1)

No msg is printed and the program never stops.
Does anyone have any idea about this?