kbhit() and getch() question

Jeremy Reed jp_reed at yahoo.com
Thu Mar 8 11:21:37 CET 2001

In a previous question, I asked how to get non-blocking input from the user.
The answer proposed is as follows:

import msvcrt

ch = 0
while ch != 'x':
    if msvcrt.kbhit():
        ch = msvcrt.getch()
        print ch

I have run into a few problems.  First of all, the kbhit() doesn't seem to
work.  I am unable to find a way for the function to return true.

Second, the getch() function is listed in the documentation as a blocking
call, but it doesn't block when i run it in the following script.

ch = 0
while ch != 'x':
    ch = mscvrt.getch()
    print ch

Instead, it prints the equivalent to chr(255) in an infinite loop.

Is there some setup call or initialization function that I'm neglecting to
call?  Why aren't these function working like the documentation says they

More information about the Python-list mailing list