3

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

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

This is a light version of the sample code I already included. Also, this is a gtk window. I need a gtk.gdk window... in this case the ENTIRE desktop, not a simple application window. My problem would be trivial, otherwise.
@lsthree: where do you call, set_events() ??? it should be called just after creating the widget in your case gtk.gdk.Window !!!
ah! oops, when I have previously glanced I thought our event method names were the same. I made the addition, but the problem still remains. For example, you cannot connect key_press, button_press, etc to the Desktop's root window. 'self.root_window.set_events(gtk.gdk.ALL_EVENTS_MASK)' gives me events such as when I focus a different window or when I minimize/maximize a window. I need to get keyboard and mouse events.
@lsthree: i don't know if this can help but have you set the event_mask attribute when creating your gtk.gdk.Window ?? , see the doc pygtk.org/docs/pygtk/class-gdkwindow.html something like: self.window = gtk.gdk.Window(... , event_mask=gtk.gdk.KEY_PRESS_MASK | gtk.gdk.MOTION_NOTIFY)
I never create a window. I simply use the root/toplevel window containing the desktop etc. At no point do I create a window and pass args to it. PITA
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.