[pygtk] catching all keyboard/mouse events

Jan Martinek honza at dp.fce.vutbr.cz
Sat Aug 19 22:05:40 WST 2006


Hello,

please, does anyone know how to catch all keyboard and mouse events 
regardless of main window state? If I try this

#!/usr/bin/env python
import gtk

def wakeup(widget, event):
   print "Event number %d woke me up" % event.type

w = gtk.Window()

w.add_events(gtk.gdk.KEY_PRESS_MASK |
             gtk.gdk.POINTER_MOTION_MASK |
             gtk.gdk.BUTTON_PRESS_MASK |
             gtk.gdk.SCROLL_MASK)

w.connect("motion-notify-event", wakeup)
w.connect("key-press-event", wakeup)
w.connect("button-press-event", wakeup)
w.connect("scroll-event", wakeup)

w.show()

gtk.main()


then it responds to keypresses only if the window is active. Mouse events
are catched only when the cursor is placed within the window.

How can I catch all the events even when the window is
inactive/minimalized/hidden? Do you know any application that must be able
to do this? For example

- applets measuring distance travelled by mouse cursor
- typing-break applications that notify the user that he typed for a long
  period of time a should have a rest.
- something that monitors user's activity/inactivity (various IM clients?)
- "xeyes" - eyes that always look at the mouse cursor
- keyloggers - malicious programs (if there are any) sniffing keystrokes
  (mainly passwords) and logging them into a file.
- ???

I am looking for something written in pure GTK (no direct xlib calls) so
that I could learn from the source code.

Thank you
Jan Martinek


More information about the pygtk mailing list