Question:
How can I track the pressing of the keyboard button if the python script is running in the background?
Answer:
You can track button presses through the keyboard
module:
pip install keyboard
-
This is an example for tracking hot button clicks:
import keyboard def foo(): print('World') keyboard.add_hotkey('Ctrl + 1', lambda: print('Hello')) keyboard.add_hotkey('Ctrl + 2', foo) keyboard.wait('Ctrl + Q')
-
Tracking all button clicks:
import keyboard def print_pressed_keys(e): print(e, e.event_type, e.name) keyboard.hook(print_pressed_keys) keyboard.wait()