Reputation: 1195
As the title states, I'm trying to capture Mouse and Keyboard events with Python-gtk.
I can do this easily with python-xlib with:
self.display = display.Display()
self.screen = self.display.screen()
self.root_window = self.screen.root
self.root_window.grab_pointer(1,
X.ButtonPressMask | X.ButtonReleaseMask | X.Button1MotionMask,
X.GrabModeAsync,
X.GrabModeAsync,
X.NONE, X.NONE,
X.CurrentTime)
self.root_window.grab_keyboard(1,
X.GrabModeAsync,
X.GrabModeAsync,
X.CurrentTime)
I see the analog using gtk.gdk.* functions, but I just can't seem to capture events on the main desktop window. Can this be done?
This is how I was trying to accomplish the task... (ALL_EVENTS_MASK was an act of desperation ;] )
self.root_window = gtk.gdk.get_default_root_window()
self.root_window.set_events(gtk.gdk.ALL_EVENTS_MASK)
gtk.gdk.event_handler_set(self.filter_callback)
gtk.main()
def filter_callback (self, *args):
print args
Upvotes: 3
Views: 5022
Reputation: 851
I'm guessing this can't be done using plain Gtk and you'll have to involve Xlib or other form of communication with the server itself. Unless maybe your app is running in the root window itself.
I can be wrong, of course.
Upvotes: 0
Reputation: 70021
Here is an example that i just did that you can base on it:
import gtk
def on_key_press(widget, data=None):
print "click"
if __name__ == '__main__':
w = gtk.Window()
# Connect the callback on_key_press to the signal key_press.
w.connect("key_press_event", on_key_press)
# Make the widget aware of the signal to catch.
w.set_events(gtk.gdk.KEY_PRESS_MASK)
w.show_all()
gtk.main()
Launch now the script and clicking on any keyword key et voilà (Output):
$ python gtk_script.py
click
click
click
click
Hope this can help
Upvotes: 3