How to capture "key-press-event" when the window is not focused?

I am writing a program using gtk. What the program does is to monitor the keystroke entered by the user and play a sound. My question is, how can I catch the key-press-event event when the window is out of focus? I plan to keep my program in the tray icon, so I wonder how I can capture any press event from there. Thanks

Edit: I finally found a way to do this - the XTest extension, I found a piece of code from the "xmacro" program. You can see my implementation here: http://github.com/Aitjcize/Qwertickle/blob/master/src/qwertickle.c

btw, it’s still pretty buggy, maybe someone can help me? :)

+4
source share
2 answers

As Matt Joiner said,

On Linux, this is not so simple.

and unfortunately GTK + cannot do this kind of magic.

You should take a look at XEvIE - Extending X event capture - this will make your job easier.

XEvIE is an X extension that provides features that allow users to capture keyboard / mouse events.

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

+3
source

There is a program called xbindkeys that can bind the mouse and keyboard keys in X to run shell commands. You can either use this to send commands to your program, or look at the source code to see how it is done: xbindkeys

You can also directly open / dev / input / eventX and read () from it to the input_event structure, but this is a bit unpleasant because you need to have the correct permissions (usually root or change them with chmod)

0
source

All Articles