I'm trying to toggle a boolean with the help of a seperate function. If my research is correct, there are no pointers in Python (I'm starting to learn it right now) so I'm unsure how to fix this issue.
For key presses i am using the library 'keyboard' which uses callbacks for hotkeys. Thus i have to somehow pass the toggle as a callback and need a method for toggling it. (If you have a different approach wihich could solve this its also appreciated)
Doing it inside the while loop like the shutdown call doesn't work. because the keyboard library doesn't have a simple method to check for key press or key release so it would be called repeatedly until the key is released.
import keyboard
# Global Variables
running = False
# ------------
# FUNCTIONS
# ------------
def shutdown():
print("Tool shutting down!")
quit()
def toggle(b):
b = not b
def init():
running = False
keyboard.add_hotkey('alt+1', toggle, args=[running])
# ------------
# MAIN PROGRAM
# ------------
init()
while True:
if keyboard.is_pressed('alt+q'):
shutdown()
The Variable should be consistent at least in the main program itself (It can be also consistent globally, which was my latest approach that didn't work out) and be toggled once i use my set hotkey.