We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

How do I check for a keypress without blocking?

Use the msvcrt module. This is a standard Windows-specific extension module. It defines a function msvcrt.kbhit which checks whether a keyboard hit is present, and msvcrt.getch which gets one character without echoing it.

import msvcrt

while 1:
    c = msvcrt.getch()
    print "Got character", repr(c)
    if c == "q":
        break # quit

CATEGORY: windows