VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox4/src/QIHotKeyEdit.cpp@ 14548

Last change on this file since 14548 was 14548, checked in by vboxsync, 16 years ago

FE/Qt4: 3266: "Wrong host-key displayed" : Pause (VK_PAUSE) key case fixed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.9 KB
Line 
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)
41QMap<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
56const int XFocusOut = FocusOut;
57const int XFocusIn = FocusIn;
58const int XKeyPress = KeyPress;
59const int XKeyRelease = KeyRelease;
60#undef KeyRelease
61#undef KeyPress
62#undef FocusOut
63#undef FocusIn
64#endif
65#include "XKeyboard.h"
66QMap<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 */
82int 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
109const char *QIHotKeyEdit::kNoneSymbName = "<none>";
110
111QIHotKeyEdit::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
156QIHotKeyEdit::~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 */
176void 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 */
197QSize 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 */
215QSize 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 */
237int 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 */
281void 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 */
335void 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 */
364QString 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 * hands. Besides that it can't recognize such virtual keys as
378 * VK_DIVIDE & VK_PAUSE, this is also known bug. */
379 int scan;
380 switch (aKeyVal)
381 {
382 /* Processing special keys... */
383 case VK_PAUSE: scan = 0x45 << 16; break;
384 case VK_RSHIFT: scan = 0x36 << 16; break;
385 case VK_RCONTROL: scan = (0x1D << 16) | (1 << 24); break;
386 case VK_RMENU: scan = (0x38 << 16) | (1 << 24); break;
387 /* Processing extended keys... */
388 case VK_APPS:
389 case VK_LWIN:
390 case VK_RWIN:
391 case VK_NUMLOCK: scan = (::MapVirtualKey (aKeyVal, 0) | 256) << 16; break;
392 default: scan = ::MapVirtualKey (aKeyVal, 0) << 16;
393 }
394 TCHAR *str = new TCHAR [256];
395 if (::GetKeyNameText (scan, str, 256))
396 {
397 name = QString::fromUtf16 (str);
398 }
399 else
400 {
401 AssertFailed();
402 name = QString (tr ("<key_%1>")).arg (aKeyVal);
403 }
404 delete[] str;
405#elif defined (Q_WS_PM)
406 name = sKeyNames [aKeyVal];
407 if (name.isNull())
408 {
409 AssertFailed();
410 name = QString (tr ("<key_%1>")).arg (aKeyVal);
411 }
412#elif defined (Q_WS_X11)
413 char *sn = ::XKeysymToString ((KeySym) aKeyVal);
414 if (sn)
415 {
416 name = sKeyNames [sn];
417 if (name.isEmpty())
418 name = sn;
419 }
420 else
421 {
422 AssertFailed();
423 name = QString (tr ("<key_%1>")).arg (aKeyVal);
424 }
425#elif defined(Q_WS_MAC)
426 UInt32 modMask = DarwinKeyCodeToDarwinModifierMask (aKeyVal);
427 switch (modMask)
428 {
429 case shiftKey:
430 case optionKey:
431 case controlKey:
432 case cmdKey:
433 name = tr ("Left ");
434 break;
435 case rightShiftKey:
436 case rightOptionKey:
437 case rightControlKey:
438 case kEventKeyModifierRightCmdKeyMask:
439 name = tr ("Right ");
440 break;
441 default:
442 AssertMsgFailedReturn (("modMask=%#x\n", modMask), QString());
443 }
444 switch (modMask)
445 {
446 case shiftKey:
447 case rightShiftKey:
448 name += QChar (kShiftUnicode);
449 break;
450 case optionKey:
451 case rightOptionKey:
452 name += QChar (kOptionUnicode);
453 break;
454 case controlKey:
455 case rightControlKey:
456 name += QChar (kControlUnicode);
457 break;
458 case cmdKey:
459 case kEventKeyModifierRightCmdKeyMask:
460 name += QChar (kCommandUnicode);
461 break;
462 }
463#else
464 AssertFailed();
465 name = QString (tr ("<key_%1>")).arg (aKeyVal);
466#endif
467 }
468
469 return name;
470}
471
472/* static */
473bool QIHotKeyEdit::isValidKey (int aKeyVal)
474{
475#if defined(Q_WS_WIN32)
476 return (
477 (aKeyVal >= VK_SHIFT && aKeyVal <= VK_CAPITAL) ||
478 aKeyVal == VK_PRINT ||
479 aKeyVal == VK_LWIN || aKeyVal == VK_RWIN ||
480 aKeyVal == VK_APPS ||
481 (aKeyVal >= VK_F1 && aKeyVal <= VK_F24) ||
482 aKeyVal == VK_NUMLOCK || aKeyVal == VK_SCROLL ||
483 (aKeyVal >= VK_LSHIFT && aKeyVal <= VK_RMENU));
484#elif defined(Q_WS_PM)
485 return (
486 (aKeyVal >= VK_SHIFT && aKeyVal <= VK_CAPSLOCK) ||
487 aKeyVal == VK_PRINTSCRN ||
488 (aKeyVal >= VK_F1 && aKeyVal <= VK_F24) ||
489 aKeyVal == VK_NUMLOCK || aKeyVal == VK_SCRLLOCK ||
490 (aKeyVal >= VK_LSHIFT && aKeyVal <= VK_BACKWARD));
491#elif defined(Q_WS_X11)
492 KeySym ks = (KeySym) aKeyVal;
493 return
494 (
495 ks != NoSymbol &&
496 ks != XK_Insert
497 ) && (
498 ks == XK_Scroll_Lock ||
499 IsModifierKey (ks) ||
500 IsFunctionKey (ks) ||
501 IsMiscFunctionKey (ks)
502 );
503#elif defined(Q_WS_MAC)
504 UInt32 modMask = ::DarwinKeyCodeToDarwinModifierMask (aKeyVal);
505 switch (modMask)
506 {
507 case shiftKey:
508 case optionKey:
509 case controlKey:
510 case rightShiftKey:
511 case rightOptionKey:
512 case rightControlKey:
513 case cmdKey:
514 case kEventKeyModifierRightCmdKeyMask:
515 return true;
516 default:
517 return false;
518 }
519#else
520 Q_UNUSED (aKeyVal);
521 return true;
522#endif
523}
524
525// Public slots
526/////////////////////////////////////////////////////////////////////////////
527
528void QIHotKeyEdit::clear()
529{
530 mKeyVal = 0;
531 mSymbName = tr (kNoneSymbName);
532 updateText();
533}
534
535// Protected members
536/////////////////////////////////////////////////////////////////////////////
537
538// Protected events
539/////////////////////////////////////////////////////////////////////////////
540
541#if defined (Q_WS_WIN32)
542
543bool QIHotKeyEdit::winEvent (MSG *aMsg, long* /* aResult */)
544{
545 if (!(aMsg->message == WM_KEYDOWN || aMsg->message == WM_SYSKEYDOWN ||
546 aMsg->message == WM_KEYUP || aMsg->message == WM_SYSKEYUP ||
547 aMsg->message == WM_CHAR || aMsg->message == WM_SYSCHAR ||
548 aMsg->message == WM_DEADCHAR || aMsg->message == WM_SYSDEADCHAR ||
549 aMsg->message == WM_CONTEXTMENU))
550 return false;
551
552 /* ignore if not a valid hot key */
553 if (!isValidKey (aMsg->wParam))
554 return false;
555
556#if 0
557 LogFlow (("%WM_%04X: vk=%04X rep=%05d scan=%02X ext=%01d"
558 "rzv=%01X ctx=%01d prev=%01d tran=%01d\n",
559 aMsg->message, aMsg->wParam,
560 (aMsg->lParam & 0xFFFF),
561 ((aMsg->lParam >> 16) & 0xFF),
562 ((aMsg->lParam >> 24) & 0x1),
563 ((aMsg->lParam >> 25) & 0xF),
564 ((aMsg->lParam >> 29) & 0x1),
565 ((aMsg->lParam >> 30) & 0x1),
566 ((aMsg->lParam >> 31) & 0x1)));
567#endif
568
569 if (aMsg->message == WM_KEYDOWN || aMsg->message == WM_SYSKEYDOWN)
570 {
571 /* determine platform-dependent key */
572 mKeyVal = qi_distinguish_modifier_vkey (aMsg->wParam);
573 /* determine symbolic name */
574 TCHAR *str = new TCHAR [256];
575 if (::GetKeyNameText (aMsg->lParam, str, 256))
576 {
577 mSymbName = QString::fromUtf16 (str);
578 }
579 else
580 {
581 AssertFailed();
582 mSymbName = QString (tr ("<key_%1>")).arg (mKeyVal);
583 }
584 delete[] str;
585 /* update the display */
586 updateText();
587 }
588
589 return true;
590}
591
592#elif defined (Q_WS_PM)
593
594bool QIHotKeyEdit::pmEvent (QMSG *aMsg)
595{
596 if (aMsg->msg != WM_CHAR)
597 return false;
598
599 USHORT f = SHORT1FROMMP (aMsg->mp1);
600
601 int vkey = QIHotKeyEdit::virtualKey (aMsg);
602
603 /* ignore if not a valid hot key */
604 if (!isValidKey (vkey))
605 return false;
606
607 if (!(f & KC_KEYUP))
608 {
609 /* determine platform-dependent key */
610 mKeyVal = vkey;
611 /* determine symbolic name */
612 mSymbName = QIHotKeyEdit::keyName (mKeyVal);
613 /* update the display */
614 updateText();
615 }
616
617 return true;
618}
619
620#elif defined (Q_WS_X11)
621
622bool QIHotKeyEdit::x11Event (XEvent *event)
623{
624 switch (event->type)
625 {
626 case XKeyPress:
627 case XKeyRelease:
628 {
629 XKeyEvent *ke = (XKeyEvent *) event;
630 KeySym ks = ::XKeycodeToKeysym (ke->display, ke->keycode, 0);
631 /* ignore if not a valid hot key */
632 if (!isValidKey ((int) ks))
633 return false;
634
635 /* skip key releases */
636 if (event->type == XKeyRelease)
637 return true;
638
639 /* determine platform-dependent key */
640 mKeyVal = (int) ks;
641 /* determine symbolic name */
642 mSymbName = QIHotKeyEdit::keyName (mKeyVal);
643 /* update the display */
644 updateText();
645#if 0
646 LogFlow (("%s: state=%08X keycode=%08X keysym=%08X symb=%s\n",
647 event->type == XKeyPress ? "XKeyPress" : "XKeyRelease",
648 ke->state, ke->keycode, ks,
649 symbname.latin1()));
650#endif
651 return true;
652 }
653 }
654
655 return false;
656}
657
658#elif defined (Q_WS_MAC)
659
660/* static */
661pascal OSStatus QIHotKeyEdit::darwinEventHandlerProc (EventHandlerCallRef inHandlerCallRef,
662 EventRef inEvent, void *inUserData)
663{
664 QIHotKeyEdit *edit = (QIHotKeyEdit *) inUserData;
665 UInt32 EventClass = ::GetEventClass (inEvent);
666 if (EventClass == kEventClassKeyboard)
667 {
668 if (edit->darwinKeyboardEvent (inEvent))
669 return 0;
670 }
671 return CallNextEventHandler (inHandlerCallRef, inEvent);
672}
673
674bool QIHotKeyEdit::darwinKeyboardEvent (EventRef inEvent)
675{
676 /* ignore key changes unless we're the focus widget */
677 if (!hasFocus())
678 return false;
679
680 UInt32 eventKind = ::GetEventKind (inEvent);
681 switch (eventKind)
682 {
683 /*case kEventRawKeyDown:
684 case kEventRawKeyUp:
685 case kEventRawKeyRepeat:*/
686 case kEventRawKeyModifiersChanged:
687 {
688 UInt32 modifierMask = 0;
689 ::GetEventParameter (inEvent, kEventParamKeyModifiers, typeUInt32, NULL,
690 sizeof (modifierMask), NULL, &modifierMask);
691
692 modifierMask = ::DarwinAdjustModifierMask (modifierMask);
693 UInt32 changed = mDarwinKeyModifiers ^ modifierMask;
694 mDarwinKeyModifiers = modifierMask;
695
696 /* skip key releases */
697 if (changed && (changed & modifierMask))
698 break;
699
700 /* convert to keycode and skip keycodes we don't care about. */
701 unsigned keyCode = ::DarwinModifierMaskToDarwinKeycode (changed);
702 if (!keyCode || keyCode == ~0U || !isValidKey (keyCode))
703 break;
704
705 /* update key current key. */
706 mKeyVal = keyCode;
707 mSymbName = QIHotKeyEdit::keyName (keyCode);
708 updateText();
709 break; //return true;
710 }
711 break;
712 }
713 return false;
714}
715
716#else
717# warning "Port me!"
718#endif
719
720void QIHotKeyEdit::focusInEvent (QFocusEvent *aEvent)
721{
722 QLabel::focusInEvent (aEvent);
723
724 QPalette p = palette();
725 p.setColor (QPalette::Active, QPalette::Foreground,
726 p.color (QPalette::Active, QPalette::HighlightedText));
727 p.setColor (QPalette::Active, QPalette::Background,
728 p.color (QPalette::Active, QPalette::Highlight));
729 setPalette (p);
730}
731
732void QIHotKeyEdit::focusOutEvent (QFocusEvent *aEvent)
733{
734 QLabel::focusOutEvent (aEvent);
735
736 QPalette p = palette();
737 p.setColor (QPalette::Active, QPalette::Foreground,
738 p.color (QPalette::Active, QPalette::Text));
739 p.setColor (QPalette::Active, QPalette::Background,
740 p.color (QPalette::Active, QPalette::Base));
741 setPalette (p);
742}
743
744void QIHotKeyEdit::paintEvent (QPaintEvent *aEvent)
745{
746 if (hasFocus())
747 {
748 QStylePainter painter (this);
749 QStyleOptionFocusRect option;
750 option.initFrom (this);
751 option.backgroundColor = palette().color (QPalette::Background);
752 option.rect = contentsRect();
753 painter.drawPrimitive (QStyle::PE_FrameFocusRect, option);
754 }
755 QLabel::paintEvent (aEvent);
756}
757
758// Private members
759/////////////////////////////////////////////////////////////////////////////
760
761void QIHotKeyEdit::updateText()
762{
763 setText (QString (" %1 ").arg (mSymbName));
764}
765
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette