help for using msvcrt.kbhit() with Python 2.3 editors?
Mark Hammond
mhammond at skippinet.com.au
Wed Aug 13 19:03:45 EDT 2003
dbrown2 at yahoo.com wrote:
> I'm having some trouble using kbhit inside the editor enviroments. I'm
> using win2000 with Python2.3 and win32all v157 versions.
>
> Any pointer on how this should be handled would be appreciated. Is
> there another non-blocking way to detect a key press?
>
> Here's the code that is causing the trouble. If I run this by
> double-clicking then it works as expected. But if I run from IDLE or
> PythonWin it basically never returns. msvcrt.kbhit() always returns 0
> as far as I can tell by adding print statements. I can't confirm it
> but I seem to recall this worked at least with the PythonWin editor
> environment under my previous Python 2.2 setup.
>
> ###########
> import serial # access to serial port
> import time # sleep command
> import msvcrt # detect keyboard key press events
>
> ser = serial.Serial(0, 4800, timeout=0.25)
> data = ''
> while not msvcrt.kbhit():
> buf = ser.read(10)
> if buf <> '': print buf
> #time.sleep(1)
> data = data + buf # concatenate buf onto data
> ser.close()
> ###########
This will not work for Windows programs. Keystrokes are delivered to
GUI programs via windows messages, not via stream type functions. Thus,
having a GUI program with no existing message loop detecting key presses
is quite difficult.
Mark.
More information about the Python-list
mailing list