How do I enter "key-press-event" when the window is not concentrated?


I am writing a program using gtk. What the program does is monitor the keystroke the user entered and play a sound. My question is that how do I catch the key-press-event when the window is not focused? I'm planning to let my program stay in tray icon, so I wonder how I can grab any key-press-event from there. Thanks

Edit: I finally find a way to do it - The XTest extension, I found the a piece of code snippet from the program 'xmacro'. You can see my implementation here:

btw, it's still quite buggy, maybe someone can help me out? :)

As Matt Joiner said,

This kind of thing isn't as easy in Linux.

and unfortunately GTK+ can't do this kind of magic.

You should take a look at XEvIE - X Event Interception Extension - it will make your job easier.

XEvIE is a X extension providing functionalities to allow users intercept keyboard/mouse events.

And as suggested by this guy, another way to go would be to use XGrabKey()/XUngrabKey() from X11. I believe that tinywm shows how to use it correctly.