Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.cpp	(revision 61944)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.cpp	(revision 61945)
@@ -315,5 +315,5 @@
          * S.a. UIKeyboardHandler::eventFilter for more information. */
 
-#elif defined(VBOX_WS_X11)
+#elif defined(VBOX_WS_X11) && QT_VERSION < 0x050000
 
         /* On X11, we are using passive XGrabKey for normal (windowed) mode
@@ -328,9 +328,5 @@
             case UIVisualStateType_Scale:
             {
-# if QT_VERSION >= 0x050000
-                xcb_grab_key_checked(QX11Info::connection(), 0, m_windows.value(uScreenId)->winId(), XCB_MOD_MASK_ANY, XCB_GRAB_ANY, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
-# else /* QT_VERSION < 0x050000 */
                 XGrabKey(QX11Info::display(), AnyKey, AnyModifier, m_windows[uScreenId]->winId(), False, GrabModeAsync, GrabModeAsync);
-# endif /* QT_VERSION < 0x050000 */
                 break;
             }
@@ -339,7 +335,4 @@
             case UIVisualStateType_Seamless:
             {
-# if QT_VERSION >= 0x050000
-                xcb_grab_keyboard(QX11Info::connection(), 0, m_windows.value(uScreenId)->winId(), XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
-# else /* QT_VERSION < 0x050000 */
                 /* Keyboard grabbing can fail because of some keyboard shortcut is still grabbed by window manager.
                  * We can't be sure this shortcut will be released at all, so we will retry to grab keyboard for 50 times,
@@ -357,5 +350,4 @@
                            CurrentTime))
                         --cTriesLeft;
-# endif /* QT_VERSION < 0x050000 */
                 break;
             }
@@ -369,4 +361,10 @@
         /* On other platforms we are just praying Qt method to work: */
         m_views[uScreenId]->grabKeyboard();
+#if defined(VBOX_WS_X11) && QT_VERSION >= 0x050000
+        /* Mouse capture hack for when the keyboard is captured.  We do not
+         * check for failure as we do not currently implement a back-up plan. */
+        if (!uisession()->isMouseCaptured())
+            xcb_grab_button_checked(QX11Info::connection(), 0, QX11Info::appRootWindow(), XCB_EVENT_MASK_BUTTON_PRESS, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, XCB_BUTTON_INDEX_1, XCB_MOD_MASK_ANY);
+#endif /* defined(VBOX_WS_X11) && QT_VERSION >= 0x050000 */
 
 #endif
@@ -414,5 +412,5 @@
          * S.a. UIKeyboardHandler::eventFilter for more information. */
 
-#elif defined(VBOX_WS_X11)
+#elif defined(VBOX_WS_X11) && QT_VERSION < 0x050000
 
         /* On X11, we are using passive XGrabKey for normal (windowed) mode
@@ -427,9 +425,5 @@
             case UIVisualStateType_Scale:
             {
-# if QT_VERSION >= 0x050000
-                xcb_ungrab_key(QX11Info::connection(), XCB_GRAB_ANY, m_windows.value(m_iKeyboardCaptureViewIndex)->winId(), XCB_MOD_MASK_ANY);
-# else /* QT_VERSION < 0x050000 */
                 XUngrabKey(QX11Info::display(), AnyKey, AnyModifier, m_windows[m_iKeyboardCaptureViewIndex]->winId());
-# endif /* QT_VERSION < 0x050000 */
                 break;
             }
@@ -438,9 +432,5 @@
             case UIVisualStateType_Seamless:
             {
-# if QT_VERSION >= 0x050000
-                xcb_ungrab_keyboard(QX11Info::connection(), CurrentTime);
-# else /* QT_VERSION < 0x050000 */
                 XUngrabKeyboard(QX11Info::display(), CurrentTime);
-# endif /* QT_VERSION < 0x050000 */
                 break;
             }
@@ -454,4 +444,9 @@
         /* On other platforms we are just praying Qt method to work: */
         m_views[m_iKeyboardCaptureViewIndex]->releaseKeyboard();
+#if defined(VBOX_WS_X11) && QT_VERSION >= 0x050000
+        /* Mouse capture hack for when the keyboard is captured. */
+        if (!uisession()->isMouseCaptured())
+            xcb_ungrab_button_checked(QX11Info::connection(), XCB_BUTTON_INDEX_1, QX11Info::appRootWindow(), XCB_MOD_MASK_ANY);
+#endif /* defined(VBOX_WS_X11) && QT_VERSION >= 0x050000 */
 
 #endif
@@ -1386,4 +1381,30 @@
             fResult = keyEvent(ks, uScan, iflags, uScreenId);
 
+            break;
+        }
+        /* If we see a mouse press outside of our views while the mouse is not
+         * captured, release the keyboard before letting the event owner see it.
+         * This is because some owners cannot deal with failures to grab the
+         * keyboard themselves (e.g. window managers dragging windows).  Only
+         * works if we have passively grabbed the mouse button. */
+        case XCB_BUTTON_PRESS:
+        {
+            /* Cast to XCB key-event: */
+            xcb_button_press_event_t *pButtonEvent = static_cast<xcb_button_press_event_t*>(pMessage);
+            QWidget *pWidget = qApp->widgetAt(pButtonEvent->root_x, pButtonEvent->root_y);
+
+            if (uisession()->isMouseCaptured())
+                break;
+            if (pWidget)
+            {
+                QPoint pos = pWidget->mapFromGlobal(QPoint(pButtonEvent->root_x, pButtonEvent->root_y));
+                pButtonEvent->event = pWidget->effectiveWinId();
+                pButtonEvent->event_x = pos.x();
+                pButtonEvent->event_y = pos.y();
+                xcb_allow_events_checked(QX11Info::connection(), XCB_ALLOW_REPLAY_POINTER, pButtonEvent->time);
+                break;
+            }
+            m_views[m_iKeyboardCaptureViewIndex]->releaseKeyboard();
+            xcb_allow_events_checked(QX11Info::connection(), XCB_ALLOW_REPLAY_POINTER, pButtonEvent->time);
             break;
         }
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp	(revision 61944)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp	(revision 61945)
@@ -1909,6 +1909,8 @@
         case XCB_KEY_PRESS:
         case XCB_KEY_RELEASE:
-        {
-            /* Delegate key-event handling to the keyboard-handler: */
+        case XCB_BUTTON_PRESS:
+        {
+            /* Delegate key-event handling to the keyboard-handler and let it
+             * filter out button presses out of the view windows: */
             return machineLogic()->keyboardHandler()->nativeEventPostprocessor(pMessage, screenId());
         }
