qxtglobalshortcut_win.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "qxtglobalshortcut_p.h"
  2. /****************************************************************************
  3. ** Copyright (c) 2006 - 2011, the LibQxt project.
  4. ** See the Qxt AUTHORS file for a list of authors and copyright holders.
  5. ** All rights reserved.
  6. **
  7. ** Redistribution and use in source and binary forms, with or without
  8. ** modification, are permitted provided that the following conditions are met:
  9. ** * Redistributions of source code must retain the above copyright
  10. ** notice, this list of conditions and the following disclaimer.
  11. ** * Redistributions in binary form must reproduce the above copyright
  12. ** notice, this list of conditions and the following disclaimer in the
  13. ** documentation and/or other materials provided with the distribution.
  14. ** * Neither the name of the LibQxt project nor the
  15. ** names of its contributors may be used to endorse or promote products
  16. ** derived from this software without specific prior written permission.
  17. **
  18. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. ** DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  22. ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. **
  29. ** <http://libqxt.org> <foundation@libqxt.org>
  30. *****************************************************************************/
  31. #include <qt_windows.h>
  32. #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  33. bool QxtGlobalShortcutPrivate::eventFilter(void* message)
  34. {
  35. #else
  36. bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray & eventType,
  37. void * message, long * result)
  38. {
  39. Q_UNUSED(eventType);
  40. Q_UNUSED(result);
  41. #endif
  42. MSG* msg = static_cast<MSG*>(message);
  43. if (msg->message == WM_HOTKEY)
  44. {
  45. const quint32 keycode = HIWORD(msg->lParam);
  46. const quint32 modifiers = LOWORD(msg->lParam);
  47. activateShortcut(keycode, modifiers);
  48. }
  49. #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  50. return prevEventFilter ? prevEventFilter(message) : false;
  51. #else
  52. return false;
  53. #endif
  54. }
  55. quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
  56. {
  57. // MOD_ALT, MOD_CONTROL, (MOD_KEYUP), MOD_SHIFT, MOD_WIN
  58. quint32 native = 0;
  59. if (modifiers & Qt::ShiftModifier)
  60. native |= MOD_SHIFT;
  61. if (modifiers & Qt::ControlModifier)
  62. native |= MOD_CONTROL;
  63. if (modifiers & Qt::AltModifier)
  64. native |= MOD_ALT;
  65. if (modifiers & Qt::MetaModifier)
  66. native |= MOD_WIN;
  67. // TODO: resolve these?
  68. //if (modifiers & Qt::KeypadModifier)
  69. //if (modifiers & Qt::GroupSwitchModifier)
  70. return native;
  71. }
  72. quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key)
  73. {
  74. switch (key)
  75. {
  76. case Qt::Key_Escape:
  77. return VK_ESCAPE;
  78. case Qt::Key_Tab:
  79. case Qt::Key_Backtab:
  80. return VK_TAB;
  81. case Qt::Key_Backspace:
  82. return VK_BACK;
  83. case Qt::Key_Return:
  84. case Qt::Key_Enter:
  85. return VK_RETURN;
  86. case Qt::Key_Insert:
  87. return VK_INSERT;
  88. case Qt::Key_Delete:
  89. return VK_DELETE;
  90. case Qt::Key_Pause:
  91. return VK_PAUSE;
  92. case Qt::Key_Print:
  93. return VK_PRINT;
  94. case Qt::Key_Clear:
  95. return VK_CLEAR;
  96. case Qt::Key_Home:
  97. return VK_HOME;
  98. case Qt::Key_End:
  99. return VK_END;
  100. case Qt::Key_Left:
  101. return VK_LEFT;
  102. case Qt::Key_Up:
  103. return VK_UP;
  104. case Qt::Key_Right:
  105. return VK_RIGHT;
  106. case Qt::Key_Down:
  107. return VK_DOWN;
  108. case Qt::Key_PageUp:
  109. return VK_PRIOR;
  110. case Qt::Key_PageDown:
  111. return VK_NEXT;
  112. case Qt::Key_F1:
  113. return VK_F1;
  114. case Qt::Key_F2:
  115. return VK_F2;
  116. case Qt::Key_F3:
  117. return VK_F3;
  118. case Qt::Key_F4:
  119. return VK_F4;
  120. case Qt::Key_F5:
  121. return VK_F5;
  122. case Qt::Key_F6:
  123. return VK_F6;
  124. case Qt::Key_F7:
  125. return VK_F7;
  126. case Qt::Key_F8:
  127. return VK_F8;
  128. case Qt::Key_F9:
  129. return VK_F9;
  130. case Qt::Key_F10:
  131. return VK_F10;
  132. case Qt::Key_F11:
  133. return VK_F11;
  134. case Qt::Key_F12:
  135. return VK_F12;
  136. case Qt::Key_F13:
  137. return VK_F13;
  138. case Qt::Key_F14:
  139. return VK_F14;
  140. case Qt::Key_F15:
  141. return VK_F15;
  142. case Qt::Key_F16:
  143. return VK_F16;
  144. case Qt::Key_F17:
  145. return VK_F17;
  146. case Qt::Key_F18:
  147. return VK_F18;
  148. case Qt::Key_F19:
  149. return VK_F19;
  150. case Qt::Key_F20:
  151. return VK_F20;
  152. case Qt::Key_F21:
  153. return VK_F21;
  154. case Qt::Key_F22:
  155. return VK_F22;
  156. case Qt::Key_F23:
  157. return VK_F23;
  158. case Qt::Key_F24:
  159. return VK_F24;
  160. case Qt::Key_Space:
  161. return VK_SPACE;
  162. case Qt::Key_Asterisk:
  163. return VK_MULTIPLY;
  164. case Qt::Key_Plus:
  165. return VK_ADD;
  166. case Qt::Key_Comma:
  167. return VK_SEPARATOR;
  168. case Qt::Key_Minus:
  169. return VK_SUBTRACT;
  170. case Qt::Key_Slash:
  171. return VK_DIVIDE;
  172. case Qt::Key_MediaNext:
  173. return VK_MEDIA_NEXT_TRACK;
  174. case Qt::Key_MediaPrevious:
  175. return VK_MEDIA_PREV_TRACK;
  176. case Qt::Key_MediaPlay:
  177. return VK_MEDIA_PLAY_PAUSE;
  178. case Qt::Key_MediaStop:
  179. return VK_MEDIA_STOP;
  180. // couldn't find those in VK_*
  181. //case Qt::Key_MediaLast:
  182. //case Qt::Key_MediaRecord:
  183. case Qt::Key_VolumeDown:
  184. return VK_VOLUME_DOWN;
  185. case Qt::Key_VolumeUp:
  186. return VK_VOLUME_UP;
  187. case Qt::Key_VolumeMute:
  188. return VK_VOLUME_MUTE;
  189. // numbers
  190. case Qt::Key_0:
  191. case Qt::Key_1:
  192. case Qt::Key_2:
  193. case Qt::Key_3:
  194. case Qt::Key_4:
  195. case Qt::Key_5:
  196. case Qt::Key_6:
  197. case Qt::Key_7:
  198. case Qt::Key_8:
  199. case Qt::Key_9:
  200. return key;
  201. // letters
  202. case Qt::Key_A:
  203. case Qt::Key_B:
  204. case Qt::Key_C:
  205. case Qt::Key_D:
  206. case Qt::Key_E:
  207. case Qt::Key_F:
  208. case Qt::Key_G:
  209. case Qt::Key_H:
  210. case Qt::Key_I:
  211. case Qt::Key_J:
  212. case Qt::Key_K:
  213. case Qt::Key_L:
  214. case Qt::Key_M:
  215. case Qt::Key_N:
  216. case Qt::Key_O:
  217. case Qt::Key_P:
  218. case Qt::Key_Q:
  219. case Qt::Key_R:
  220. case Qt::Key_S:
  221. case Qt::Key_T:
  222. case Qt::Key_U:
  223. case Qt::Key_V:
  224. case Qt::Key_W:
  225. case Qt::Key_X:
  226. case Qt::Key_Y:
  227. case Qt::Key_Z:
  228. return key;
  229. default:
  230. return 0;
  231. }
  232. }
  233. bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods)
  234. {
  235. return RegisterHotKey(0, nativeMods ^ nativeKey, nativeMods, nativeKey);
  236. }
  237. bool QxtGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods)
  238. {
  239. return UnregisterHotKey(0, nativeMods ^ nativeKey);
  240. }