From 6ef2ab6c2318d4fb15220891ce16768e84354a88 Mon Sep 17 00:00:00 2001
From: Sebastian Lackner <sebastian@fds-team.de>
Date: Tue, 16 Jun 2015 02:56:25 +0200
Subject: user32: Globally invalidate key state on changes in other threads.

---
 dlls/user32/input.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/dlls/user32/input.c b/dlls/user32/input.c
index 40e35a9..95f3c2f 100644
--- a/dlls/user32/input.c
+++ b/dlls/user32/input.c
@@ -371,6 +371,7 @@ SHORT WINAPI DECLSPEC_HOTPATCH GetAsyncKeyState( INT key )
 {
     struct user_key_state_info *key_state_info = get_user_thread_info()->key_state;
     INT counter = global_key_state_counter;
+    BYTE prev_key_state;
     SHORT ret;
 
     if (key < 0 || key >= 256) return 0;
@@ -398,14 +399,23 @@ SHORT WINAPI DECLSPEC_HOTPATCH GetAsyncKeyState( INT key )
         {
             req->tid = 0;
             req->key = key;
-            if (key_state_info) wine_server_set_reply( req, key_state_info->state,
-                                                       sizeof(key_state_info->state) );
+            if (key_state_info)
+            {
+                prev_key_state = key_state_info->state[key];
+                wine_server_set_reply( req, key_state_info->state, sizeof(key_state_info->state) );
+            }
             if (!wine_server_call( req ))
             {
                 if (reply->state & 0x40) ret |= 0x0001;
                 if (reply->state & 0x80) ret |= 0x8000;
                 if (key_state_info)
                 {
+                    /* force refreshing the key state cache - some multithreaded programs
+                     * (like Adobe Photoshop CS5) expect that changes to the async key state
+                     * are also immediately available in other threads. */
+                    if (prev_key_state != key_state_info->state[key])
+                        counter = interlocked_xchg_add( &global_key_state_counter, 1 ) + 1;
+
                     key_state_info->time    = GetTickCount();
                     key_state_info->counter = counter;
                 }
-- 
2.4.3

