March 2003
Intermediate to advanced
656 pages
39h 30m
English
getch, getche
getch( ) getche( )
Reads and returns one character from keyboard input, waiting if no
character is yet available for reading. getche
also echoes the character to screen (if printable), while
getch doesn’t. When the user
presses a special key (arrows, function keys, etc.),
it’s seen as two characters: first a
chr(0) or chr(224), then a
second character that, together with the first one, defines what
special key the user pressed. Here’s how to find out
what getch returns for any key:
import msvcrt
print "press z to exit, or any other key to see code"
while 1:
c = msvcrt.getch( )
if c = = 'z': break
print "%d (%r)" % (c, c)