msvcrt.getch() overly receptive in idle?

David Bolen db3l at fitlinxx.com
Wed Jun 13 22:42:34 EDT 2001


David LeBlanc <whisper at oz.nospamnet> writes:

> 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.

It's not quite that simple.  Pythonw itself doesn't have an event
loop, it's just a standard interpreter compiled with options so that
no console gets created upon execution.  Idle does have events, for
example, via Tk, but aside from translating keyboard events into
actions for the UI, there's no general keypress queue ever seen as far
up as an executing script (which executes in a simulated interpreter).

So the event loop in question would be part of the Tk stuff in Idle
(or the win32ui stuff in Pythonwin), and I'm not sure how flexible
they are in terms of translating single key events into queued input
to the executing Python script.

Remember also that Python itself doesn't really have any single
character interface for I/O ... getch() is in an MS specific wrapper
module, so there aren't really any hooks in the Python interpreter
loop (which things like Idle/Pythonwin use) to queue up individual
keypresses from GUI events.  I'm sure you could queue up characters to
be seen as part of the next readline() or raw_input(), but that's not
the same single-character-wait functionality.

Of course, one could replace the msvcrt wrapper under the IDE
environments, and for the getch() call wait around for an internal
character event appropriate to the GUI in use, but its probably
overkill, and re-investigating what the getch() is being used for in
the first place is likely the most sensible thing to do.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list