I am developing a console app and I want to make it so that pynput.keyboard.Listener
is only be active when the user has selected the terminal window where the project is running.
I generally have it working:
def listener(MP: Process):
def actions( key):
if str(key) == "Key.ctrl_l":
print("shutting down ...")
MP.terminate()
return False
if str(key) == "'s'":
if __name__ == '__main__':
p = Process(target=recCommand) # recCommand doesnt use the listener or input() to recive commands
p.start()
def runListener():
listener = Listener(on_press = actions)
listener.start()
runListener()
if __name__ == '__main__':
MP = Process(target=mainloop, args=(debug,)) # from the multiprocessing module
MP.start()
listener(MP=MP)
this serves its purpose but the listener is still active and reacts when ever I use the 's' or 'ctrl_l' on any other app, while the program is still running, which I want to stop.