Search code examples
pythonloopswhile-loopkeyboardkeyboardinterrupt

Is it possible to create a custom Keyboard Interrupt key in Python?


I am writing a python script that has an infinite loop and for me to stop I use the usual Keyboard Interrupt key Ctrl+c but I was wondering if I can set one of my own like by pressing space once the program can stop I want it to have the same function as Ctrl+c So if it is possible how can I assign it?


Solution

  • from pynput.keyboard import Listener
         while True:
             if keyboard.is_pressed("space"):
                 exit()