4

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: http://github.com/Aitjcize/Qwertickle/blob/master/src/qwertickle.c

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

Aitjcize
  • 187
  • 1
  • 8
  • 2
    @pmg: Why would a keylogger play a sound...? Furthermore why would you keylog <1% of desktop users when you can get 90% by writing some Win32? – Matt Joiner Sep 14 '10 at 23:00
  • I'm all FOR people writing keyloggers, viruses, trojans, worms and whatnot. I've never felt the interest in writing one myself, but if I knew how to, I'd, at least, point Aitjcize in the right direction (and "keylogger" might just be what he/she needs to get results from his/her preferred search engine) – pmg Sep 14 '10 at 23:25
  • @pmg: I was going to write something like this http://www.nattyware.com/qwertick.php in Linux, = =. – Aitjcize Sep 15 '10 at 05:37
  • I think the question is reasonable, just think of desktop wide hotkeys for application launchers and stuff – drahnr Oct 02 '10 at 05:43
  • Okay now I'm interested. This kind of thing isn't as easy in Linux. – Matt Joiner Oct 02 '10 at 12:55

2 Answers2

3

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.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Thanks a lot dude! At last I used the XTst extension, I found it in xmacro's source code. You can find my implementation here: http://github.com/Aitjcize/Qwertickle/blob/master/src/qwertickle.c – Aitjcize Oct 04 '10 at 13:06
  • @Aitjcize: That's very nice code you've written. Your switch on the key pressed would be more readable if you used character literals: `case 65` can be `case 'A'` for example. – Matt Joiner Oct 09 '10 at 00:29
  • 1
    A great answer, lots of links for further reading, well done. – Matt Joiner Oct 09 '10 at 00:31
0

There is a program called xbindkeys that can bind mouse and keyboard keys in X to launch shell commands. You can either utilize this to send commands to your program, or look at the sourcecode to see how its done: xbindkeys

You can also directly open /dev/input/eventX and read() from it to an input_event struct, but thats a little nasty, because you need to have the proper rights (normally root, or change it with chmod)

derhoch
  • 458
  • 6
  • 9