QtGlobal hotkeyQxtGlobalShortcutAnalyze and use examples to share with you. If you need a global hotkey, this article will solve it for you.
First, use RegisterHotKey and UnregisterHotKey for registration and anti-registration.Hotkey
Second, in the event global filter winEventFilter, the correspondingHotkeyBut it does not seem to mention much in Chinese documents that involve other platforms.
Libqxt
ActuallyQtThe third-party library libqxt has provided a better cross-platform solution, which isQxtGlobalShortcut.
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 will strip outQxtGlobalShortcutAs the 3 rdparty part of the project.
Let's take a look at this.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 its internal implementation, you 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:
First, the QKeySequence is divided into two parts: Qt: Key and Qt: KeyboardModifiers.
That is, the following key and mod)
Then, the key and MoD are converted into the platform-related (native) code values, and the platform-related functions are called for registration.
Finally, the QPair consisting of the key and the native code value of the MOD is used as the QHash key, and the pointer of the shortcut is saved as the value.
- 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.
- Qiniacteventdispatcher: 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 qiniacteventdispatcher: instance (). Then we process them separately in the filter, 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();
- }
Remember to registerHotkeyIs it to save things to a QHash? It works here, whenHotkeyAt the time of arrival, we can find the corresponding QHash.QxtGlobalShortcutYes. If yes and enabled, the System emits a signal.
How to Use
For ease of use, we can addQxtglobalshortcut. Pri file:
- INCLUDEPATH += $$PWD
- DEPENDPATH += $$PWD
-
- HEADERS += $$PWD/qxtglobal.h \
- $$PWD/qxtglobalshortcut.h \
- $$PWD/qxtglobalshortcut_p.h
- SOURCES += $$PWD/qxtglobalshortcut.cpp
- win32{
- SOURCES += $$PWD/qxtglobalshortcut_win.cpp
- LIBS += -luser32
- }
- unix:SOURCES += $$PWD/qxtglobalshortcut_x11.cpp
- mac: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 ).
A small example: http://code.google.com/p/h-qt-exercise/downloads/detail? Name‑shortcut.zip & can = 2 & q =
Summary:QtGlobalHotkey QxtGlobalShortcutThe Analysis and examples are provided to help you solve the problem.