Non-blocking keyboard read

pinkfloydhomer at gmail.com pinkfloydhomer at gmail.com
Wed Jun 20 06:30:02 EDT 2007


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.

2) Having a way to read the keyboard status at any given time, for
instance just reading a dictionary (or whatever) of bool for each key.
So that key[KEY_D] == true when d is being pressed, and key[LEFT_ALT]
== true when left alt is being pressed etc.

1) can of course be created from 2) which is lower level, so I would
prefer 1) to save me some work.

Also, are there problems with using a non-curses method of reading
keyboard input, within a curses application?

/David




More information about the Python-list mailing list