loop until keypress (Windows XP)

placid Bulkan at gmail.com
Thu Aug 10 01:19:24 EDT 2006


Hi all,


Im using the cmd module and i have command that loops and keeps on
printing text, what i want to be able to do is loop until the user
presses a particular key, say Q/q ? I tried the following code;


line = raw_input ("press q to abort")
while line[0] != "q":
    """ keep printing text """
    line = raw_input ("press q to abort")

but raw_input blocks for input until the newline char. So i then tried
the following code


import sys

chr = sys.stdin.read(1)
while chr != "q":
    """ keep printing text """
    chr = sys.stdin.read(1)

but again this blocks too.

is there a way to do this, wait for user input but dont block? I could
use a thread that just does the previous code block but i already have
three Thread classes, its just getting too complex with threads!

Cheers




More information about the Python-list mailing list