loop until keypress (Windows XP)
Gabriel Genellina
gagsl-py at yahoo.com.ar
Thu Aug 10 01:37:08 EDT 2006
At Thursday 10/8/2006 02:19, placid wrote:
>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!
If your script only needs to be run on Windows -as the subject
suggests- you can use the msvcrt module:
from msvcrt import kbhit,getch
stop = False
while not stop:
print "Hello world!"
if kbhit(): stop = getch()=='q'
kbhit() is used to detect when a keypress is waiting, so the next
getch() will not block.
Gabriel Genellina
Softlab SRL
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
More information about the Python-list
mailing list