[Idle-dev] non-blocking getch() or kbhit() in IDLE

Tal Einat taleinat at gmail.com
Wed Sep 5 09:37:46 CEST 2007


On 9/5/07, Douglas S. Blank <dblank at brynmawr.edu> wrote:
> Is there a way in IDLE to check to see if a key has been pressed without
> blocking? Either cross-platform or otherwise? I'd like to have students
> write code like:
>
> while not keyPress():
>      robot.forward(1)
>      wait(.1)
>
> I was exploring the idlelib.rpc.RPCProxy object at sys.stdin, but didn't
> see anything obvious.
>
> Any ideas appreciated,
>
> -Doug

In the IDLE shell you don't have access to any of the Tk events, since
it is run in a separate process. So you can't directly check for
keyboard events. And IDLE doesn't send input to the subprocess until a
code block is finished, so you can't check for single characters in
the input stream.

If you want to use IDLE's event loop, you'll have to write an
extension for this. You can also write something else which binds to
keyboard events, but then you'll have to make sure it gets notified of
the events ( i.e. IDLE doesn't "swallow" them first).

If you run IDLE without a subprocess then you can use its event loop
directly, but that would be ugly.

- Tal


More information about the IDLE-dev mailing list