From ac5c2aaf35bba3048aeff5c26cf0aa704fe63df6 Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Date: Mon, 14 Dec 2015 12:02:29 +0100
Subject: [PATCH] Windows: Clear key recorder when application becomes
 inactive.

When keys are pressed and the application becomes inactive before
they are released, the stored state becomes inconsistent.

Task-number: QTBUG-49930
Change-Id: Ide86b1d9052df060f30f7c02b81a4f2ae15d28e7
Reviewed-by: Oliver Wolff <oliver.wolff@theqtcompany.com>
---
 src/plugins/platforms/windows/qwindowskeymapper.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/plugins/platforms/windows/qwindowskeymapper.cpp b/src/plugins/platforms/windows/qwindowskeymapper.cpp
index 4c0b94e6e7a..c5dff601143 100644
--- a/src/plugins/platforms/windows/qwindowskeymapper.cpp
+++ b/src/plugins/platforms/windows/qwindowskeymapper.cpp
@@ -86,10 +86,15 @@ QT_BEGIN_NAMESPACE
     The code originates from \c qkeymapper_win.cpp.
 */
 
+static void clearKeyRecorderOnApplicationInActive(Qt::ApplicationState state);
+
 QWindowsKeyMapper::QWindowsKeyMapper()
     : m_useRTLExtensions(false), m_keyGrabber(0)
 {
     memset(keyLayout, 0, sizeof(keyLayout));
+    QGuiApplication *app = static_cast<QGuiApplication *>(QGuiApplication::instance());
+    QObject::connect(app, &QGuiApplication::applicationStateChanged,
+                     app, clearKeyRecorderOnApplicationInActive);
 }
 
 QWindowsKeyMapper::~QWindowsKeyMapper()
@@ -144,6 +149,12 @@ struct KeyRecorder
 };
 static KeyRecorder key_recorder;
 
+static void clearKeyRecorderOnApplicationInActive(Qt::ApplicationState state)
+{
+    if (state == Qt::ApplicationInactive)
+        key_recorder.clearKeys();
+}
+
 KeyRecord *KeyRecorder::findKey(int code, bool remove)
 {
     KeyRecord *result = 0;
-- 
GitLab