Anyone know of a way to get non-blocking keyboard input?

Ben Hutchings ben.hutchings at roundpoint.com
Tue Feb 27 18:54:55 EST 2001


Jeremy Reed <jpreed00 at MailAndNews.com> writes:

> I am looking to create a telnet client that will, by using the select() 
> statment, poll an open socket for data to be received while at the same time 
> keep accepting keyboard input from the user.
> 
> I have tried to run a thread in the background that collects input, but I 
> never can figure out how to achieve the 'fluidity' that I desire--i.e. no 
> stopping for a return-key press.

I think this ought to work for some platforms:

    try:
        istty = sys.stdin.isatty()
    except AttributeError:
        istty = 0

    if istty:
        if os.name=='posix':
            import tty
            tty.setraw(sys.stdin.fileno())
        elif os.name=='nt':
            import win32file, win32con
            hstdin = win32file._get_osfhandle(sys.stdin.fileno())
            modes = (win32file.GetConsoleMode(hstdin)
                     & ~(win32con.ENABLE_LINE_INPUT
                         |win32con.ENABLE_ECHO_INPUT))
            win32file.SetConsoleMode(hstdin, modes)

Unfortunately, GetConsoleMode, SetConsoleMode, and associated
constants don't seem to be included in win32all yet!

-- 
Any opinions expressed are my own and not necessarily those of Roundpoint.



More information about the Python-list mailing list