raw_input in loop

Michael Hudson mwh21 at cam.ac.uk
Thu May 4 16:35:28 EDT 2000


robin.escalation at ACM.org writes:

> "Robert Cragie" <rcc at nospamthanks_jennic.com> wrote:
> 
> >There is also the msvcrt module - function kbhit() might be what you're
> >after...
> >
> >http://www.python.org/doc/current/lib/msvcrt-console.html
> 
> That's fine for WinXX, but how about other platforms? I hate to lock
> myself into GatesLand solutions.

On unix you can have lots of fun with termios; something like this?

import termios
import TERMIOS
import select

def kbhit():
    oldattr = termios.tcgetattr(0)
    try:
        attr = termios.tcgetattr(0)
        attr[3] = attr[3] & ~(TERMIOS.ICANON|TERMIOS.ECHO)
        termios.tcsetattr(0,TERMIOS.TCSANOW,attr)
        while 1:
            r,_,_ = select.select([0],[],[],0)
            if r:
                return 1
            else:
                return 0
    finally:
        termios.tcsetattr(0,TERMIOS.TCSANOW,oldattr)

This sort of thing's inherently unportable though.

bear-in-mind-I-came-up-with-that-merely-by-playing-around
-not-through-any-deep-knowledge-ly y'rs
M.

-- 
58. Fools ignore complexity. Pragmatists suffer it. Some can avoid
    it. Geniuses remove it.
     -- Alan Perlis, http://www.cs.yale.edu/~perlis-alan/quotes.html



More information about the Python-list mailing list