Qt 全局热键 QxtGlobalShortcut 分析与使用举例
来源:互联网 发布:mac下载的app在哪里 编辑:程序博客网 时间:2024/04/29 15:58
不少人有给程序添加全局热键的需求。网上随便一搜,就能找到不少介绍windows下如何实现的方式:
首先: 使用 RegisterHotKey和 UnregisterHotKey 注册和反注册热键
- 其次: 在事件全局过滤器 winEventFilter 中相应热键
但是涉及其他平台的,中文资料中提到的似乎不多。
libqxt
其实Qt第三方的库 libqxt 中已经给出了一个比较好的跨平台的解决方案,这就是 QxtGlobalShortcut。
所以,只要你使用这个第三方库就可以了。
可是,你会不会想,我仅仅为了使用这个类,而引入这个 libqxt 第三方库是不是有点大题小做?我能不能将其剥离出来单独使用?
QxtGlobalShortcut
恩,用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
- (即:下面的 key 和 mods)
- 然后,key 和 mods 分别转换成平台相关(native)的码值,并调用平台相关的函数进行注册
- 最后,将key与mods的native码值构成的QPair作为QHash的键,将该shortcut的指针作为值保存起来。
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 安装事件过滤器
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().
- 然后我们在过滤器中分别进行处理,比如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;}
或 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=
参考
http://libqxt.bitbucket.org/doc/tip/qxtglobalshortcut.html
http://software.intel.com/zh-cn/articles/jason_lee20100430/
http://www.cuteqt.com/blog/?p=2088
- Qt 全局热键 QxtGlobalShortcut 分析与使用举例
- Qt 全局热键 QxtGlobalShortcut 分析与使用举例
- Qt5全局热键-QxtGlobalShortcut
- Qt全局热键
- Qt实现全局热键
- QT全局热键
- Qt注册全局热键
- 使用钩子技术实现QT中的全局热键
- Qt全局热键(windows篇)
- Qt全局热键(Windows篇)
- Qt 全局热键的实现
- 第九节 组合热键与注册全局热键
- Qt 全局热键的详细解析
- Qt 5.x 全局热键 for windows
- C#全局热键设置与窗体热键设置实例
- 【转载】C#窗体热键与全局热键设置
- 全局热键
- 全局热键
- c++ 重载构造函数示例
- HTML标签常问题
- 有一种单身,是为了等一个人
- 数组array处理数据
- 猫战
- Qt 全局热键 QxtGlobalShortcut 分析与使用举例
- Alert: Querying v$asm_file Gives ORA-15196 After ASM Was Upgraded From 10gR2 To 11gR2 with an AU size > 1M [ID 1145365.1]
- uva 10392 - Factoring Large Numbers
- B-ISDN业务的特征之业务速率
- js禁用只读文本框获得焦点时的回格键
- B—ISDN保持连接的时间
- hdu1003 最大连续子序和
- C语言高效编程的几招
- 迪杰斯特拉+线段树