Xlib: watching key presses without trapping them

R

Raffaele Ricciardi

Hello there,

I'm writing a X Windows program to perform some actions when modifier
keys have
been released. After looking into the Pykeylogger project
(pykeylogger.sourceforge.net), I've gone as far as detecting all events
related
to modifiers, but then such events get trapped by the program and do not
reach
other applications.

I'm using Python 2.7.3. Thanks for your help.

Here it is the program:

#! /usr/bin/env python

from Xlib.display import Display
from Xlib import X

Control_R = 64 # Keycode for right Control.
Control_L = 108 # Keycode for left Control.
# Keycodes we are listening for. I've left out Right Control
# to be able to stop the program.
keycodes = [Control_L]

# Handle X events.
def handle_event(event):
# Let us know whether this event is about a Key Release of
# one of the keys in which we are interested.
if event.type == X.KeyRelease:
keycode = event.detail
if keycode in keycodes:
print "KeyRelease"

# Objects needed to call Xlib.
display = Display()
root = display.screen().root

# Tell the X server we want to catch KeyRelease events.
root.change_attributes(event_mask = X.KeyReleaseMask)

# Grab those keys.
for keycode in keycodes:
root.grab_key(keycode, X.AnyModifier, 1, X.GrabModeAsync,
X.GrabModeAsync)

# Event loop.
while 1:
event = root.display.next_event()
handle_event(event)

# End of program.
 
S

Steven D'Aprano

Hello there,

I'm writing a X Windows program to perform some actions when modifier
keys have
been released. After looking into the Pykeylogger project
(pykeylogger.sourceforge.net), I've gone as far as detecting all events
related
to modifiers, but then such events get trapped by the program and do not
reach
other applications.


Questions about specialised modules like Pykeylogger will have a much
better chance of being answered if you ask in a dedicated Pykeylogger
forum. If there is none, try emailing the author.

If you do get an answer elsewhere, please consider passing it on here so
others can learn about it too.

Have you looked at how GUIs like wxPython and Tkinter handle key events?
That might help.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top