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

Christian Gollwitzer auriocus at gmx.de
Fri Oct 28 01:34:33 EDT 2016


Am 28.10.16 um 07:05 schrieb Michael Torrie:
> On 10/27/2016 04:07 AM, Terry Reedy wrote:
>> As I and others have said, those keyboard functions are not available on
>> text terminals.  I predict that keyboard functions that so not work on
>> all systems will never become built-ins.  But some are available with an
>> import.
>
> Sure you can't get a keyboard scancode when you're in terminal. But you
> can get "keystrokes" as it were, without having to read an entire line
> from standard in.  I use editors and programs all the time which are
> interactive (they don't buffer keyboard input into lines) in the
> terminal.  vim, nano, pico, mc, etc.
>
> Is this not what BartC is talking about?  A way of reading in
> "keystrokes" in a terminal.

You can do this, if you want, by setting the terminal to raw mode. On 
Linux and OSX you can call "stty" to do that which should not be too 
difficult. Then you need a second thread or switch to non-blocking I/O 
from stdin, maybe asyncio can do that. On Windows OTOH you have to 
switch to a whole different API instead of reading from a file descripor 
AFAIK.

I still believe that it is not a "basic functionality". You need it, if 
you want to program a text editor or similar thing, but without using a 
real GUI. This is a small niche. It is both easier and more functional 
to use a real GUI library.

	Christian



More information about the Python-list mailing list