Re: Non-blocking keyboard read

2007-06-26 Thread Laszlo Nagy
[EMAIL PROTECTED] 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.
 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Non-blocking keyboard read

2007-06-26 Thread Jean-Paul Calderone
On Tue, 26 Jun 2007 22:37:16 +0200, Laszlo Nagy [EMAIL PROTECTED] wrote:
[EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Non-blocking keyboard read

2007-06-20 Thread [EMAIL PROTECTED]
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

-- 
http://mail.python.org/mailman/listinfo/python-list