Search code examples
pythonconsolechatinterrupt

Python - react to custom keyboard interrupt


I am writing python chatbot that displays output through console. Every half second it asks server for updates, and responds to message. In the console I can see chat log.

This is sufficient in most cases, however, sometimes I want to interrupt normal workflow and write custom chat answer myself. I would love to be able to press a button (or combination) that would switch to "custom reply mode". What is the best way to do that, or achieve similar result?

Thanks a lot!


Solution

  • Using select.select() on sys.stdin will allow you to check if a key has been pressed at the terminal.