Non-blocking keyboard read

Jean-Paul Calderone exarkun at divmod.com
Tue Jun 26 17:02:59 EDT 2007


On Tue, 26 Jun 2007 22:37:16 +0200, Laszlo Nagy <gandalf at shopzeus.com> wrote:
>pinkfloydhomer at gmail.com wrote:
>> I am writing a curses application, but the getch() does not seem to
>> give me all I want. Of course, if I press "d", it returns an ord("d")
>> and so on. But I want to be able to detect whether alt, shift or ctrl
>> has been pressed also. Shift is normally covered by returning an
>> uppercase character instead and ctrl seems to return control codes (as
>> is normal, I guess), but alt I can't detect with getch, it seems.
>>
>> Preferably, I would  like one of two things:
>>
>> 1) Having a getch() (or other function) that returns a code like now,
>> but with different codes depending on the status of the ctrl, alt or
>> shift keys, for instance by setting higher bits or something. Just as
>> long as I can differentiate between "d", "D", ctrl+"d", alt+"d", shift
>> +"d" and maybe ctrl+alt+"d" and ctrl+shift+"d" etc.
>>
>You can try to combine select.select with sys.stdin. I have never tried
>this, but it is my platform independent idea.

select won't work on stdin on windows, and it won't work to read anything
less than a line on posix either, unless you put the pty into unbuffered
mode first (but then it will work).

Twisted has a more abstract API for this kind of thing which works on
POSIX and Windows which might be worth investigating.  Take a look at
stdin.py and stdiodemo.py, linked from 

  http://twistedmatrix.com/projects/core/documentation/examples/

Jean-Paul



More information about the Python-list mailing list