4

Let's say I have a window-less application which has only an icon on the Taskbar (Windows, Mac OS X & Linux). I want it to capture some key & key combinations, let's say Right Control + Right Shift. Upon keying in correct, combination, it will do something, say take screenshot. I can do window-less app, icon on the Taskbar and screen capture but I don't know how to monitor keyboard globally for key combinations. Please kindly advise. Any help or hint is greatly appreciated! Thanks in advance!

Viet
  • 17,944
  • 33
  • 103
  • 135
  • To my mind, you couldn't grab or capture key, since you don't have the focus onto your window. – dzen Jun 05 '10 at 11:41
  • Just curious. Are you waiting for more thorough answer than mine? ;) – przemoc Jul 05 '10 at 11:11
  • possible duplicate of [Global shortcuts in a cross-platform application](http://stackoverflow.com/questions/11813823/global-shortcuts-in-a-cross-platform-application) – sashoalm Aug 14 '14 at 17:19

1 Answers1

9

System-wide key grabbing is a tricky subject, but system-wide key hooking is even trickier. Every OS/GUI has its own solution, at least for grabbing. Qt4 doesn't expose such feature, but Qt eXTension library solves the problem with its QxtGlobalShortcut. It's a nice wrapper for:

So you can grab explicit key combination, i.e. particular key and modifiers (XGrabKey() allows a bit more), that no other application will get. Key sequences, i.e. consecutive key combinations, are not supported here.


Keyboard hooking is much more powerful, because it allows peeking at the input events (or even filtering them). It's not only used by keyboard loggers, but they are a typical association here.

If you're into Windows, then you can read:

In X11 it's much more complicated. There are at least a two pages you may want to read:

There was a X Event Interception Extension, but it wasn't maintained and eventually has been removed.

Hopefully it can be done without the help of X11 infrastructure. In Linux 2.6 kernel there is "Event interface", known as evdev, that can be exploited here. Details can be found in the source code of the logkeys Linux keylogger. It can also be done with something in effect similar to evdev. See my PoC project: kaos - Key Activity On-Screen display.

And I don't have Mac, so no further references. ;)

phyatt
  • 18,472
  • 5
  • 61
  • 80
przemoc
  • 3,759
  • 1
  • 25
  • 29
  • +1 thanks. I didn't notice your answer. I'm using libqxt with no success in Mac OS X. – Viet Jul 05 '10 at 12:58
  • As I said, I am not a Mac user, so I cannot help you here. Sorry. – przemoc Jul 05 '10 at 13:22
  • Interesting, [Win32 Hooks](http://msdn.microsoft.com/en-us/library/ms997537.aspx) is now an empty page with `Content Removed`. Is it my fault? ;) I'll update above answer. Don't worry, there is a [Wayback Machine](http://www.archive.org/). `http://web.archive.org/web/20080510093117/http://msdn.microsoft.com/en-us/library/ms997537.aspx)` - Win32 Hooks by Kyle Marsh – przemoc Jul 06 '10 at 21:11