Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Keyboard hook in linux

Thread Tools

Re: Keyboard hook in linux
Posts: n/a
K. Elo <(E-Mail Removed)> 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.O_RDONLY) for dev in glob.glob("/dev/input/event*")]

def readevent(fds):
# file descriptor has disappeared - we unplugged the keyboard,
# resumed from suspend etc...
ps = [, 16) for fd in fds]
except OSError:
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 =, [], [])
for t, v, e in readevent(rs):
print "Event code:", e, "type:", t, "value:", v

fds = opendevs()

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 |
| __..--^^^--..__ garabik @ |
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Keyboard hook in linux K. Elo Python 0 01-13-2013 06:10 PM
Re: Keyboard hook in linux Michael Torrie Python 0 01-13-2013 04:46 PM
Windows Keyboard Hook via JNI Luc The Perverse Java 13 11-09-2006 07:39 AM
Keyboard error or no keyboard present??? Bud Light Computer Support 2 01-22-2005 04:00 AM
Keyboard Hook Problem (seems verbally unsolvable) Daniel von Fersen C++ 1 05-02-2004 01:53 AM