Many people need to add global hotkeys to the program. You can find a lot of methods to introduce how to implement windows by searching on the Internet:
However, it does not seem to mention much in Chinese documents that involve other platforms.
Libqxt
In fact, the third-party library libqxt of QT has provided a better cross-platform solution, which is qxtglobalshortcut.
Therefore, you can use this third-party library.
However, do you think it is a little difficult to introduce this libqxt third-party library just to use this class? Can I strip it out for separate use?
Qxtglobalshortcut
Well, you can find out by Google that many people have done this job on the Internet. You will find that many people use the separated qxtglobalshortcut as the 3 rdparty part of the project.
Then let's take a look at the qxtglobalshortcut:
Usage
It's so simple that it's no longer easy, right?
QxtGlobalShortcut* shortcut = new QxtGlobalShortcut(QKeySequence("Ctrl+Shift+F12"), w);connect(shortcut, SIGNAL(activated()), w, SLOT(myslot()));
Note: If you are not interested in the internal implementation and only care about how to use it, this small example may be all you need.
: Http://code.google.com/p/h-qt-exercise/downloads/detail? Name‑shortcut.zip & can = 2 & Q =
Source code file
|
Gxtglobal. h |
Libqxt a Global File |
* |
Qxtglobalshortcut. h |
Interface files that we are most concerned about |
|
Qxtglobalshortcut _. h |
Private header file |
|
Gxtglobalshortcut. cpp |
Class implementation (platform-independent) |
|
Gxtglobalshortcut_win.cpp |
Class implementation (WIN) |
|
Gxtglobalshortcut_x11.cpp |
Class implementation (X11) |
|
Gxtglobalshortcut_mac.cpp |
Class implementation (MAC) |
Registration Process of hotkeys
When we set a hot key through the constructor or the setshortcut member:
bool QxtGlobalShortcutPrivate::setShortcut(const QKeySequence& shortcut){... key = ... mods = ... const quint32 nativeKey = nativeKeycode(key); const quint32 nativeMods = nativeModifiers(mods); const bool res = registerShortcut(nativeKey, nativeMods); shortcuts.insert(qMakePair(nativeKey, nativeMods), &qxt_p());...
- The process of anti-registration is similar.
Hotkey Activation
- First install the Event Filter for qiniacteventdispatcher.
QAbstractEventDispatcher::instance()->setEventFilter(eventFilter);
Note that qapplication is not installed here. This is what manual tells us.
bool QCoreApplication::winEventFilter ( MSG * msg, long * result ) [virtual]To handle system wide messages, such as messages from a registered hot key, you need to install an event filter on the event dispatcher, which is returned from QAbstractEventDispatcher::instance().
- Then we process the filters separately, for example, in Windows
bool QxtGlobalShortcutPrivate::eventFilter(void* message){ MSG* msg = static_cast<MSG*>(message); if (msg->message == WM_HOTKEY){ const quint32 keycode = HIWORD(msg->lParam); const quint32 modifiers = LOWORD(msg->lParam); activateShortcut(keycode, modifiers); } return false;}
Or under X11:
bool QxtGlobalShortcutPrivate::eventFilter(void* message){ XEvent* event = static_cast<XEvent*>(message); if (event->type == KeyPress){ XKeyEvent* key = (XKeyEvent*) event; activateShortcut(key->keycode, key->state & (ShiftMask | ControlMask | Mod1Mask | Mod4Mask)); } return false;}
Activateshortcut is platform independent:
void QxtGlobalShortcutPrivate::activateShortcut(quint32 nativeKey, quint32 nativeMods){ QxtGlobalShortcut* shortcut = shortcuts.value(qMakePair(nativeKey, nativeMods)); if (shortcut && shortcut->isEnabled()) emit shortcut->activated();}
Do you remember that the previous registration hotkey saved something to a qhash? It works here. When a hot key arrives, we can find from this qhash whether there is a corresponding qxtglobalshortcut. If it exists and is enabled, it will send a signal.
How to Use
For ease of use, we can add a qxtglobalshortcut. PRI file in the directory at the same level of this file:
INCLUDEPATH += $$PWDDEPENDPATH += $$PWDHEADERS += $$PWD/qxtglobal.h / $$PWD/qxtglobalshortcut.h / $$PWD/qxtglobalshortcut_p.hSOURCES += $$PWD/qxtglobalshortcut.cppwin32{ SOURCES += $$PWD/qxtglobalshortcut_win.cpp LIBS += -luser32}unix:SOURCES += $$PWD/qxtglobalshortcut_x11.cppmac:SOURCES += $$PWD/qxtglobalshortcut_mac.cpp
In this way, when we use it, we only need to add one more line in our pro file:
include(yourpath/qxtglobalshortcut.pri)
You can.
A small example: http://code.google.com/p/h-qt-exercise/downloads/detail? Name‑shortcut.zip & can = 2 & Q =
Reference
Http://libqxt.bitbucket.org/doc/tip/qxtglobalshortcut.html
Http://software.intel.com/zh-cn/articles/jason_lee20100430/
Http://www.cuteqt.com/blog? P = 2088