Non-blocking keyboard read

P

pinkfloydhomer

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
 
L

Laszlo Nagy

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.
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.
I'm affraid this is not possible from the base Python installation. You
need to use external modules, but there are many. E.g. under windows,
you can use the win32 extensions. Under X window, probably you can use a
python/gnome module. I would also look at PyGame, I guess it has
functions that can read the keyboard state. (Never tried...) I'm not
sure about console mode programs though.
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?

Under unix, the non-curses-method will end up in reading a file. The
curses-method will do exactly the same. Problems will come out when you

1. use something at low level. For example, modifying the internal
keyboard buffer...
2. try to read from different threads/processes (why would you do that?)

Best,

Laszlo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top