Keyboard hook in linux

K. Elo maillists at nic.fi
Sun Jan 13 17:13:28 CET 2013


Hi!

I am working on a small console app for linux. The idea is to display 
some sensor values and the screen should update itself in, say, every 10 
seconds.

The user should have the possibly to change some configurations or gwt 
help by pressing different keys (like you can do when running e.g. 
'top'). In other words: the program should NOT wait for the keypress (so 
input() is not the solution), but simply capture keypresses and act 
accordingly. If no key is pressed, the program should continue updating 
the screen.

Practically I am looking for something similar than Pascal's 
"keypressed" function 
(http://www.freepascal.org/docs-html/rtl/crt/keypressed.html). The 
python code would be something like this:

--- snip ---

while True:
   if keypressed:
     ch=screen.getch()   # From 'curses'
     # Test, if 'ch' is a valid key
     # Do what the user want
   read_sensors()
   update_screen()

--- snip ---


I have searched in the Web and in several tutorials (e.g. "Programming 
python"), but this seems to be a tricky one. The 'pyHook' library seems 
to offer a keyboard hook manager, but 'pyHook' is not available for 
linux :( IMHO, the 'curses' library offers no (direct) solution to this...

Does anybody have an idea / a solution, how to capture keystrokes in linux?

Kind regards,
Kimmo



More information about the Python-list mailing list