Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Keyboard hook in linux

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


maillists at nic

Jan 13, 2013, 8:13 AM

Post #1 of 5 (832 views)
Permalink
Keyboard hook in linux

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
--
http://mail.python.org/mailman/listinfo/python-list


torriem at gmail

Jan 13, 2013, 8:42 AM

Post #2 of 5 (761 views)
Permalink
Re: Keyboard hook in linux [In reply to]

On 01/13/2013 09:13 AM, K. Elo wrote:
> 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.

'top' is built using a programming library called 'curses' or 'ncurses.'
That's likely where you'll need to go to. At least if you want to be
able to run properly on different terminals. ncurses handles terminal
output including writing text at certain positions, and input/output,
and even mouse interactions if you felt you needed that. It's
apparently part of the standard python library:
http://docs.python.org/2/library/curses.html


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


torriem at gmail

Jan 13, 2013, 8:46 AM

Post #3 of 5 (765 views)
Permalink
Re: Keyboard hook in linux [In reply to]

On 01/13/2013 09:13 AM, K. Elo wrote:
> 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...

You're wrong. curses does offer a direct solution to this. Check the
docs. Also here's a nice intro document for Python 3:
http://docs.python.org/dev/howto/curses.html

You can check to see if a keystroke is waiting, grab it and then do
something useful. curses can even grab keys like page up or page down.

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


maillists at nic

Jan 13, 2013, 10:10 AM

Post #4 of 5 (763 views)
Permalink
Re: Keyboard hook in linux [In reply to]

Hi!

Thanks, Michael, for your quick - and heplful - reply.

13.01.2013 18:46, Michael Torrie wrote:
> You're wrong. curses does offer a direct solution to this. Check the
> docs. Also here's a nice intro document for Python 3:
> http://docs.python.org/dev/howto/curses.html

You are right :) The docs tell us (I somehow missed this when reading
the doc last time):

"Itís possible to change this behavior with the method nodelay(). After
nodelay(1), getch() for the window becomes non-blocking and returns
curses.ERR (a value of -1) when no input is ready. Thereís also a
halfdelay() function, which can be used to (in effect) set a timer on
each getch(); if no input becomes available within a specified delay
(measured in tenths of a second), curses raises an exception."

This is actually funny: if you google for e.g. "capture keystrokes
python", you will find masses of suggestions, none of them having this
simple and elegant (i.e. python-like :) ) solution included.

Now it works and my problem is solved. Thank you!

Kind regards,
Kimmo

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


garabik-news-2005-05 at kassiopeia

Jan 13, 2013, 10:11 AM

Post #5 of 5 (778 views)
Permalink
Re: Keyboard hook in linux [In reply to]

K. Elo <maillists [at] nic> wrote:

> Practically I am looking for something similar than Pascal's
> "keypressed" function

As already mentioned, (n)curses is a good solution.
However, if you need/want to go to lower levels, you can read
/dev/input/event* like this (excerpt from one of my programs):

def opendevs():
return [os.open(dev, os.O_RDONLY) for dev in glob.glob("/dev/input/event*")]

def readevent(fds):
try:
# file descriptor has disappeared - we unplugged the keyboard,
# resumed from suspend etc...
ps = [os.read(fd, 16) for fd in fds]
except OSError:
traceback.print_exc()
yield None, None, None
for p in ps:
timeval, suseconds, typ, code, value = struct.unpack( 'llHHI', p[:16])
yield typ, value, code

def run_print(fds):
while 1:
rs, ws, xs = select.select(fds, [], [])
for t, v, e in readevent(rs):
print "Event code:", e, "type:", t, "value:", v

fds = opendevs()
run_print(fds)


This is of course not portable at all (and won't run on ancient
Linuces), but the advantage is that you can hook to the keys or key
combinations curses cannot (e.g. modifiers, Scrolllock etc...) and the
program can react to the key events even in the background.

--
-----------------------------------------------------------
| Radovan Garabík http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk |
-----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.