Re: Non-blocking keyboard read
- From: Laszlo Nagy <gandalf@xxxxxxxxxxxx>
- Date: Tue, 26 Jun 2007 22:37:16 +0200
pinkfloydhomer@xxxxxxxxx wrote:
I am writing a curses application, but the getch() does not seem toYou can try to combine select.select with sys.stdin. I have never tried this, but it is my platform independent idea.
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, forI'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.
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?
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
.
- References:
- Non-blocking keyboard read
- From: pinkfloydhomer@xxxxxxxxx
- Non-blocking keyboard read
- Prev by Date: ironpython exception line number
- Next by Date: subprocess.Popen() problem
- Previous by thread: Non-blocking keyboard read
- Next by thread: Re: Non-blocking keyboard read
- Index(es):
Relevant Pages
|