Re: Keyboard hook in linux

Discussion in 'Python' started by garabik-news-2005-05@kassiopeia.juls.savba.sk, Jan 13, 2013.

  1. Guest

    K. Elo <> 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!
     
    , Jan 13, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Rapha=EBl?=

    How to scan Keyboard in Perl or else on Linux ?

    =?ISO-8859-1?Q?Rapha=EBl?=, Nov 4, 2004, in forum: Perl
    Replies:
    2
    Views:
    893
    =?ISO-8859-1?Q?Rapha=EBl?=
    Nov 9, 2004
  2. Daniel von Fersen
    Replies:
    1
    Views:
    551
    John Carson
    May 2, 2004
  3. Luc The Perverse

    Windows Keyboard Hook via JNI

    Luc The Perverse, Oct 31, 2006, in forum: Java
    Replies:
    13
    Views:
    1,728
    Luc The Perverse
    Nov 9, 2006
  4. Michael Torrie

    Re: Keyboard hook in linux

    Michael Torrie, Jan 13, 2013, in forum: Python
    Replies:
    0
    Views:
    157
    Michael Torrie
    Jan 13, 2013
  5. K. Elo

    Re: Keyboard hook in linux

    K. Elo, Jan 13, 2013, in forum: Python
    Replies:
    0
    Views:
    355
    K. Elo
    Jan 13, 2013
Loading...

Share This Page