不少人有给程序添加全局热键的需求。网上随便一搜,就能找到不少介绍windows下如何实现的方式:
首先: 使用 RegisterHotKey和 UnregisterHotKey 注册和反注册热键
但是涉及其他平台的,中文资料中提到的似乎不多。
其实Qt第三方的库 libqxt 中已经给出了一个比较好的跨平台的解决方案,这就是 QxtGlobalShortcut。
所以,只要你使用这个第三方库就可以了。
可是,你会不会想,我仅仅为了使用这个类,而引入这个 libqxt 第三方库是不是有点大题小做?我能不能将其剥离出来单独使用?
恩,用google搜索一下,就可以知道,网上很多人都做过这个工作了。你会发现很多人将剥离出的 QxtGlobalShortcut 作为项目的 3rdparty 部分。
那么我们接下来就看看这个 QxtGlobalShortcut :
已经简单到不能再简单了,是不?
QxtGlobalShortcut* shortcut = new QxtGlobalShortcut(QKeySequence("Ctrl+Shift+F12"), w);connect(shortcut, SIGNAL(activated()), w, SLOT(myslot()));
注意:如果你对其内部实现不感兴趣,只关心如何使用,那么这个小例子可能就是你需要的全部了:http://code.google.com/p/h-qt-exercise/downloads/detail?name=shortcut.zip&can=2&q=
gxtglobal.h | libqxt 一个全局文件 | |
* | qxtglobalshortcut.h | 我们最关心的接口文件 |
| qxtglobalshortcut_.h | 私有头文件 |
gxtglobalshortcut.cpp | 类的实现(平台无关部分) | |
gxtglobalshortcut_win.cpp | 类的实现(win) | |
gxtglobalshortcut_x11.cpp | 类的实现(x11) | |
gxtglobalshortcut_mac.cpp | 类的实现(mac) |
我们通过构造函数,或者setShortcut 成员设置一个热键时:
首先将这个 QKeySequence 分解成两部分 Qt::Key 和 Qt::KeyboardModifiers
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());...
QAbstractEventDispatcher::instance()->setEventFilter(eventFilter);
注意,这儿不是给 QApplication 安装的。Manual中是这样告诉我们的
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().
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;}
或 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是平台无关的:
void QxtGlobalShortcutPrivate::activateShortcut(quint32 nativeKey, quint32 nativeMods){ QxtGlobalShortcut* shortcut = shortcuts.value(qMakePair(nativeKey, nativeMods)); if (shortcut && shortcut->isEnabled()) emit shortcut->activated();}
记得前面注册热键是保存东西到一个QHash么?它是在这儿起作用的,当一个热键到来时,我们从该QHash查找有没有相应的QxtGlobalShortcut存在,存在且启用的话,则发射信号。
为了便于使用,我们可以在这个文件的同级目录中添加一个 qxtglobalshortcut.pri 的文件:
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
这样一来,当我们使用时,只需要在我们的pro文件内多加一行:
include(yourpath/qxtglobalshortcut.pri)
即可
一个具体的小例子:http://code.google.com/p/h-qt-exercise/downloads/detail?name=shortcut.zip&can=2&q=
联系客服