Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36739 > unrolled thread

Keyboard hook in linux

Started by"K. Elo" <maillists@nic.fi>
First post2013-01-13 18:13 +0200
Last post2013-01-13 18:11 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Keyboard hook in linux "K. Elo" <maillists@nic.fi> - 2013-01-13 18:13 +0200
    Re: Keyboard hook in linux garabik-news-2005-05@kassiopeia.juls.savba.sk - 2013-01-13 18:11 +0000

#36739 — Keyboard hook in linux

From"K. Elo" <maillists@nic.fi>
Date2013-01-13 18:13 +0200
SubjectKeyboard hook in linux
Message-ID<mailman.470.1358093620.2939.python-list@python.org>
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

[toc] | [next] | [standalone]


#36746

Fromgarabik-news-2005-05@kassiopeia.juls.savba.sk
Date2013-01-13 18:11 +0000
Message-ID<kcutch$85r$1@speranza.aioe.org>
In reply to#36739
K. Elo <maillists@nic.fi> 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!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web