1 /****************************************************************************
2  **
3  ** Copyright (C) Qxt Foundation. Some rights reserved.
4  **
5  ** This file is part of the QxtGui module of the Qxt library.
6  **
7  ** This library is free software; you can redistribute it and/or modify it
8  ** under the terms of the Common Public License, version 1.0, as published
9  ** by IBM, and/or under the terms of the GNU Lesser General Public License,
10  ** version 2.1, as published by the Free Software Foundation.
11  **
12  ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
13  ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
14  ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
15  ** FITNESS FOR A PARTICULAR PURPOSE.
16  **
17  ** You should have received a copy of the CPL and the LGPL along with this
18  ** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
19  ** included with the source distribution for more information.
20  ** If you did not receive a copy of the licenses, contact the Qxt Foundation.
21  **
22  ** <http://libqxt.org>  <foundation@libqxt.org>
23  **
24  ****************************************************************************/
25 #include "qxtglobalshortcut_p.h"
26 #include <qt_windows.h>
27 
28 #if QT_VERSION<0x050000
eventFilter(void * message)29 bool QxtGlobalShortcutPrivate::eventFilter(void* message)
30 #else
31 bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray &, void *message, long *result)
32 #endif
33 {
34     MSG* msg = static_cast<MSG*>(message);
35     if (msg->message == WM_HOTKEY)
36     {
37         const quint32 keycode = HIWORD(msg->lParam);
38         const quint32 modifiers = LOWORD(msg->lParam);
39         activateShortcut(keycode, modifiers);
40     }
41     return false;
42 }
43 
nativeModifiers(Qt::KeyboardModifiers modifiers)44 quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
45 {
46     // MOD_ALT, MOD_CONTROL, (MOD_KEYUP), MOD_SHIFT, MOD_WIN
47     quint32 native = 0;
48     if (modifiers & Qt::ShiftModifier)
49         native |= MOD_SHIFT;
50     if (modifiers & Qt::ControlModifier)
51         native |= MOD_CONTROL;
52     if (modifiers & Qt::AltModifier)
53         native |= MOD_ALT;
54     if (modifiers & Qt::MetaModifier)
55         native |= MOD_WIN;
56     // TODO: resolve these?
57     //if (modifiers & Qt::KeypadModifier)
58     //if (modifiers & Qt::GroupSwitchModifier)
59     return native;
60 }
61 
nativeKeycode(Qt::Key key)62 quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key)
63 {
64     switch (key)
65     {
66     case Qt::Key_Escape:
67         return VK_ESCAPE;
68     case Qt::Key_Tab:
69     case Qt::Key_Backtab:
70         return VK_TAB;
71     case Qt::Key_Backspace:
72         return VK_BACK;
73     case Qt::Key_Return:
74     case Qt::Key_Enter:
75         return VK_RETURN;
76     case Qt::Key_Insert:
77         return VK_INSERT;
78     case Qt::Key_Delete:
79         return VK_DELETE;
80     case Qt::Key_Pause:
81         return VK_PAUSE;
82     case Qt::Key_Print:
83         return VK_PRINT;
84     case Qt::Key_Clear:
85         return VK_CLEAR;
86     case Qt::Key_Home:
87         return VK_HOME;
88     case Qt::Key_End:
89         return VK_END;
90     case Qt::Key_Left:
91         return VK_LEFT;
92     case Qt::Key_Up:
93         return VK_UP;
94     case Qt::Key_Right:
95         return VK_RIGHT;
96     case Qt::Key_Down:
97         return VK_DOWN;
98     case Qt::Key_PageUp:
99         return VK_PRIOR;
100     case Qt::Key_PageDown:
101         return VK_NEXT;
102     case Qt::Key_F1:
103         return VK_F1;
104     case Qt::Key_F2:
105         return VK_F2;
106     case Qt::Key_F3:
107         return VK_F3;
108     case Qt::Key_F4:
109         return VK_F4;
110     case Qt::Key_F5:
111         return VK_F5;
112     case Qt::Key_F6:
113         return VK_F6;
114     case Qt::Key_F7:
115         return VK_F7;
116     case Qt::Key_F8:
117         return VK_F8;
118     case Qt::Key_F9:
119         return VK_F9;
120     case Qt::Key_F10:
121         return VK_F10;
122     case Qt::Key_F11:
123         return VK_F11;
124     case Qt::Key_F12:
125         return VK_F12;
126     case Qt::Key_F13:
127         return VK_F13;
128     case Qt::Key_F14:
129         return VK_F14;
130     case Qt::Key_F15:
131         return VK_F15;
132     case Qt::Key_F16:
133         return VK_F16;
134     case Qt::Key_F17:
135         return VK_F17;
136     case Qt::Key_F18:
137         return VK_F18;
138     case Qt::Key_F19:
139         return VK_F19;
140     case Qt::Key_F20:
141         return VK_F20;
142     case Qt::Key_F21:
143         return VK_F21;
144     case Qt::Key_F22:
145         return VK_F22;
146     case Qt::Key_F23:
147         return VK_F23;
148     case Qt::Key_F24:
149         return VK_F24;
150     case Qt::Key_Space:
151         return VK_SPACE;
152     case Qt::Key_Asterisk:
153         return VK_MULTIPLY;
154     case Qt::Key_Plus:
155         return VK_ADD;
156     case Qt::Key_Comma:
157         return VK_SEPARATOR;
158     case Qt::Key_Minus:
159         return VK_SUBTRACT;
160     case Qt::Key_Slash:
161         return VK_DIVIDE;
162 
163     case Qt::Key_MediaNext:
164         return VK_MEDIA_NEXT_TRACK;
165     case Qt::Key_MediaPrevious:
166         return VK_MEDIA_PREV_TRACK;
167     case Qt::Key_MediaStop:
168         return VK_MEDIA_STOP;
169     case Qt::Key_MediaPlay:
170         return VK_MEDIA_PLAY_PAUSE;
171     case Qt::Key_VolumeDown:
172         return VK_VOLUME_DOWN;
173     case Qt::Key_VolumeUp:
174         return VK_VOLUME_UP;
175     case Qt::Key_VolumeMute:
176         return VK_VOLUME_MUTE;
177 
178         // numbers
179     case Qt::Key_0:
180     case Qt::Key_1:
181     case Qt::Key_2:
182     case Qt::Key_3:
183     case Qt::Key_4:
184     case Qt::Key_5:
185     case Qt::Key_6:
186     case Qt::Key_7:
187     case Qt::Key_8:
188     case Qt::Key_9:
189         return key;
190 
191         // letters
192     case Qt::Key_A:
193     case Qt::Key_B:
194     case Qt::Key_C:
195     case Qt::Key_D:
196     case Qt::Key_E:
197     case Qt::Key_F:
198     case Qt::Key_G:
199     case Qt::Key_H:
200     case Qt::Key_I:
201     case Qt::Key_J:
202     case Qt::Key_K:
203     case Qt::Key_L:
204     case Qt::Key_M:
205     case Qt::Key_N:
206     case Qt::Key_O:
207     case Qt::Key_P:
208     case Qt::Key_Q:
209     case Qt::Key_R:
210     case Qt::Key_S:
211     case Qt::Key_T:
212     case Qt::Key_U:
213     case Qt::Key_V:
214     case Qt::Key_W:
215     case Qt::Key_X:
216     case Qt::Key_Y:
217     case Qt::Key_Z:
218         return key;
219 
220     default:
221         return 0;
222     }
223 }
224 
registerShortcut(quint32 nativeKey,quint32 nativeMods)225 bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods)
226 {
227     return RegisterHotKey(0, nativeMods ^ nativeKey, nativeMods, nativeKey);
228 }
229 
unregisterShortcut(quint32 nativeKey,quint32 nativeMods)230 bool QxtGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods)
231 {
232     return UnregisterHotKey(0, nativeMods ^ nativeKey);
233 }
234