Hook key & key combinations from keyboard with Qt 4.6

后端 未结 1 674
逝去的感伤
逝去的感伤 2020-12-05 21:55

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\

相关标签:
1条回答
  • 2020-12-05 22:55

    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:

    • XGrabKey()/XUngrabKey() in X11,
    • RegisterHotKey()/UnregisterHotKey() in Windows,
    • RegisterEventHotKey()/UnregisterEventHotKey() in Mac OS X.

    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:

    • Hooks and DLLs by Joseph M. Newcomer,
    • Hooks.

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

    • X.Org Wiki - Development/Documentation/InputEventProcessing - to have some background,
    • Exploiting X11 to monitor keystrokes - to understand difficulties.

    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. ;)

    0 讨论(0)
提交回复
热议问题