Why doesn't Python include non-blocking keyboard input function?

Terry Reedy tjreedy at udel.edu
Wed Oct 26 20:33:44 EDT 2016


On 10/26/2016 8:33 AM, Marko Rauhamaa wrote:
> BartC <bc at freeuk.com>:

> Say you want to implement a simple, character-based shooting game where
> the two guns are operated by the [Shift] keys. Unfortunately, the Unix
> terminal API doesn't make that possible. You need to get the keyboard
> events from some other API. In practice, your only choice is X11/Wayland
> (on Linux).

Or tk(inter), which uses X11 on *nix and other stuff on OSX and Windows. 
  I verified that it gives access to the Shift keys as keys in 
themselves, rather than just as modifiers.

import tkinter as tk
root = tk.Tk()
def lshift(event): print('shift-l')
def rshift(event): print('shift-r')
root.bind('<Key-Shift_L>', lshift)
root.bind('<Key-Shift_R>', rshift)
root.mainloop()

does what I hoped, with autorepeat when the key was held down.

>> It's more building a mountain of complexity around something that
>> ought to be straightforward.
>
> Maybe there should be some way to get the raw events from the PTY.

PTY?  Must be Linux-specific.  Most beginners are not on Linux.

> However, next you'd start wanting the mouse events and pixel-level color
> controls. It starts to look like a GUI application.
>
> But what would be wrong in a GUI PTY API? No windowing,

'No windowing'?  Python normally runs in a text widget in a window, 
programmed to emulate a dumb terminal.  The request for non-blocking 
access to user actions is a request for something smarter.

> just a regular
> character display where you could draw pictures and interpret the inputs
> directly à la Apple II or Commodore 64.

I never touched an Apple II and only briefly a Commodore 64, so either 
never knew or have forgotten the Basic commands they had.  But it should 
be possible to emulate at least the text screen of either with tkinter. 
(Graphics modes with pixel peek and poke might also be possible with a 
Canvas, but I won't claim that without knowing more.)  It should then be 
possible to translate old games into Python.

Maybe something like this has been done?

> It would make teaching programming much more fun, too.

The logo-in-Python turtle module is already used for this.

What would be doable and possibly useful would be a textscreen module 
with a Screen class that does as much boilerplate for people as 
possible.  For my proof-of-concept above, something like

from textscreen import Screen
def lshift(event): print('shift-l')
def rshift(event): print('shift-r')
Screen().go

should be enough.  The go method should import tkinter, create root, add 
a Text, scan globals() for functions with key names (and names of mouse 
actions), bind any that are found, and start mainloop.

This sort of thing would be even nicer if and when tcl/tk gains support 
for the full unicode character set so that all the non-BMP emoji are 
potentially available.  (A font supporting such would also be needed.)

-- 
Terry Jan Reedy





More information about the Python-list mailing list