1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * VirtualBox Qt extensions: QIHotKeyEdit class implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "QIHotKeyEdit.h"
|
---|
24 | #include "VBoxDefs.h"
|
---|
25 |
|
---|
26 | /* Qt includes */
|
---|
27 | #include <QApplication>
|
---|
28 | #include <QStyleOption>
|
---|
29 | #include <QStylePainter>
|
---|
30 |
|
---|
31 | #ifdef Q_WS_WIN
|
---|
32 | /* VBox/cdefs.h defines these: */
|
---|
33 | #undef LOWORD
|
---|
34 | #undef HIWORD
|
---|
35 | #undef LOBYTE
|
---|
36 | #undef HIBYTE
|
---|
37 | #include <windows.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #if defined (Q_WS_PM)
|
---|
41 | QMap<int, QString> QIHotKeyEdit::sKeyNames;
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #ifdef Q_WS_X11
|
---|
45 | /* We need to capture some X11 events directly which
|
---|
46 | * requires the XEvent structure to be defined. However,
|
---|
47 | * including the Xlib header file will cause some nasty
|
---|
48 | * conflicts with Qt. Therefore we use the following hack
|
---|
49 | * to redefine those conflicting identifiers. */
|
---|
50 | #define XK_XKB_KEYS
|
---|
51 | #define XK_MISCELLANY
|
---|
52 | #include <X11/Xlib.h>
|
---|
53 | #include <X11/Xutil.h>
|
---|
54 | #include <X11/keysymdef.h>
|
---|
55 | #ifdef KeyPress
|
---|
56 | const int XFocusOut = FocusOut;
|
---|
57 | const int XFocusIn = FocusIn;
|
---|
58 | const int XKeyPress = KeyPress;
|
---|
59 | const int XKeyRelease = KeyRelease;
|
---|
60 | #undef KeyRelease
|
---|
61 | #undef KeyPress
|
---|
62 | #undef FocusOut
|
---|
63 | #undef FocusIn
|
---|
64 | #endif
|
---|
65 | #include "XKeyboard.h"
|
---|
66 | QMap<QString, QString> QIHotKeyEdit::sKeyNames;
|
---|
67 | #include <QX11Info>
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | #ifdef Q_WS_MAC
|
---|
71 | #include "DarwinKeyboard.h"
|
---|
72 | #endif
|
---|
73 |
|
---|
74 |
|
---|
75 | #if defined (Q_WS_WIN32)
|
---|
76 | /**
|
---|
77 | * Returns the correct modifier vkey for the *last* keyboard message,
|
---|
78 | * distinguishing between left and right keys. If both are pressed
|
---|
79 | * the left key wins. If the pressed key not a modifier, wParam is returned
|
---|
80 | * unchanged.
|
---|
81 | */
|
---|
82 | int qi_distinguish_modifier_vkey (WPARAM wParam)
|
---|
83 | {
|
---|
84 | int keyval = wParam;
|
---|
85 | switch (wParam)
|
---|
86 | {
|
---|
87 | case VK_SHIFT:
|
---|
88 | if (::GetKeyState (VK_LSHIFT) & 0x8000) keyval = VK_LSHIFT;
|
---|
89 | else if (::GetKeyState (VK_RSHIFT) & 0x8000) keyval = VK_RSHIFT;
|
---|
90 | break;
|
---|
91 | case VK_CONTROL:
|
---|
92 | if (::GetKeyState (VK_LCONTROL) & 0x8000) keyval = VK_LCONTROL;
|
---|
93 | else if (::GetKeyState (VK_RCONTROL) & 0x8000) keyval = VK_RCONTROL;
|
---|
94 | break;
|
---|
95 | case VK_MENU:
|
---|
96 | if (::GetKeyState (VK_LMENU) & 0x8000) keyval = VK_LMENU;
|
---|
97 | else if (::GetKeyState (VK_RMENU) & 0x8000) keyval = VK_RMENU;
|
---|
98 | break;
|
---|
99 | }
|
---|
100 | return keyval;
|
---|
101 | }
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | /** @class QIHotKeyEdit
|
---|
105 | *
|
---|
106 | * The QIHotKeyEdit widget is a hot key editor.
|
---|
107 | */
|
---|
108 |
|
---|
109 | const char *QIHotKeyEdit::kNoneSymbName = "<none>";
|
---|
110 |
|
---|
111 | QIHotKeyEdit::QIHotKeyEdit (QWidget *aParent) :
|
---|
112 | QLabel (aParent)
|
---|
113 | {
|
---|
114 | #ifdef Q_WS_X11
|
---|
115 | /* Initialize the X keyboard subsystem */
|
---|
116 | initXKeyboard (QX11Info::display());
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | clear();
|
---|
120 |
|
---|
121 | setFrameStyle (QFrame::StyledPanel | Sunken);
|
---|
122 | setAlignment (Qt::AlignCenter);
|
---|
123 | setFocusPolicy (Qt::StrongFocus);
|
---|
124 | setAutoFillBackground (true);
|
---|
125 |
|
---|
126 | QPalette p = palette();
|
---|
127 | p.setColor (QPalette::Active, QPalette::Foreground,
|
---|
128 | p.color (QPalette::Active, QPalette::Text));
|
---|
129 | p.setColor (QPalette::Active, QPalette::Background,
|
---|
130 | p.color (QPalette::Active, QPalette::Base));
|
---|
131 | setPalette (p);
|
---|
132 |
|
---|
133 | #ifdef Q_WS_MAC
|
---|
134 | mDarwinKeyModifiers = GetCurrentEventKeyModifiers();
|
---|
135 |
|
---|
136 | EventTypeSpec eventTypes [4];
|
---|
137 | eventTypes [0].eventClass = kEventClassKeyboard;
|
---|
138 | eventTypes [0].eventKind = kEventRawKeyDown;
|
---|
139 | eventTypes [1].eventClass = kEventClassKeyboard;
|
---|
140 | eventTypes [1].eventKind = kEventRawKeyUp;
|
---|
141 | eventTypes [2].eventClass = kEventClassKeyboard;
|
---|
142 | eventTypes [2].eventKind = kEventRawKeyRepeat;
|
---|
143 | eventTypes [3].eventClass = kEventClassKeyboard;
|
---|
144 | eventTypes [3].eventKind = kEventRawKeyModifiersChanged;
|
---|
145 |
|
---|
146 | EventHandlerUPP eventHandler = ::NewEventHandlerUPP (QIHotKeyEdit::darwinEventHandlerProc);
|
---|
147 |
|
---|
148 | mDarwinEventHandlerRef = NULL;
|
---|
149 | ::InstallApplicationEventHandler (eventHandler, RT_ELEMENTS (eventTypes), &eventTypes [0],
|
---|
150 | this, &mDarwinEventHandlerRef);
|
---|
151 | ::DisposeEventHandlerUPP (eventHandler);
|
---|
152 | ::DarwinGrabKeyboard (false /* just modifiers */);
|
---|
153 | #endif
|
---|
154 | }
|
---|
155 |
|
---|
156 | QIHotKeyEdit::~QIHotKeyEdit()
|
---|
157 | {
|
---|
158 | #ifdef Q_WS_MAC
|
---|
159 | ::DarwinReleaseKeyboard();
|
---|
160 | ::RemoveEventHandler (mDarwinEventHandlerRef);
|
---|
161 | mDarwinEventHandlerRef = NULL;
|
---|
162 | #endif
|
---|
163 | }
|
---|
164 |
|
---|
165 | // Public members
|
---|
166 | /////////////////////////////////////////////////////////////////////////////
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Set the hot key value. O means there is no hot key.
|
---|
170 | *
|
---|
171 | * @note
|
---|
172 | * The key value is platform-dependent. On Win32 it is the
|
---|
173 | * virtial key, on Linux it is the first (0) keysym corresponding
|
---|
174 | * to the keycode.
|
---|
175 | */
|
---|
176 | void QIHotKeyEdit::setKey (int aKeyVal)
|
---|
177 | {
|
---|
178 | mKeyVal = aKeyVal;
|
---|
179 | mSymbName = QIHotKeyEdit::keyName (aKeyVal);
|
---|
180 | updateText();
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**@@ QIHotKeyEdit::key() const
|
---|
184 | *
|
---|
185 | * Returns the value of the last recodred hot key.
|
---|
186 | * O means there is no hot key.
|
---|
187 | *
|
---|
188 | * @note
|
---|
189 | * The key value is platform-dependent. On Win32 it is the
|
---|
190 | * virtial key, on Linux it is the first (0) keysym corresponding
|
---|
191 | * to the keycode.
|
---|
192 | */
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Stolen from QLineEdit.
|
---|
196 | */
|
---|
197 | QSize QIHotKeyEdit::sizeHint() const
|
---|
198 | {
|
---|
199 | ensurePolished();
|
---|
200 | QFontMetrics fm (font());
|
---|
201 | int h = qMax (fm.lineSpacing(), 14) + 2;
|
---|
202 | int w = fm.width ('x') * 17; // "some"
|
---|
203 | int m = frameWidth() * 2;
|
---|
204 | QStyleOption option;
|
---|
205 | option.initFrom (this);
|
---|
206 | return (style()->sizeFromContents (QStyle::CT_LineEdit, &option,
|
---|
207 | QSize (w + m, h + m)
|
---|
208 | .expandedTo (QApplication::globalStrut()),
|
---|
209 | this));
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Stolen from QLineEdit.
|
---|
214 | */
|
---|
215 | QSize QIHotKeyEdit::minimumSizeHint() const
|
---|
216 | {
|
---|
217 | ensurePolished();
|
---|
218 | QFontMetrics fm = fontMetrics();
|
---|
219 | int h = fm.height() + qMax (2, fm.leading());
|
---|
220 | int w = fm.maxWidth();
|
---|
221 | int m = frameWidth() * 2;
|
---|
222 | return QSize (w + m, h + m);
|
---|
223 | }
|
---|
224 |
|
---|
225 | #if defined (Q_WS_PM)
|
---|
226 | /**
|
---|
227 | * Returns the virtual key extracted from the QMSG structure.
|
---|
228 | *
|
---|
229 | * This function tries to detect some extra virtual keys definitions missing
|
---|
230 | * in PM (like Left Shift, Left Ctrl, Win keys). In all other cases it simply
|
---|
231 | * returns SHORT2FROMMP (aMsg->mp2).
|
---|
232 | *
|
---|
233 | * @param aMsg Pointer to the QMSG structure to extract the virtual key from.
|
---|
234 | * @return The extracted virtual key code or zero if there is no virtual key.
|
---|
235 | */
|
---|
236 | /* static */
|
---|
237 | int QIHotKeyEdit::virtualKey (QMSG *aMsg)
|
---|
238 | {
|
---|
239 | USHORT f = SHORT1FROMMP (aMsg->mp1);
|
---|
240 | CHAR scan = CHAR4FROMMP (aMsg->mp1);
|
---|
241 | USHORT ch = SHORT1FROMMP (aMsg->mp2);
|
---|
242 | int vkey = (unsigned int) SHORT2FROMMP (aMsg->mp2);
|
---|
243 |
|
---|
244 | if (f & KC_VIRTUALKEY)
|
---|
245 | {
|
---|
246 | /* distinguish Left Shift from Right Shift) */
|
---|
247 | if (vkey == VK_SHIFT && scan == 0x2A)
|
---|
248 | vkey = VK_LSHIFT;
|
---|
249 | /* distinguish Left Ctrl from Right Ctrl */
|
---|
250 | else if (vkey == VK_CTRL && scan == 0x1D)
|
---|
251 | vkey = VK_LCTRL;
|
---|
252 | /* distinguish Ctrl+ScrLock from Ctrl+Break */
|
---|
253 | else if (vkey == VK_BREAK && scan == 0x46 && f & KC_CTRL)
|
---|
254 | vkey = VK_SCRLLOCK;
|
---|
255 | }
|
---|
256 | else if (!(f & KC_CHAR))
|
---|
257 | {
|
---|
258 | /* detect some special keys that have a pseudo char code in the high
|
---|
259 | * byte of ch (probably this is less device-dependent than
|
---|
260 | * scancode) */
|
---|
261 | switch (ch)
|
---|
262 | {
|
---|
263 | case 0xEC00: vkey = VK_LWIN; break;
|
---|
264 | case 0xED00: vkey = VK_RWIN; break;
|
---|
265 | case 0xEE00: vkey = VK_WINMENU; break;
|
---|
266 | case 0xF900: vkey = VK_FORWARD; break;
|
---|
267 | case 0xFA00: vkey = VK_BACKWARD; break;
|
---|
268 | default: vkey = 0;
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | return vkey;
|
---|
273 | }
|
---|
274 | #endif
|
---|
275 |
|
---|
276 | #if defined (Q_WS_PM)
|
---|
277 | /**
|
---|
278 | * Updates the associative array containing the translations of PM virtual
|
---|
279 | * keys to human readable key names.
|
---|
280 | */
|
---|
281 | void QIHotKeyEdit::retranslateUi()
|
---|
282 | {
|
---|
283 | /* Note: strings for the same key must match strings in retranslateUi()
|
---|
284 | * versions for all platforms, to keep translators happy. */
|
---|
285 |
|
---|
286 | sKeyNames [VK_LSHIFT] = tr ("Left Shift");
|
---|
287 | sKeyNames [VK_SHIFT] = tr ("Right Shift");
|
---|
288 | sKeyNames [VK_LCTRL] = tr ("Left Ctrl");
|
---|
289 | sKeyNames [VK_CTRL] = tr ("Right Ctrl");
|
---|
290 | sKeyNames [VK_ALT] = tr ("Left Alt");
|
---|
291 | sKeyNames [VK_ALTGRAF] = tr ("Right Alt");
|
---|
292 | sKeyNames [VK_LWIN] = tr ("Left WinKey");
|
---|
293 | sKeyNames [VK_RWIN] = tr ("Right WinKey");
|
---|
294 | sKeyNames [VK_WINMENU] = tr ("Menu key");
|
---|
295 | sKeyNames [VK_CAPSLOCK] = tr ("Caps Lock");
|
---|
296 | sKeyNames [VK_SCRLLOCK] = tr ("Scroll Lock");
|
---|
297 |
|
---|
298 | sKeyNames [VK_PAUSE] = tr ("Pause");
|
---|
299 | sKeyNames [VK_PRINTSCRN] = tr ("Print Screen");
|
---|
300 |
|
---|
301 | sKeyNames [VK_F1] = tr ("F1");
|
---|
302 | sKeyNames [VK_F2] = tr ("F2");
|
---|
303 | sKeyNames [VK_F3] = tr ("F3");
|
---|
304 | sKeyNames [VK_F4] = tr ("F4");
|
---|
305 | sKeyNames [VK_F5] = tr ("F5");
|
---|
306 | sKeyNames [VK_F6] = tr ("F6");
|
---|
307 | sKeyNames [VK_F7] = tr ("F7");
|
---|
308 | sKeyNames [VK_F8] = tr ("F8");
|
---|
309 | sKeyNames [VK_F9] = tr ("F9");
|
---|
310 | sKeyNames [VK_F10] = tr ("F10");
|
---|
311 | sKeyNames [VK_F11] = tr ("F11");
|
---|
312 | sKeyNames [VK_F12] = tr ("F12");
|
---|
313 | sKeyNames [VK_F13] = tr ("F13");
|
---|
314 | sKeyNames [VK_F14] = tr ("F14");
|
---|
315 | sKeyNames [VK_F15] = tr ("F15");
|
---|
316 | sKeyNames [VK_F16] = tr ("F16");
|
---|
317 | sKeyNames [VK_F17] = tr ("F17");
|
---|
318 | sKeyNames [VK_F18] = tr ("F18");
|
---|
319 | sKeyNames [VK_F19] = tr ("F19");
|
---|
320 | sKeyNames [VK_F20] = tr ("F20");
|
---|
321 | sKeyNames [VK_F21] = tr ("F21");
|
---|
322 | sKeyNames [VK_F22] = tr ("F22");
|
---|
323 | sKeyNames [VK_F23] = tr ("F23");
|
---|
324 | sKeyNames [VK_F24] = tr ("F24");
|
---|
325 |
|
---|
326 | sKeyNames [VK_NUMLOCK] = tr ("Num Lock");
|
---|
327 | sKeyNames [VK_FORWARD] = tr ("Forward");
|
---|
328 | sKeyNames [VK_BACKWARD] = tr ("Back");
|
---|
329 | }
|
---|
330 | #elif defined (Q_WS_X11)
|
---|
331 | /**
|
---|
332 | * Updates the associative array containing the translations of X11 key strings to human
|
---|
333 | * readable key names.
|
---|
334 | */
|
---|
335 | void QIHotKeyEdit::retranslateUi()
|
---|
336 | {
|
---|
337 | /* Note: strings for the same key must match strings in retranslateUi()
|
---|
338 | * versions for all platforms, to keep translators happy. */
|
---|
339 |
|
---|
340 | sKeyNames ["Shift_L"] = tr ("Left Shift");
|
---|
341 | sKeyNames ["Shift_R"] = tr ("Right Shift");
|
---|
342 | sKeyNames ["Control_L"] = tr ("Left Ctrl");
|
---|
343 | sKeyNames ["Control_R"] = tr ("Right Ctrl");
|
---|
344 | sKeyNames ["Alt_L"] = tr ("Left Alt");
|
---|
345 | sKeyNames ["Alt_R"] = tr ("Right Alt");
|
---|
346 | sKeyNames ["Super_L"] = tr ("Left WinKey");
|
---|
347 | sKeyNames ["Super_R"] = tr ("Right WinKey");
|
---|
348 | sKeyNames ["Menu"] = tr ("Menu key");
|
---|
349 | sKeyNames ["ISO_Level3_Shift"] = tr ("Alt Gr");
|
---|
350 | sKeyNames ["Caps_Lock"] = tr ("Caps Lock");
|
---|
351 | sKeyNames ["Scroll_Lock"] = tr ("Scroll Lock");
|
---|
352 | }
|
---|
353 | #endif
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Returns the string representation of a given key.
|
---|
357 | *
|
---|
358 | * @note
|
---|
359 | * The key value is platform-dependent. On Win32 it is the
|
---|
360 | * virtial key, on Linux it is the first (0) keysym corresponding
|
---|
361 | * to the keycode.
|
---|
362 | */
|
---|
363 | /* static */
|
---|
364 | QString QIHotKeyEdit::keyName (int aKeyVal)
|
---|
365 | {
|
---|
366 | QString name;
|
---|
367 |
|
---|
368 | if (!aKeyVal)
|
---|
369 | {
|
---|
370 | name = tr (kNoneSymbName);
|
---|
371 | }
|
---|
372 | else
|
---|
373 | {
|
---|
374 | #if defined (Q_WS_WIN32)
|
---|
375 | /* stupid MapVirtualKey doesn't distinguish between right and left
|
---|
376 | * vkeys, even under XP, despite that it stated in msdn. do it by
|
---|
377 | * hand. */
|
---|
378 | int scan;
|
---|
379 | switch (aKeyVal)
|
---|
380 | {
|
---|
381 | /* Processing special keys... */
|
---|
382 | case VK_RSHIFT: scan = 0x36 << 16; break;
|
---|
383 | case VK_RCONTROL: scan = (0x1D << 16) | (1 << 24); break;
|
---|
384 | case VK_RMENU: scan = (0x38 << 16) | (1 << 24); break;
|
---|
385 | /* Processing extended keys... */
|
---|
386 | case VK_APPS:
|
---|
387 | case VK_LWIN:
|
---|
388 | case VK_RWIN:
|
---|
389 | case VK_PAUSE:
|
---|
390 | case VK_NUMLOCK: scan = (::MapVirtualKey (aKeyVal, 0) | 256) << 16; break;
|
---|
391 | default: scan = ::MapVirtualKey (aKeyVal, 0) << 16;
|
---|
392 | }
|
---|
393 | TCHAR *str = new TCHAR [256];
|
---|
394 | if (::GetKeyNameText (scan, str, 256))
|
---|
395 | {
|
---|
396 | name = QString::fromUtf16 (str);
|
---|
397 | }
|
---|
398 | else
|
---|
399 | {
|
---|
400 | AssertFailed();
|
---|
401 | name = QString (tr ("<key_%1>")).arg (aKeyVal);
|
---|
402 | }
|
---|
403 | delete[] str;
|
---|
404 | #elif defined (Q_WS_PM)
|
---|
405 | name = sKeyNames [aKeyVal];
|
---|
406 | if (name.isNull())
|
---|
407 | {
|
---|
408 | AssertFailed();
|
---|
409 | name = QString (tr ("<key_%1>")).arg (aKeyVal);
|
---|
410 | }
|
---|
411 | #elif defined (Q_WS_X11)
|
---|
412 | char *sn = ::XKeysymToString ((KeySym) aKeyVal);
|
---|
413 | if (sn)
|
---|
414 | {
|
---|
415 | name = sKeyNames [sn];
|
---|
416 | if (name.isEmpty())
|
---|
417 | name = sn;
|
---|
418 | }
|
---|
419 | else
|
---|
420 | {
|
---|
421 | AssertFailed();
|
---|
422 | name = QString (tr ("<key_%1>")).arg (aKeyVal);
|
---|
423 | }
|
---|
424 | #elif defined(Q_WS_MAC)
|
---|
425 | UInt32 modMask = DarwinKeyCodeToDarwinModifierMask (aKeyVal);
|
---|
426 | switch (modMask)
|
---|
427 | {
|
---|
428 | case shiftKey:
|
---|
429 | case optionKey:
|
---|
430 | case controlKey:
|
---|
431 | case cmdKey:
|
---|
432 | name = tr ("Left ");
|
---|
433 | break;
|
---|
434 | case rightShiftKey:
|
---|
435 | case rightOptionKey:
|
---|
436 | case rightControlKey:
|
---|
437 | case kEventKeyModifierRightCmdKeyMask:
|
---|
438 | name = tr ("Right ");
|
---|
439 | break;
|
---|
440 | default:
|
---|
441 | AssertMsgFailedReturn (("modMask=%#x\n", modMask), QString());
|
---|
442 | }
|
---|
443 | switch (modMask)
|
---|
444 | {
|
---|
445 | case shiftKey:
|
---|
446 | case rightShiftKey:
|
---|
447 | name += QChar (kShiftUnicode);
|
---|
448 | break;
|
---|
449 | case optionKey:
|
---|
450 | case rightOptionKey:
|
---|
451 | name += QChar (kOptionUnicode);
|
---|
452 | break;
|
---|
453 | case controlKey:
|
---|
454 | case rightControlKey:
|
---|
455 | name += QChar (kControlUnicode);
|
---|
456 | break;
|
---|
457 | case cmdKey:
|
---|
458 | case kEventKeyModifierRightCmdKeyMask:
|
---|
459 | name += QChar (kCommandUnicode);
|
---|
460 | break;
|
---|
461 | }
|
---|
462 | #else
|
---|
463 | AssertFailed();
|
---|
464 | name = QString (tr ("<key_%1>")).arg (aKeyVal);
|
---|
465 | #endif
|
---|
466 | }
|
---|
467 |
|
---|
468 | return name;
|
---|
469 | }
|
---|
470 |
|
---|
471 | /* static */
|
---|
472 | bool QIHotKeyEdit::isValidKey (int aKeyVal)
|
---|
473 | {
|
---|
474 | #if defined(Q_WS_WIN32)
|
---|
475 | return (
|
---|
476 | (aKeyVal >= VK_SHIFT && aKeyVal <= VK_CAPITAL) ||
|
---|
477 | aKeyVal == VK_PRINT ||
|
---|
478 | aKeyVal == VK_LWIN || aKeyVal == VK_RWIN ||
|
---|
479 | aKeyVal == VK_APPS ||
|
---|
480 | (aKeyVal >= VK_F1 && aKeyVal <= VK_F24) ||
|
---|
481 | aKeyVal == VK_NUMLOCK || aKeyVal == VK_SCROLL ||
|
---|
482 | (aKeyVal >= VK_LSHIFT && aKeyVal <= VK_RMENU));
|
---|
483 | #elif defined(Q_WS_PM)
|
---|
484 | return (
|
---|
485 | (aKeyVal >= VK_SHIFT && aKeyVal <= VK_CAPSLOCK) ||
|
---|
486 | aKeyVal == VK_PRINTSCRN ||
|
---|
487 | (aKeyVal >= VK_F1 && aKeyVal <= VK_F24) ||
|
---|
488 | aKeyVal == VK_NUMLOCK || aKeyVal == VK_SCRLLOCK ||
|
---|
489 | (aKeyVal >= VK_LSHIFT && aKeyVal <= VK_BACKWARD));
|
---|
490 | #elif defined(Q_WS_X11)
|
---|
491 | KeySym ks = (KeySym) aKeyVal;
|
---|
492 | return
|
---|
493 | (
|
---|
494 | ks != NoSymbol &&
|
---|
495 | ks != XK_Insert
|
---|
496 | ) && (
|
---|
497 | ks == XK_Scroll_Lock ||
|
---|
498 | IsModifierKey (ks) ||
|
---|
499 | IsFunctionKey (ks) ||
|
---|
500 | IsMiscFunctionKey (ks)
|
---|
501 | );
|
---|
502 | #elif defined(Q_WS_MAC)
|
---|
503 | UInt32 modMask = ::DarwinKeyCodeToDarwinModifierMask (aKeyVal);
|
---|
504 | switch (modMask)
|
---|
505 | {
|
---|
506 | case shiftKey:
|
---|
507 | case optionKey:
|
---|
508 | case controlKey:
|
---|
509 | case rightShiftKey:
|
---|
510 | case rightOptionKey:
|
---|
511 | case rightControlKey:
|
---|
512 | case cmdKey:
|
---|
513 | case kEventKeyModifierRightCmdKeyMask:
|
---|
514 | return true;
|
---|
515 | default:
|
---|
516 | return false;
|
---|
517 | }
|
---|
518 | #else
|
---|
519 | Q_UNUSED (aKeyVal);
|
---|
520 | return true;
|
---|
521 | #endif
|
---|
522 | }
|
---|
523 |
|
---|
524 | // Public slots
|
---|
525 | /////////////////////////////////////////////////////////////////////////////
|
---|
526 |
|
---|
527 | void QIHotKeyEdit::clear()
|
---|
528 | {
|
---|
529 | mKeyVal = 0;
|
---|
530 | mSymbName = tr (kNoneSymbName);
|
---|
531 | updateText();
|
---|
532 | }
|
---|
533 |
|
---|
534 | // Protected members
|
---|
535 | /////////////////////////////////////////////////////////////////////////////
|
---|
536 |
|
---|
537 | // Protected events
|
---|
538 | /////////////////////////////////////////////////////////////////////////////
|
---|
539 |
|
---|
540 | #if defined (Q_WS_WIN32)
|
---|
541 |
|
---|
542 | bool QIHotKeyEdit::winEvent (MSG *aMsg, long* /* aResult */)
|
---|
543 | {
|
---|
544 | if (!(aMsg->message == WM_KEYDOWN || aMsg->message == WM_SYSKEYDOWN ||
|
---|
545 | aMsg->message == WM_KEYUP || aMsg->message == WM_SYSKEYUP ||
|
---|
546 | aMsg->message == WM_CHAR || aMsg->message == WM_SYSCHAR ||
|
---|
547 | aMsg->message == WM_DEADCHAR || aMsg->message == WM_SYSDEADCHAR ||
|
---|
548 | aMsg->message == WM_CONTEXTMENU))
|
---|
549 | return false;
|
---|
550 |
|
---|
551 | /* ignore if not a valid hot key */
|
---|
552 | if (!isValidKey (aMsg->wParam))
|
---|
553 | return false;
|
---|
554 |
|
---|
555 | #if 0
|
---|
556 | LogFlow (("%WM_%04X: vk=%04X rep=%05d scan=%02X ext=%01d"
|
---|
557 | "rzv=%01X ctx=%01d prev=%01d tran=%01d\n",
|
---|
558 | aMsg->message, aMsg->wParam,
|
---|
559 | (aMsg->lParam & 0xFFFF),
|
---|
560 | ((aMsg->lParam >> 16) & 0xFF),
|
---|
561 | ((aMsg->lParam >> 24) & 0x1),
|
---|
562 | ((aMsg->lParam >> 25) & 0xF),
|
---|
563 | ((aMsg->lParam >> 29) & 0x1),
|
---|
564 | ((aMsg->lParam >> 30) & 0x1),
|
---|
565 | ((aMsg->lParam >> 31) & 0x1)));
|
---|
566 | #endif
|
---|
567 |
|
---|
568 | if (aMsg->message == WM_KEYDOWN || aMsg->message == WM_SYSKEYDOWN)
|
---|
569 | {
|
---|
570 | /* determine platform-dependent key */
|
---|
571 | mKeyVal = qi_distinguish_modifier_vkey (aMsg->wParam);
|
---|
572 | /* determine symbolic name */
|
---|
573 | TCHAR *str = new TCHAR [256];
|
---|
574 | if (::GetKeyNameText (aMsg->lParam, str, 256))
|
---|
575 | {
|
---|
576 | mSymbName = QString::fromUtf16 (str);
|
---|
577 | }
|
---|
578 | else
|
---|
579 | {
|
---|
580 | AssertFailed();
|
---|
581 | mSymbName = QString (tr ("<key_%1>")).arg (mKeyVal);
|
---|
582 | }
|
---|
583 | delete[] str;
|
---|
584 | /* update the display */
|
---|
585 | updateText();
|
---|
586 | }
|
---|
587 |
|
---|
588 | return true;
|
---|
589 | }
|
---|
590 |
|
---|
591 | #elif defined (Q_WS_PM)
|
---|
592 |
|
---|
593 | bool QIHotKeyEdit::pmEvent (QMSG *aMsg)
|
---|
594 | {
|
---|
595 | if (aMsg->msg != WM_CHAR)
|
---|
596 | return false;
|
---|
597 |
|
---|
598 | USHORT f = SHORT1FROMMP (aMsg->mp1);
|
---|
599 |
|
---|
600 | int vkey = QIHotKeyEdit::virtualKey (aMsg);
|
---|
601 |
|
---|
602 | /* ignore if not a valid hot key */
|
---|
603 | if (!isValidKey (vkey))
|
---|
604 | return false;
|
---|
605 |
|
---|
606 | if (!(f & KC_KEYUP))
|
---|
607 | {
|
---|
608 | /* determine platform-dependent key */
|
---|
609 | mKeyVal = vkey;
|
---|
610 | /* determine symbolic name */
|
---|
611 | mSymbName = QIHotKeyEdit::keyName (mKeyVal);
|
---|
612 | /* update the display */
|
---|
613 | updateText();
|
---|
614 | }
|
---|
615 |
|
---|
616 | return true;
|
---|
617 | }
|
---|
618 |
|
---|
619 | #elif defined (Q_WS_X11)
|
---|
620 |
|
---|
621 | bool QIHotKeyEdit::x11Event (XEvent *event)
|
---|
622 | {
|
---|
623 | switch (event->type)
|
---|
624 | {
|
---|
625 | case XKeyPress:
|
---|
626 | case XKeyRelease:
|
---|
627 | {
|
---|
628 | XKeyEvent *ke = (XKeyEvent *) event;
|
---|
629 | KeySym ks = ::XKeycodeToKeysym (ke->display, ke->keycode, 0);
|
---|
630 | /* ignore if not a valid hot key */
|
---|
631 | if (!isValidKey ((int) ks))
|
---|
632 | return false;
|
---|
633 |
|
---|
634 | /* skip key releases */
|
---|
635 | if (event->type == XKeyRelease)
|
---|
636 | return true;
|
---|
637 |
|
---|
638 | /* determine platform-dependent key */
|
---|
639 | mKeyVal = (int) ks;
|
---|
640 | /* determine symbolic name */
|
---|
641 | mSymbName = QIHotKeyEdit::keyName (mKeyVal);
|
---|
642 | /* update the display */
|
---|
643 | updateText();
|
---|
644 | #if 0
|
---|
645 | LogFlow (("%s: state=%08X keycode=%08X keysym=%08X symb=%s\n",
|
---|
646 | event->type == XKeyPress ? "XKeyPress" : "XKeyRelease",
|
---|
647 | ke->state, ke->keycode, ks,
|
---|
648 | symbname.latin1()));
|
---|
649 | #endif
|
---|
650 | return true;
|
---|
651 | }
|
---|
652 | }
|
---|
653 |
|
---|
654 | return false;
|
---|
655 | }
|
---|
656 |
|
---|
657 | #elif defined (Q_WS_MAC)
|
---|
658 |
|
---|
659 | /* static */
|
---|
660 | pascal OSStatus QIHotKeyEdit::darwinEventHandlerProc (EventHandlerCallRef inHandlerCallRef,
|
---|
661 | EventRef inEvent, void *inUserData)
|
---|
662 | {
|
---|
663 | QIHotKeyEdit *edit = (QIHotKeyEdit *) inUserData;
|
---|
664 | UInt32 EventClass = ::GetEventClass (inEvent);
|
---|
665 | if (EventClass == kEventClassKeyboard)
|
---|
666 | {
|
---|
667 | if (edit->darwinKeyboardEvent (inEvent))
|
---|
668 | return 0;
|
---|
669 | }
|
---|
670 | return CallNextEventHandler (inHandlerCallRef, inEvent);
|
---|
671 | }
|
---|
672 |
|
---|
673 | bool QIHotKeyEdit::darwinKeyboardEvent (EventRef inEvent)
|
---|
674 | {
|
---|
675 | /* ignore key changes unless we're the focus widget */
|
---|
676 | if (!hasFocus())
|
---|
677 | return false;
|
---|
678 |
|
---|
679 | UInt32 eventKind = ::GetEventKind (inEvent);
|
---|
680 | switch (eventKind)
|
---|
681 | {
|
---|
682 | /*case kEventRawKeyDown:
|
---|
683 | case kEventRawKeyUp:
|
---|
684 | case kEventRawKeyRepeat:*/
|
---|
685 | case kEventRawKeyModifiersChanged:
|
---|
686 | {
|
---|
687 | UInt32 modifierMask = 0;
|
---|
688 | ::GetEventParameter (inEvent, kEventParamKeyModifiers, typeUInt32, NULL,
|
---|
689 | sizeof (modifierMask), NULL, &modifierMask);
|
---|
690 |
|
---|
691 | modifierMask = ::DarwinAdjustModifierMask (modifierMask);
|
---|
692 | UInt32 changed = mDarwinKeyModifiers ^ modifierMask;
|
---|
693 | mDarwinKeyModifiers = modifierMask;
|
---|
694 |
|
---|
695 | /* skip key releases */
|
---|
696 | if (changed && (changed & modifierMask))
|
---|
697 | break;
|
---|
698 |
|
---|
699 | /* convert to keycode and skip keycodes we don't care about. */
|
---|
700 | unsigned keyCode = ::DarwinModifierMaskToDarwinKeycode (changed);
|
---|
701 | if (!keyCode || keyCode == ~0U || !isValidKey (keyCode))
|
---|
702 | break;
|
---|
703 |
|
---|
704 | /* update key current key. */
|
---|
705 | mKeyVal = keyCode;
|
---|
706 | mSymbName = QIHotKeyEdit::keyName (keyCode);
|
---|
707 | updateText();
|
---|
708 | break; //return true;
|
---|
709 | }
|
---|
710 | break;
|
---|
711 | }
|
---|
712 | return false;
|
---|
713 | }
|
---|
714 |
|
---|
715 | #else
|
---|
716 | # warning "Port me!"
|
---|
717 | #endif
|
---|
718 |
|
---|
719 | void QIHotKeyEdit::focusInEvent (QFocusEvent *aEvent)
|
---|
720 | {
|
---|
721 | QLabel::focusInEvent (aEvent);
|
---|
722 |
|
---|
723 | QPalette p = palette();
|
---|
724 | p.setColor (QPalette::Active, QPalette::Foreground,
|
---|
725 | p.color (QPalette::Active, QPalette::HighlightedText));
|
---|
726 | p.setColor (QPalette::Active, QPalette::Background,
|
---|
727 | p.color (QPalette::Active, QPalette::Highlight));
|
---|
728 | setPalette (p);
|
---|
729 | }
|
---|
730 |
|
---|
731 | void QIHotKeyEdit::focusOutEvent (QFocusEvent *aEvent)
|
---|
732 | {
|
---|
733 | QLabel::focusOutEvent (aEvent);
|
---|
734 |
|
---|
735 | QPalette p = palette();
|
---|
736 | p.setColor (QPalette::Active, QPalette::Foreground,
|
---|
737 | p.color (QPalette::Active, QPalette::Text));
|
---|
738 | p.setColor (QPalette::Active, QPalette::Background,
|
---|
739 | p.color (QPalette::Active, QPalette::Base));
|
---|
740 | setPalette (p);
|
---|
741 | }
|
---|
742 |
|
---|
743 | void QIHotKeyEdit::paintEvent (QPaintEvent *aEvent)
|
---|
744 | {
|
---|
745 | if (hasFocus())
|
---|
746 | {
|
---|
747 | QStylePainter painter (this);
|
---|
748 | QStyleOptionFocusRect option;
|
---|
749 | option.initFrom (this);
|
---|
750 | option.backgroundColor = palette().color (QPalette::Background);
|
---|
751 | option.rect = contentsRect();
|
---|
752 | painter.drawPrimitive (QStyle::PE_FrameFocusRect, option);
|
---|
753 | }
|
---|
754 | QLabel::paintEvent (aEvent);
|
---|
755 | }
|
---|
756 |
|
---|
757 | // Private members
|
---|
758 | /////////////////////////////////////////////////////////////////////////////
|
---|
759 |
|
---|
760 | void QIHotKeyEdit::updateText()
|
---|
761 | {
|
---|
762 | setText (QString (" %1 ").arg (mSymbName));
|
---|
763 | }
|
---|
764 |
|
---|