loop until keypress (Windows XP)

placid Bulkan at gmail.com
Thu Aug 10 02:25:58 EDT 2006


Gabriel Genellina wrote:
> 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.

Thanks for the solution ive got it working.  You were correct, the
script needs to run only on Windows.

Cheers (and thanks all for the replies)




More information about the Python-list mailing list