QT全局热键的实现

QT全局热键的实现

1、加载库

 
 
  1. QT += gui widgets

2、加载头文件

 
 
  1. #include"MyGlobalShortCut.h"
  2. ````
  3. 3、在main函数添加
  4. ```C++
  5. MyGlobalShortCut *shortcut = new MyGlobalShortCut("Ctrl+F9",&m);
  6. QObject::connect(shortcut,SIGNAL(activated()),&m,SLOT(activated()));

4、添加SLOT头文件

 
 
  1. public slots:
  2. void activated();

5、添加SLOT源文件

 
 
  1. void MyDialog::activated()
  2. {
  3. qDebug()<<"2222";
  4. QMessageBox::warning(NULL,"ok","OK...OK");
  5. }

全局热键实现文件

MyGlobalShortCut.h

 
 
  1. // Qt 升级到5.x版本后
  2. // QAbstractEventDispatcher 中函数发生变动
  3. // 导致libqxt库中的qxtGlobalShortcut挂掉
  4. // 参考qxtGlobalShortcut写了一个全局热键类
  5. // 用法与qxtGlobalShortcut一致
  6. // 在 Win8.1 + Qt5.11 下能正常使用
  7. // by 迷路君 2014.03.27
  8. #pragma once
  9. #include <QApplication>
  10. #include <windows.h>
  11. #include <MyWinEventFilter.h>
  12. #include <QKeySequence>
  13. #include <QHash>
  14. class MyGlobalShortCut : public QObject
  15. {
  16. Q_OBJECT
  17. public:
  18. MyGlobalShortCut(QString key,QObject* app);
  19. ~MyGlobalShortCut();
  20. void activateShortcut();
  21. bool registerHotKey();
  22. bool unregisterHotKey();
  23. QHash<QPair<quint32, quint32>, MyGlobalShortCut*> shortcuts;
  24. private:
  25. QApplication *m_app;
  26. MyWinEventFilter *m_filter;
  27. QKeySequence m_key;
  28. Qt::Key key;
  29. Qt::KeyboardModifiers mods;
  30. static quint32 nativeKeycode(Qt::Key keycode);
  31. static quint32 nativeModifiers(Qt::KeyboardModifiers modifiers);
  32. signals:
  33. void activated();
  34. };

MyGlobalShortCut.cpp

 
 
  1. #include "MyGlobalShortCut.h"
  2. MyGlobalShortCut::~MyGlobalShortCut()
  3. {
  4. unregisterHotKey();
  5. }
  6. MyGlobalShortCut::MyGlobalShortCut(QString key, QObject *app)
  7. {
  8. m_key = QKeySequence(key);
  9. m_filter = new MyWinEventFilter(this);
  10. m_app->installNativeEventFilter(m_filter);
  11. registerHotKey();
  12. }
  13. void MyGlobalShortCut::activateShortcut()
  14. {
  15. emit activated();
  16. }
  17. bool MyGlobalShortCut::registerHotKey()
  18. {
  19. Qt::KeyboardModifiers allMods = Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier;
  20. key = (m_key.isEmpty() ? Qt::Key(0) : Qt::Key((m_key[0] ^ allMods) & m_key[0]));
  21. mods = m_key.isEmpty() ? Qt::KeyboardModifiers(0) : Qt::KeyboardModifiers(m_key[0] & allMods);
  22. const quint32 nativeKey = nativeKeycode(key);
  23. const quint32 nativeMods = nativeModifiers(mods);
  24. shortcuts.insert(qMakePair(nativeKey, nativeMods),this);
  25. return RegisterHotKey(0, nativeMods ^ nativeKey, nativeMods, nativeKey);
  26. }
  27. bool MyGlobalShortCut::unregisterHotKey()
  28. {
  29. return UnregisterHotKey(0, (quint32)nativeModifiers(mods) ^ (quint32)nativeKeycode(key));
  30. }
  31. quint32 MyGlobalShortCut::nativeKeycode(Qt::Key key)
  32. {
  33. switch (key)
  34. {
  35. case Qt::Key_Escape:
  36. return VK_ESCAPE;
  37. case Qt::Key_Tab:
  38. case Qt::Key_Backtab:
  39. return VK_TAB;
  40. case Qt::Key_Backspace:
  41. return VK_BACK;
  42. case Qt::Key_Return:
  43. case Qt::Key_Enter:
  44. return VK_RETURN;
  45. case Qt::Key_Insert:
  46. return VK_INSERT;
  47. case Qt::Key_Delete:
  48. return VK_DELETE;
  49. case Qt::Key_Pause:
  50. return VK_PAUSE;
  51. case Qt::Key_Print:
  52. return VK_PRINT;
  53. case Qt::Key_Clear:
  54. return VK_CLEAR;
  55. case Qt::Key_Home:
  56. return VK_HOME;
  57. case Qt::Key_End:
  58. return VK_END;
  59. case Qt::Key_Left:
  60. return VK_LEFT;
  61. case Qt::Key_Up:
  62. return VK_UP;
  63. case Qt::Key_Right:
  64. return VK_RIGHT;
  65. case Qt::Key_Down:
  66. return VK_DOWN;
  67. case Qt::Key_PageUp:
  68. return VK_PRIOR;
  69. case Qt::Key_PageDown:
  70. return VK_NEXT;
  71. case Qt::Key_F1:
  72. return VK_F1;
  73. case Qt::Key_F2:
  74. return VK_F2;
  75. case Qt::Key_F3:
  76. return VK_F3;
  77. case Qt::Key_F4:
  78. return VK_F4;
  79. case Qt::Key_F5:
  80. return VK_F5;
  81. case Qt::Key_F6:
  82. return VK_F6;
  83. case Qt::Key_F7:
  84. return VK_F7;
  85. case Qt::Key_F8:
  86. return VK_F8;
  87. case Qt::Key_F9:
  88. return VK_F9;
  89. case Qt::Key_F10:
  90. return VK_F10;
  91. case Qt::Key_F11:
  92. return VK_F11;
  93. case Qt::Key_F12:
  94. return VK_F12;
  95. case Qt::Key_F13:
  96. return VK_F13;
  97. case Qt::Key_F14:
  98. return VK_F14;
  99. case Qt::Key_F15:
  100. return VK_F15;
  101. case Qt::Key_F16:
  102. return VK_F16;
  103. case Qt::Key_F17:
  104. return VK_F17;
  105. case Qt::Key_F18:
  106. return VK_F18;
  107. case Qt::Key_F19:
  108. return VK_F19;
  109. case Qt::Key_F20:
  110. return VK_F20;
  111. case Qt::Key_F21:
  112. return VK_F21;
  113. case Qt::Key_F22:
  114. return VK_F22;
  115. case Qt::Key_F23:
  116. return VK_F23;
  117. case Qt::Key_F24:
  118. return VK_F24;
  119. case Qt::Key_Space:
  120. return VK_SPACE;
  121. case Qt::Key_Asterisk:
  122. return VK_MULTIPLY;
  123. case Qt::Key_Plus:
  124. return VK_ADD;
  125. case Qt::Key_Comma:
  126. return VK_SEPARATOR;
  127. case Qt::Key_Minus:
  128. return VK_SUBTRACT;
  129. case Qt::Key_Slash:
  130. return VK_DIVIDE;
  131. // numbers
  132. case Qt::Key_0:
  133. case Qt::Key_1:
  134. case Qt::Key_2:
  135. case Qt::Key_3:
  136. case Qt::Key_4:
  137. case Qt::Key_5:
  138. case Qt::Key_6:
  139. case Qt::Key_7:
  140. case Qt::Key_8:
  141. case Qt::Key_9:
  142. return key;
  143. // letters
  144. case Qt::Key_A:
  145. case Qt::Key_B:
  146. case Qt::Key_C:
  147. case Qt::Key_D:
  148. case Qt::Key_E:
  149. case Qt::Key_F:
  150. case Qt::Key_G:
  151. case Qt::Key_H:
  152. case Qt::Key_I:
  153. case Qt::Key_J:
  154. case Qt::Key_K:
  155. case Qt::Key_L:
  156. case Qt::Key_M:
  157. case Qt::Key_N:
  158. case Qt::Key_O:
  159. case Qt::Key_P:
  160. case Qt::Key_Q:
  161. case Qt::Key_R:
  162. case Qt::Key_S:
  163. case Qt::Key_T:
  164. case Qt::Key_U:
  165. case Qt::Key_V:
  166. case Qt::Key_W:
  167. case Qt::Key_X:
  168. case Qt::Key_Y:
  169. case Qt::Key_Z:
  170. return key;
  171. default:
  172. return 0;
  173. }
  174. }
  175. quint32 MyGlobalShortCut::nativeModifiers(Qt::KeyboardModifiers modifiers)
  176. {
  177. // MOD_ALT, MOD_CONTROL, (MOD_KEYUP), MOD_SHIFT, MOD_WIN
  178. quint32 native = 0;
  179. if (modifiers & Qt::ShiftModifier)
  180. native |= MOD_SHIFT;
  181. if (modifiers & Qt::ControlModifier)
  182. native |= MOD_CONTROL;
  183. if (modifiers & Qt::AltModifier)
  184. native |= MOD_ALT;
  185. if (modifiers & Qt::MetaModifier)
  186. native |= MOD_WIN;
  187. // TODO: resolve these?
  188. //if (modifiers & Qt::KeypadModifier)
  189. //if (modifiers & Qt::GroupSwitchModifier)
  190. return native;
  191. }

MyWinEventFilter.h

 
 
  1. #pragma once
  2. #include <QAbstractNativeEventFilter>
  3. class MyGlobalShortCut;
  4. class MyWinEventFilter : public QAbstractNativeEventFilter
  5. {
  6. public:
  7. MyWinEventFilter(MyGlobalShortCut *shortcut);
  8. ~MyWinEventFilter();
  9. virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *);
  10. private:
  11. MyGlobalShortCut *m_shortcut;
  12. };

MyWinEventFilter.cpp

 
 
  1. #include <MyWinEventFilter.h>
  2. #include <MyGlobalShortCut.h>
  3. MyWinEventFilter::~MyWinEventFilter()
  4. {
  5. }
  6. MyWinEventFilter::MyWinEventFilter(MyGlobalShortCut *shortcut)
  7. : m_shortcut(shortcut)
  8. {
  9. }
  10. bool MyWinEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *)
  11. {
  12. if(eventType == "windows_generic_MSG")
  13. {
  14. MSG *msg = static_cast<MSG *>(message);
  15. if(msg->message == WM_HOTKEY)
  16. {
  17. // if(msg->wParam == VK_F10)
  18. // {
  19. // m_shortcut->activateShortcut();
  20. // return true;
  21. // }
  22. const quint32 keycode = HIWORD(msg->lParam);
  23. const quint32 modifiers = LOWORD(msg->lParam);
  24. bool res = m_shortcut->shortcuts.value(qMakePair(keycode, modifiers));
  25. if(res)
  26. {
  27. m_shortcut ->activateShortcut();
  28. return true;
  29. }
  30. }
  31. }
  32. return false;
  33. }




转载于:https://www.cnblogs.com/nfking/p/5713500.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值