Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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


Solution 2:

Given the comments in the previous answer, you need a non-blocking function to tell whether any keys were pressed rather than something that triggers as soon as the key was pressed.

I would therefore recommend using some of the terminal APIs available on your OS. Typically this would be curses or the win32 console API. However, I have written a common wrapper to both in asciimatics. The get_event() method on the Screen should provide a simple cross-platform way of getting mouse and keyboard events. To see if it was a keyboard event, check the type of returned event. If there was no event, you'll get a return code of None, but if it was a key pressed, you'll get KeyboardEvent.


Post a Comment for "Python - React To Custom Keyboard Interrupt"