msvcrt.getch() overly receptive in idle?

David LeBlanc whisper at oz.nospamnet
Wed Jun 13 21:18:01 EDT 2001


In article <u4rtl46mf.fsf at ctwd0143.fitlinxx.com>, db3l at fitlinxx.com 
says...
> fpm at u.washington.edu (Frank Miles) writes:
> 
> > I'm trying to get getch() working in Windows (Python 2.0).  It seems to
> > work fine outside of Idle, but inside Idle it immediately returns a
> > character with the value 255.
> > 
> > Can anyone point me to the obvious solution?  Thanks!
> 
> I expect that getch() requires a working "console" window, and when
> you run Idle you don't get one as its set to use pythonw.exe.  Thus,
> it's technically not able to process any keystrokes and returns
> immediately.
> 
> You could try to verify that by starting Idle from the command line
> with the standard python.  (But you may have to be in that console
> window to trigger the getch() to return).
> 
> A GUI doesn't really have individual keystrokes sent as console
> characters, but instead receives events upon keypresses.  So a getch()
> call in a GUI application is sort of non-sensical (within the GUI
> framework).
> 
> To do the equivalent in a GUI, you'd need to wait for a keypress
> event.  I'm not sure how easy or not it would be to tie into Idle's
> event loop to notice such a thing.
> 
> --
> -- David
> 
One could certainly put in an "any key" event at the bottom of the list 
of other interesting keys (or all keys and no other key events and 
dispatch manually for "interesting" keystrokes and pass on the ones not 
interesting to this level of the app (ctrl-c for example)). The default 
action would be to pass the event on down the event chain. Alternatively, 
the event is enabled and "interesting" keys are processed. Sounds right 
about like a key-configurable editor would do it... This could be added 
to Idle's or Pythonw's event loop easily enough, although the pythonw 
route would have to be approved for inclusion by the snake wranglers 
imho.

getch() only makes sense in a procedurally driven interface where the 
program controls the interaction with the user. In an event driven system 
where the program waits for events to be generated by the user, it 
doesn't - but if needed, could be emulated in a texteditor control.

Regards,

Dave LeBlanc



More information about the Python-list mailing list