[Tutor] msvcrt or else?

alan.gauld@bt.com alan.gauld@bt.com
Fri Mar 28 12:58:02 2003


> def  hello():
>     n = raw_input('your name?>>')
>     if n == 'q':
>         sys.exit()
> How to replace "if" conditional with single press on "Esc" key?
> Would it be the same solution in unix?

You can use the msvcrt.getchar() function in a loop to read 
characters until you get an ESC. You need to append the chars 
you read to the input string and take account of empty 
input buffers etc so its not trivial but ultimately does 
make for a more dynamic UI.

You might find my event-driven programming topic useful 
(especially if you have the paper book version coz it actually
uses msvcrt!).

Alan g.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld/