Name

getch, getche

Synopsis

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)

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.