473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Non-blocking keyboard read

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

Jun 20 '07 #1
1 5269
pi************@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.
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

Jun 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.