1

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.

KeMeK
  • 25
  • 1
  • 6
  • `pynput` doesn't have method to send only to one window. It sends it to system (Windows, Linux, Mac) and system automatically sends it to active window. You would need other modules to check what window is active and skip key when it is not terminal. – furas Sep 13 '21 at 16:41
  • [Obtain Active window using Python](https://stackoverflow.com/questions/10266281/obtain-active-window-using-python) – furas Sep 13 '21 at 16:43

0 Answers0