Is there a way to send an interrupt to a python module when the user inputs something in the console? For example, if I’m running an infinite while loop, i can surround it with a try/except for KeyboardInterrupt and then do what I need to do in the except block.
Is there a way to duplicate this functionality with any arbitrary input? Either a control sequence or a standard character?
Edit: Sorry, this is on linux
Dependent on the operating system and the libraries available, there are different ways of achieving that. This answer provides a few of them.
Here is the Linux/OS X part copied from there, with in infinite loop terminated using the escape character. For the Windows solution you can check the answer itself.
Edit: Changed the character detection to use characters as defined by
curses.ascii, thanks to Daenyth’s unhappiness with magic values which I share.