VirtualBox

Ignore:
Timestamp:
Jan 14, 2016 1:56:30 PM (9 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
105028
Message:

FE/Qt: Qt5 migration (part 54): Reworking keyboard-handler: Refactoring.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.cpp

    r59345 r59347  
    138138
    139139/* Prepare listened objects: */
    140 void UIKeyboardHandler::prepareListener(ulong uIndex, UIMachineWindow *pMachineWindow)
     140void UIKeyboardHandler::prepareListener(ulong uScreenId, UIMachineWindow *pMachineWindow)
    141141{
    142142    /* If that window is NOT registered yet: */
    143     if (!m_windows.contains(uIndex))
     143    if (!m_windows.contains(uScreenId))
    144144    {
    145145        /* Add window: */
    146         m_windows.insert(uIndex, pMachineWindow);
     146        m_windows.insert(uScreenId, pMachineWindow);
    147147        /* Install event-filter for window: */
    148         m_windows[uIndex]->installEventFilter(this);
     148        m_windows[uScreenId]->installEventFilter(this);
    149149    }
    150150
    151151    /* If that view is NOT registered yet: */
    152     if (!m_views.contains(uIndex))
     152    if (!m_views.contains(uScreenId))
    153153    {
    154154        /* Add view: */
    155         m_views.insert(uIndex, pMachineWindow->machineView());
     155        m_views.insert(uScreenId, pMachineWindow->machineView());
    156156        /* Install event-filter for view: */
    157         m_views[uIndex]->installEventFilter(this);
     157        m_views[uScreenId]->installEventFilter(this);
    158158    }
    159159}
    160160
    161161/* Cleanup listened objects: */
    162 void UIKeyboardHandler::cleanupListener(ulong uIndex)
     162void UIKeyboardHandler::cleanupListener(ulong uScreenId)
    163163{
    164164    /* Check if we should release keyboard first: */
    165     if ((int)uIndex == m_iKeyboardCaptureViewIndex)
     165    if ((int)uScreenId == m_iKeyboardCaptureViewIndex)
    166166        releaseKeyboard();
    167167
    168168    /* If window still registered: */
    169     if (m_windows.contains(uIndex))
     169    if (m_windows.contains(uScreenId))
    170170    {
    171171        /* Remove window: */
    172         m_windows.remove(uIndex);
     172        m_windows.remove(uScreenId);
    173173    }
    174174
    175175    /* If view still registered: */
    176     if (m_views.contains(uIndex))
     176    if (m_views.contains(uScreenId))
    177177    {
    178178        /* Remove view: */
    179         m_views.remove(uIndex);
     179        m_views.remove(uScreenId);
    180180    }
    181181}
     
    406406        hostComboModifierMask |= ::DarwinKeyCodeToDarwinModifierMask(hostCombo.at(i));
    407407    /* Clear most of the modifiers: */
    408     m_darwinKeyModifiers &=
     408    m_uDarwinKeyModifiers &=
    409409        alphaLock | kEventKeyModifierNumLockMask |
    410410        (aReleaseHostKey ? 0 : hostComboModifierMask);
     
    889889    , m_fDebuggerActive(false)
    890890#if defined(Q_WS_MAC)
    891     , m_darwinKeyModifiers(0)
    892     , m_fKeyboardGrabbed(false)
    893     , m_iKeyboardGrabViewIndex(-1)
     891    , m_iKeyboardHookViewIndex(-1)
     892    , m_uDarwinKeyModifiers(0)
    894893#elif defined(Q_WS_WIN)
    895     , m_bIsHostkeyInCapture(false)
    896894    , m_iKeyboardHookViewIndex(-1)
     895    , m_fIsHostkeyInCapture(false)
    897896    , m_fSkipKeyboardEvents(false)
     897    , m_keyboardHook(NULL)
    898898    , m_pAltGrMonitor(0)
    899899#endif /* Q_WS_WIN */
     
    964964    /* We have to make sure the callback for the keyboard events
    965965     * is released when closing this view. */
    966     if (m_fKeyboardGrabbed)
     966    if (m_iKeyboardHookViewIndex != -1)
    967967        darwinGrabKeyboardEvents(false);
    968968#endif /* Q_WS_MAC */
     
    10221022                    }
    10231023                    /* Register new keyboard-hook: */
    1024                     m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, lowLevelKeyboardProc, GetModuleHandle(NULL), 0);
     1024                    m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, UIKeyboardHandler::winKeyboardProc, GetModuleHandle(NULL), 0);
    10251025                    AssertMsg(m_keyboardHook, ("SetWindowsHookEx(): err=%d", GetLastError()));
    10261026                    /* Remember which view had captured keyboard: */
     
    10641064                /* If keyboard-event handler is NOT currently installed;
    10651065                 * Or installed but NOT for that view: */
    1066                 if (m_iKeyboardGrabViewIndex != (int)uScreenId)
     1066                if (m_iKeyboardHookViewIndex != (int)uScreenId)
    10671067                {
    10681068                    /* If keyboard-event handler is NOT currently installed: */
    1069                     if (m_iKeyboardGrabViewIndex == -1)
     1069                    if (m_iKeyboardHookViewIndex == -1)
    10701070                    {
    10711071                        /* Install the keyboard-event handler: */
     
    10731073                    }
    10741074                    /* Update the id: */
    1075                     m_iKeyboardGrabViewIndex = uScreenId;
     1075                    m_iKeyboardHookViewIndex = uScreenId;
    10761076                }
    10771077#endif /* Q_WS_MAC */
     
    10971097#ifdef Q_WS_MAC
    10981098                /* If keyboard-event handler is installed for that view: */
    1099                 if (m_iKeyboardGrabViewIndex == (int)uScreenId)
     1099                if (m_iKeyboardHookViewIndex == (int)uScreenId)
    11001100                {
    11011101                    /* Remove the keyboard-event handler: */
    11021102                    darwinGrabKeyboardEvents(false);
    11031103                    /* Update the id: */
    1104                     m_iKeyboardGrabViewIndex = -1;
     1104                    m_iKeyboardHookViewIndex = -1;
    11051105                }
    11061106#endif /* Q_WS_MAC */
     
    11651165void UIKeyboardHandler::darwinGrabKeyboardEvents(bool fGrab)
    11661166{
    1167     m_fKeyboardGrabbed = fGrab;
    11681167    if (fGrab)
    11691168    {
     
    11741173        /* Bring the caps lock state up to date, otherwise e.g. a later Shift
    11751174         * key press will accidentally inject a CapsLock key press and release,
    1176          * see UIKeyboardHandler::darwinKeyboardEvent for the code handling
     1175         * see UIKeyboardHandler::macKeyboardEvent for the code handling
    11771176         * modifier key state changes */
    1178         m_darwinKeyModifiers ^= (m_darwinKeyModifiers ^ ::GetCurrentEventKeyModifiers()) & alphaLock;
     1177        m_uDarwinKeyModifiers ^= (m_uDarwinKeyModifiers ^ ::GetCurrentEventKeyModifiers()) & alphaLock;
    11791178
    11801179        /* Register the event callback/hook and grab the keyboard. */
    11811180        UICocoaApplication::instance()->registerForNativeEvents(RT_BIT_32(10) | RT_BIT_32(11) | RT_BIT_32(12) /* NSKeyDown  | NSKeyUp | | NSFlagsChanged */,
    1182                                                                 UIKeyboardHandler::darwinEventHandlerProc, this);
     1181                                                                UIKeyboardHandler::macKeyboardProc, this);
    11831182
    11841183        ::DarwinGrabKeyboard (false);
     
    11881187        ::DarwinReleaseKeyboard();
    11891188        UICocoaApplication::instance()->unregisterForNativeEvents(RT_BIT_32(10) | RT_BIT_32(11) | RT_BIT_32(12) /* NSKeyDown  | NSKeyUp | | NSFlagsChanged */,
    1190                                                                   UIKeyboardHandler::darwinEventHandlerProc, this);
    1191     }
    1192 }
    1193 
    1194 bool UIKeyboardHandler::darwinEventHandlerProc(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser)
     1189                                                                  UIKeyboardHandler::macKeyboardProc, this);
     1190    }
     1191}
     1192
     1193bool UIKeyboardHandler::macKeyboardProc(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser)
    11951194{
    11961195    UIKeyboardHandler *pKeyboardHandler = (UIKeyboardHandler*)pvUser;
     
    12061205    if (eventClass == kEventClassKeyboard)
    12071206    {
    1208         if (pKeyboardHandler->darwinKeyboardEvent (pvCocoaEvent, inEvent))
     1207        if (pKeyboardHandler->macKeyboardEvent(pvCocoaEvent, inEvent))
    12091208            return true;
    12101209    }
     
    12131212}
    12141213
    1215 bool UIKeyboardHandler::darwinKeyboardEvent(const void *pvCocoaEvent, EventRef inEvent)
     1214bool UIKeyboardHandler::macKeyboardEvent(const void *pvCocoaEvent, EventRef inEvent)
    12161215{
    12171216    bool ret = false;
     
    12561255            ucs[cbWritten / sizeof(wchar_t)] = 0; /* The api doesn't terminate it. */
    12571256
    1258             ret = keyEvent(keyCode, scanCode, flags, m_iKeyboardGrabViewIndex, ucs[0] ? ucs : NULL);
     1257            ret = keyEvent(keyCode, scanCode, flags, m_iKeyboardHookViewIndex, ucs[0] ? ucs : NULL);
    12591258        }
    12601259    }
     
    12661265                            sizeof(newMask), NULL, &newMask);
    12671266        newMask = ::DarwinAdjustModifierMask(newMask, pvCocoaEvent);
    1268         UInt32 changed = newMask ^ m_darwinKeyModifiers;
     1267        UInt32 changed = newMask ^ m_uDarwinKeyModifiers;
    12691268        if (changed)
    12701269        {
     
    12851284                        flags |= KeyExtended;
    12861285                    scanCode &= VBOXKEY_SCANCODE_MASK;
    1287                     ret |= keyEvent(keyCode, scanCode & 0xff, flags, m_iKeyboardGrabViewIndex);
     1286                    ret |= keyEvent(keyCode, scanCode & 0xff, flags, m_iKeyboardHookViewIndex);
    12881287                }
    12891288                else
     
    12931292                        flags |= KeyExtended;
    12941293                    scanCode &= VBOXKEY_SCANCODE_MASK;
    1295                     keyEvent(keyCode, scanCode, flags | KeyPressed, m_iKeyboardGrabViewIndex);
    1296                     keyEvent(keyCode, scanCode, flags, m_iKeyboardGrabViewIndex);
     1294                    keyEvent(keyCode, scanCode, flags | KeyPressed, m_iKeyboardHookViewIndex);
     1295                    keyEvent(keyCode, scanCode, flags, m_iKeyboardHookViewIndex);
    12971296                }
    12981297            }
    12991298        }
    13001299
    1301         m_darwinKeyModifiers = newMask;
     1300        m_uDarwinKeyModifiers = newMask;
    13021301
    13031302        /* Always return true here because we'll otherwise getting a Qt event
     
    13111310#elif defined(Q_WS_WIN)
    13121311
    1313 LRESULT CALLBACK UIKeyboardHandler::lowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
    1314 {
    1315     if (nCode == HC_ACTION && m_spKeyboardHandler && m_spKeyboardHandler->winLowKeyboardEvent(wParam, *(KBDLLHOOKSTRUCT*)lParam))
     1312LRESULT CALLBACK UIKeyboardHandler::winKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
     1313{
     1314    if (nCode == HC_ACTION && m_spKeyboardHandler && m_spKeyboardHandler->winKeyboardEvent(wParam, *(KBDLLHOOKSTRUCT*)lParam))
    13161315        return 1;
    13171316
     
    13191318}
    13201319
    1321 bool UIKeyboardHandler::winLowKeyboardEvent(UINT msg, const KBDLLHOOKSTRUCT &event)
     1320bool UIKeyboardHandler::winKeyboardEvent(UINT msg, const KBDLLHOOKSTRUCT &event)
    13221321{
    13231322    /* Check what related machine-view was NOT unregistered yet: */
     
    13331332    if (   (event.flags & 0x80) /* released */
    13341333        && (   (   UIHostCombo::toKeyCodeList(m_globalSettings.hostCombo()).contains(event.vkCode)
    1335                 && !m_bIsHostkeyInCapture)
     1334                && !m_fIsHostkeyInCapture)
    13361335            ||    (  m_pressedKeys[event.scanCode & 0x7F]
    13371336                   & (IsKbdCaptured | what_pressed))
     
    16011600    if (m_bIsHostComboPressed || isHostComboStateChanged)
    16021601    {
    1603         /* Currently this is used in winLowKeyboardEvent() only: */
    1604         m_bIsHostkeyInCapture = m_fIsKeyboardCaptured;
     1602        /* Currently this is used in winKeyboardEvent() only: */
     1603        m_fIsHostkeyInCapture = m_fIsKeyboardCaptured;
    16051604    }
    16061605#endif /* Q_WS_WIN */
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.h

    r59346 r59347  
    7171
    7272    /* Prepare/cleanup listeners: */
    73     void prepareListener(ulong uIndex, UIMachineWindow *pMachineWindow);
    74     void cleanupListener(ulong uIndex);
     73    void prepareListener(ulong uScreenId, UIMachineWindow *pMachineWindow);
     74    void cleanupListener(ulong uScreenId);
    7575
    7676    /* Commands to capture/release keyboard: */
     
    9191#ifdef Q_WS_MAC
    9292    bool isHostKeyAlone() const { return m_bIsHostComboAlone; }
    93     bool isKeyboardGrabbed() const { return m_fKeyboardGrabbed; }
     93    bool isKeyboardGrabbed() const { return m_iKeyboardHookViewIndex != -1; }
    9494#endif /* Q_WS_MAC */
    9595
     
    153153    void darwinGrabKeyboardEvents(bool fGrab);
    154154    /** Mac: Performs initial pre-processing of all the native keyboard events. */
    155     static bool darwinEventHandlerProc(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
     155    static bool macKeyboardProc(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
    156156    /** Mac: Performs initial pre-processing of all the native keyboard events. */
    157     bool darwinKeyboardEvent(const void *pvCocoaEvent, EventRef inEvent);
     157    bool macKeyboardEvent(const void *pvCocoaEvent, EventRef inEvent);
    158158#elif defined(Q_WS_WIN)
    159159    /** Win: Performs initial pre-processing of all the native keyboard events. */
    160     static LRESULT CALLBACK lowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
     160    static LRESULT CALLBACK winKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
    161161    /** Win: Performs initial pre-processing of all the native keyboard events. */
    162     bool winLowKeyboardEvent(UINT msg, const KBDLLHOOKSTRUCT &event);
     162    bool winKeyboardEvent(UINT msg, const KBDLLHOOKSTRUCT &event);
    163163#endif /* Q_WS_WIN */
    164164
     
    212212
    213213#if defined(Q_WS_MAC)
     214    /** Holds the keyboard hook view index. */
     215    int m_iKeyboardHookViewIndex;
    214216    /** Holds the current modifiers key mask. */
    215     UInt32 m_darwinKeyModifiers;
    216     /** Holds whether the keyboard is grabbed. */
    217     bool m_fKeyboardGrabbed;
     217    UInt32 m_uDarwinKeyModifiers;
     218#elif defined(Q_WS_WIN)
    218219    /** Holds the keyboard hook view index. */
    219     int m_iKeyboardGrabViewIndex;
    220 #elif defined(Q_WS_WIN)
    221     /* Currently this is used in winLowKeyboardEvent() only: */
    222     bool m_bIsHostkeyInCapture;
     220    int m_iKeyboardHookViewIndex;
     221    /* Currently this is used in winKeyboardEvent() only: */
     222    bool m_fIsHostkeyInCapture;
     223    /** Holds whether the keyboard event filter should ignore keyboard events. */
     224    bool m_fSkipKeyboardEvents;
     225    /** Holds the keyboard hook instance. */
     226    HHOOK m_keyboardHook;
     227    /** Holds the object monitoring key event stream for problematic AltGr events. */
     228    WinAltGrMonitor *m_pAltGrMonitor;
    223229    /** Holds the keyboard handler reference to be accessible from the keyboard hook. */
    224230    static UIKeyboardHandler *m_spKeyboardHandler;
    225     /** Holds the keyboard hook instance. */
    226     HHOOK m_keyboardHook;
    227     /** Holds the keyboard hook view index. */
    228     int m_iKeyboardHookViewIndex;
    229     /** Holds whether the keyboard event filter should ignore keyboard events. */
    230     bool m_fSkipKeyboardEvents;
    231     /** Holds the object monitoring key event stream for problematic AltGr events. */
    232     WinAltGrMonitor *m_pAltGrMonitor;
    233231#endif /* Q_WS_WIN */
    234232
Note: See TracChangeset for help on using the changeset viewer.

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