Wait for a keypress before continuing?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Aug 8 02:27:59 CEST 2011


John Doe wrote:

> My program does not need a prompt, it just needs to wait for any
> key to be pressed before it continues. This is in Windows.
> 
>   char=0
>   while not char:
>     char=msvcrt.getch()
> 
> That doesn't delay anything here.

Works perfectly for me. You don't need the while loop, since getch blocks
until a key is pressed.

Rather than making arbitrary changes to the code, try printing char after
the loop exits and see what it contains. That may give you a hint as to
what is going on.

Also, are you using an IDE? If so, it could very well be interfering with
the keyboard buffer, for its own purposes. E.g. in IDLE 2.6.2, if I call
getch it *immediately* returns without blocking:

>>> msvcrt.getch()
'\xff'



-- 
Steven




More information about the Python-list mailing list