Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.cpp	(revision 63598)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.cpp	(revision 63599)
@@ -39,5 +39,8 @@
 # ifdef VBOX_WS_X11
 #  include <QX11Info>
-# endif /* VBOX_WS_X11 */
+#  if QT_VERSION >= 0x050000
+#   include <QTimer>
+#  endif
+# endif
 
 /* GUI includes: */
@@ -255,5 +258,5 @@
     }
 
-    /* If such view exists: */
+    /* If the view exists: */
     if (m_views.contains(uScreenId))
     {
@@ -326,5 +329,5 @@
 
         /* On X11, we do not grab the keyboard as soon as it is captured, but delay it
-         * until the first keypress after the capture. We do this for several reasons:
+         * for 300 milliseconds after the formal capture. We do this for several reasons:
          * - First, when several windows are created they all try to capture the keyboard when
          *   they get the focus. Due to the asynchronous nature of X11 the first window may only
@@ -335,4 +338,7 @@
          *   by refusing to e.g. drag a window using its title bar. */
 
+        /* Delay finalising capture for 300 milliseconds: */
+        QTimer::singleShot(300, this, SLOT(sltFinaliseCaptureKeyboard()));
+
 # endif /* QT_VERSION >= 0x050000 */
 #else
@@ -343,9 +349,9 @@
 #endif
 
-        /* Remember which screen wish to capture the keyboard: */
+        /* Remember which screen wishes to capture the keyboard: */
         m_iKeyboardCaptureViewIndex = uScreenId;
 
 #if !defined(VBOX_WS_X11) || QT_VERSION < 0x050000
-        /* Finalising keyboard capture: */
+        /* Finalise keyboard capture: */
         finaliseCaptureKeyboard();
 #endif
@@ -353,15 +359,15 @@
 }
 
-void UIKeyboardHandler::finaliseCaptureKeyboard()
+bool UIKeyboardHandler::finaliseCaptureKeyboard()
 {
     /* Do NOT capture the keyboard if it is already captured: */
     if (m_fIsKeyboardCaptured)
-        return;
+        return true;
 
     /* Make sure capture was really requested: */
     if (m_iKeyboardCaptureViewIndex == -1)
-        return;
-
-    /* If such view exists: */
+        return true;
+
+    /* If the view exists: */
     if (m_views.contains(m_iKeyboardCaptureViewIndex))
     {
@@ -386,5 +392,5 @@
             /* Try again later: */
             free(pGrabReply);
-            return;
+            return false;
         }
         free(pGrabReply);
@@ -397,5 +403,9 @@
         /* Notify all the listeners: */
         emit sigStateChange(state());
-    }
+
+        return true;
+    }
+
+    return false;
 }
 
@@ -410,5 +420,5 @@
     }
 
-    /* If such view exists: */
+    /* If the view exists: */
     if (m_views.contains(m_iKeyboardCaptureViewIndex))
     {
@@ -1356,8 +1366,4 @@
         case XCB_KEY_RELEASE:
         {
-            /* If we were asked to grab the keyboard previously but had to delay it
-             * then try again on every key press and release event until we manage: */
-            finaliseCaptureKeyboard();
-
             /* Cast to XCB key-event: */
             xcb_key_press_event_t *pKeyEvent = static_cast<xcb_key_press_event_t*>(pMessage);
@@ -1539,4 +1545,16 @@
         popupCenter().forgetAboutPausedVMInput(machineLogic()->activeMachineWindow());
 }
+
+#if defined(VBOX_WS_X11) && QT_VERSION >= 0x050000
+void UIKeyboardHandler::sltFinaliseCaptureKeyboard()
+{
+    /* Try to finalise keyboard capture: */
+    if (!finaliseCaptureKeyboard())
+    {
+        /* Try again in another 300 milliseconds in case of failure: */
+        QTimer::singleShot(300, this, SLOT(sltFinaliseCaptureKeyboard()));
+    }
+}
+#endif
 
 /* Keyboard-handler constructor: */
@@ -2102,5 +2120,4 @@
                     {
                         captureKeyboard(uScreenId);
-                        finaliseCaptureKeyboard();
 #ifdef VBOX_WS_X11
                         /* Make sure that pending FocusOut events from the
@@ -2110,4 +2127,5 @@
                         qApp->processEvents();
 #endif /* VBOX_WS_X11 */
+                        finaliseCaptureKeyboard();
                         if (fCaptureMouse)
                         {
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.h	(revision 63598)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.h	(revision 63599)
@@ -83,6 +83,6 @@
     /** Captures the keyboard for @a uScreenId. */
     void captureKeyboard(ulong uScreenId);
-    /** Finalises keyboard capturing. */
-    void finaliseCaptureKeyboard();
+    /** Finalises keyboard capture. */
+    bool finaliseCaptureKeyboard();
     /** Releases the keyboard. */
     void releaseKeyboard();
@@ -133,4 +133,9 @@
     virtual void sltMachineStateChanged();
 
+#if defined(VBOX_WS_X11) && QT_VERSION >= 0x050000
+    /** Finalises keyboard capture. */
+    void sltFinaliseCaptureKeyboard();
+#endif
+
 protected:
 
