[Tutor] How to use getch()?

Dick Moores rdm at rcblue.com
Mon Aug 28 01:07:42 CEST 2006

Alan Gauld has also been trying to help me by direct email. But his suggested

import msvcrt
c = 0
while True:
     c += 1
     if msvcrt.kbhit():
        if msvcrt.getch() == 'k':
     else: continue
print c

didn't work for me either. I was just about to tell him so, when I 
remembered to try to run it not using IDLE, i.e., by just clicking on 
the script icon (to use the console--is that the term?)  I knew I'd 
have to add a line something like

raw_input("\n\nPress the enter key to exit.")

and when I did the script ran just as I'd hoped!

My thanks to Kent and Alan.

Dick Moores

More information about the Tutor mailing list