I am trying to make a simple IRC client in Python (as kind of a project while I learn the language).
I have a loop that I use to receive and parse what the IRC server sends me, but if I use raw_input
to input stuff, it stops the loop dead in its tracks until I input something (obviously).
How can I input something without the loop stopping?
Thanks in advance.
(I don't think I need to post the code, I just want to input something without the while 1 loop stopping.)
EDIT: I'm on Windows.
With python3.3 and above you can use the
asyncio
module as mentioned in this answer. You will have to re factor your code though to work withasyncio
. Prompt for user input using python asyncio.create_server instanceI think curses library can help.
For Windows, console only, use the
msvcrt
module:For Linux, this article describes the following solution, it requires the
termios
module:For cross platform, or in case you want a GUI as well, you can use Pygame:
On Linux, here's a refactoring of mizipzor's code that makes this a little easier, in case you have to use this code in multiple places.
Here's how to use this: This code will print a counter that keeps growing until you press ESC.
This is the most awesome solution1 I've ever seen. Pasted here in case link goes down:
1 Made by Simon D. Levy, part of a compilation of software he has written and released under the Gnu Lesser General Public License.
I'd do what Mickey Chan said, but I'd use
unicurses
instead of normal curses.Unicurses
is universal (works on all or at least almost all operating systems)