I am trying to write a script that will stay in a loop and infinitely type random characters using pynput's press and release functions. However, I am also trying to detect hotkeys (e.g. ctrl+alt+q to exit the script) to start, stop, and pause the script. The issue is that my code is detecting the press and release functions instead, which are interfering with the hotkeys the user presses. Like I'll type ctrl+alt and pynput will type a "j", which is not a valid hotkey, and then when I type my "q", it isn't detected and the script refuses to exit. Is there a way to distinguish between the two so I can tell what is being typed by the user and what is coming from pynput?
This is the relevant code. As you can see, I'm using the keyboard module's add_hotkey function to detect key presses, and apparently it also detects key presses from the type_char function.
# Uses the pynput module
# Simulates a key press to send the character to the screen
def type_char(char):
pynput_keyboard.press(char)
pynput_keyboard.release(char)
# Uses the keyboard module
def initialize_hotkeys():
keyboard.add_hotkey("ctrl+alt+t", lambda: type_infinitely_threaded())
keyboard.add_hotkey("ctrl+alt+q", lambda: exit_script())