From 3621cb10f796413ee12af0277ca1ac61f2f09522 Mon Sep 17 00:00:00 2001
From: LaurentGom <LaurentGom@4e206d99-4929-0410-ac5d-dfc041789085>
Date: Wed, 10 Feb 2010 23:35:01 +0000
Subject: [PATCH] Fixed bugs with the LShift and RShift keys

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1400 4e206d99-4929-0410-ac5d-dfc041789085
---
 src/SFML/Window/Win32/WindowImplWin32.cpp | 81 ++++++-----------------
 src/SFML/Window/Win32/WindowImplWin32.hpp | 11 ---
 2 files changed, 20 insertions(+), 72 deletions(-)

diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp
index f59f4021..526ad566 100644
--- a/src/SFML/Window/Win32/WindowImplWin32.cpp
+++ b/src/SFML/Window/Win32/WindowImplWin32.cpp
@@ -765,26 +765,15 @@ void WindowImplWin32::ProcessEvent(UINT Message, WPARAM WParam, LPARAM LParam)
         case WM_KEYDOWN :
         case WM_SYSKEYDOWN :
         {
-            if (myKeyRepeatEnabled || ((LParam & (1 << 30)) == 0))
+            if (myKeyRepeatEnabled || ((HIWORD(LParam) & KF_REPEAT) == 0))
             {
                 Event Evt;
                 Evt.Type        = Event::KeyPressed;
                 Evt.Key.Alt     = HIWORD(GetAsyncKeyState(VK_MENU))    != 0;
                 Evt.Key.Control = HIWORD(GetAsyncKeyState(VK_CONTROL)) != 0;
                 Evt.Key.Shift   = HIWORD(GetAsyncKeyState(VK_SHIFT))   != 0;
-
-                if (WParam != VK_SHIFT)
-                {
-                    Evt.Key.Code = VirtualKeyCodeToSF(WParam, LParam);
-                    SendEvent(Evt);
-                }
-                else
-                {
-                    // Special case for shift, its state can't be retrieved directly
-                    Evt.Key.Code = GetShiftState(true);
-                    if (Evt.Key.Code != 0)
-                        SendEvent(Evt);
-                }
+                Evt.Key.Code    = VirtualKeyCodeToSF(WParam, LParam);
+                SendEvent(Evt);
             }
             break;
         }
@@ -798,19 +787,8 @@ void WindowImplWin32::ProcessEvent(UINT Message, WPARAM WParam, LPARAM LParam)
             Evt.Key.Alt     = HIWORD(GetAsyncKeyState(VK_MENU))    != 0;
             Evt.Key.Control = HIWORD(GetAsyncKeyState(VK_CONTROL)) != 0;
             Evt.Key.Shift   = HIWORD(GetAsyncKeyState(VK_SHIFT))   != 0;
-
-            if (WParam != VK_SHIFT)
-            {
-                Evt.Key.Code = VirtualKeyCodeToSF(WParam, LParam);
-                SendEvent(Evt);
-            }
-            else
-            {
-                // Special case for shift, its state can't be retrieved directly
-                Evt.Key.Code = GetShiftState(false);
-                if (Evt.Key.Code != 0)
-                    SendEvent(Evt);
-            }
+            Evt.Key.Code    = VirtualKeyCodeToSF(WParam, LParam);
+            SendEvent(Evt);
 
             break;
         }
@@ -962,37 +940,6 @@ void WindowImplWin32::ProcessEvent(UINT Message, WPARAM WParam, LPARAM LParam)
 }
 
 
-////////////////////////////////////////////////////////////
-/// Check the state of the shift keys on a key event,
-/// and return the corresponding SF key code
-////////////////////////////////////////////////////////////
-Key::Code WindowImplWin32::GetShiftState(bool KeyDown)
-{
-    static bool LShiftPrevDown = false;
-    static bool RShiftPrevDown = false;
-
-    bool LShiftDown = (HIWORD(GetAsyncKeyState(VK_LSHIFT)) != 0);
-    bool RShiftDown = (HIWORD(GetAsyncKeyState(VK_RSHIFT)) != 0);
-
-    Key::Code Code = Key::Code(0);
-    if (KeyDown)
-    {
-        if      (!LShiftPrevDown && LShiftDown) Code = Key::LShift;
-        else if (!RShiftPrevDown && RShiftDown) Code = Key::RShift;
-    }
-    else
-    {
-        if      (LShiftPrevDown && !LShiftDown) Code = Key::LShift;
-        else if (RShiftPrevDown && !RShiftDown) Code = Key::RShift;
-    }
-
-    LShiftPrevDown = LShiftDown;
-    RShiftPrevDown = RShiftDown;
-
-    return Code;
-}
-
-
 ////////////////////////////////////////////////////////////
 /// Convert a Win32 virtual key code to a SFML key code
 ////////////////////////////////////////////////////////////
@@ -1000,9 +947,21 @@ Key::Code WindowImplWin32::VirtualKeyCodeToSF(WPARAM VirtualKey, LPARAM Flags)
 {
     switch (VirtualKey)
     {
-        // VK_SHIFT is handled by the GetShiftState function
-        case VK_MENU :       return (Flags & (1 << 24)) ? Key::RAlt     : Key::LAlt;
-        case VK_CONTROL :    return (Flags & (1 << 24)) ? Key::RControl : Key::LControl;
+        // Check the scancode to distinguish between left and right shift
+        case VK_SHIFT :
+        {
+            static UINT LShift = MapVirtualKey(VK_LSHIFT, MAPVK_VK_TO_VSC);
+            UINT scancode = (Flags & (0xFF << 16)) >> 16;
+            return scancode == LShift ? Key::LShift : Key::RShift;
+        }
+
+        // Check the "extended" flag to distinguish between left and right alt
+        case VK_MENU : return (HIWORD(Flags) & KF_EXTENDED) ? Key::RAlt : Key::LAlt;
+
+        // Check the "extended" flag to distinguish between left and right control
+        case VK_CONTROL : return (HIWORD(Flags) & KF_EXTENDED) ? Key::RControl : Key::LControl;
+
+        // Other keys are reported properly
         case VK_LWIN :       return Key::LSystem;
         case VK_RWIN :       return Key::RSystem;
         case VK_APPS :       return Key::Menu;
diff --git a/src/SFML/Window/Win32/WindowImplWin32.hpp b/src/SFML/Window/Win32/WindowImplWin32.hpp
index 75c1c59e..20557dcd 100644
--- a/src/SFML/Window/Win32/WindowImplWin32.hpp
+++ b/src/SFML/Window/Win32/WindowImplWin32.hpp
@@ -193,17 +193,6 @@ private :
     ////////////////////////////////////////////////////////////
     void ProcessEvent(UINT Message, WPARAM WParam, LPARAM LParam);
 
-    ////////////////////////////////////////////////////////////
-    /// Check the state of the shift keys on a key event,
-    /// and return the corresponding SF key code
-    ///
-    /// \param KeyDown : True for a keydown event, false for a keyup event
-    ///
-    /// \return SFML key code corresponding to the shift key
-    ///
-    ////////////////////////////////////////////////////////////
-    static Key::Code GetShiftState(bool KeyDown);
-
     ////////////////////////////////////////////////////////////
     /// Convert a Win32 virtual key code to a SFML key code
     ///