From 630f5f572f7a59c18e651407c73a7ac5bb45505c Mon Sep 17 00:00:00 2001 From: Jake Ginnivan Date: Mon, 29 Jul 2013 18:24:41 +0100 Subject: [PATCH] Detect swapped mouse buttons rather than being a configuration option --- src/TestStack.White.sln.DotSettings | 1 + .../Configuration/CoreAppXmlConfiguration.cs | 6 --- .../Configuration/CoreConfiguration.cs | 1 - src/TestStack.White/InputDevices/Mouse.cs | 39 ++++++++++--------- .../InputDevices/SystemMetric.cs | 11 ++++++ src/TestStack.White/TestStack.White.csproj | 1 + 6 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 src/TestStack.White/InputDevices/SystemMetric.cs diff --git a/src/TestStack.White.sln.DotSettings b/src/TestStack.White.sln.DotSettings index e3dd115d..5f3f3513 100644 --- a/src/TestStack.White.sln.DotSettings +++ b/src/TestStack.White.sln.DotSettings @@ -5,6 +5,7 @@ False NEXT_LINE NEXT_LINE + False False False NEXT_LINE diff --git a/src/TestStack.White/Configuration/CoreAppXmlConfiguration.cs b/src/TestStack.White/Configuration/CoreAppXmlConfiguration.cs index 0b18cb0c..d69d45d5 100644 --- a/src/TestStack.White/Configuration/CoreAppXmlConfiguration.cs +++ b/src/TestStack.White/Configuration/CoreAppXmlConfiguration.cs @@ -138,12 +138,6 @@ public virtual bool MoveMouseToGetStatusOfHourGlass set { SetUsedValue("MoveMouseToGetStatusOfHourGlass", value); } } - public virtual bool InvertMouseButtons - { - get { return Convert.ToBoolean(UsedValues["InvertMouseButtons"]); } - set { SetUsedValue("InvertMouseButtons", value); } - } - public virtual ILoggerFactory LoggerFactory { get; set; } public virtual IDisposable ApplyTemporarySetting(Action changes) diff --git a/src/TestStack.White/Configuration/CoreConfiguration.cs b/src/TestStack.White/Configuration/CoreConfiguration.cs index c2261991..17d99e11 100644 --- a/src/TestStack.White/Configuration/CoreConfiguration.cs +++ b/src/TestStack.White/Configuration/CoreConfiguration.cs @@ -32,7 +32,6 @@ public interface ICoreConfiguration bool RawElementBasedSearch { get; set; } int DoubleClickInterval { get; set; } bool MoveMouseToGetStatusOfHourGlass { get; set; } - bool InvertMouseButtons { get; set; } ILoggerFactory LoggerFactory { get; set; } IDisposable ApplyTemporarySetting(Action changes); } diff --git a/src/TestStack.White/InputDevices/Mouse.cs b/src/TestStack.White/InputDevices/Mouse.cs index d6613b83..23e650a4 100644 --- a/src/TestStack.White/InputDevices/Mouse.cs +++ b/src/TestStack.White/InputDevices/Mouse.cs @@ -15,36 +15,37 @@ namespace TestStack.White.InputDevices public class Mouse : IMouse { [DllImport("user32", EntryPoint = "SendInput")] - private static extern int SendInput(uint numberOfInputs, ref Input input, int structSize); + static extern int SendInput(uint numberOfInputs, ref Input input, int structSize); [DllImport("user32", EntryPoint = "SendInput")] - private static extern int SendInput64(int numberOfInputs, ref Input64 input, int structSize); - + static extern int SendInput64(int numberOfInputs, ref Input64 input, int structSize); + [DllImport("user32.dll")] - private static extern IntPtr GetMessageExtraInfo(); + static extern IntPtr GetMessageExtraInfo(); [DllImport("user32.dll")] - private static extern bool GetCursorPos(ref System.Drawing.Point cursorInfo); + static extern bool GetCursorPos(ref System.Drawing.Point cursorInfo); + + [DllImport("user32.dll")] + static extern bool SetCursorPos(int x, int y); [DllImport("user32.dll")] - private static extern bool SetCursorPos(int x, int y); + static extern bool GetCursorInfo(ref CursorInfo cursorInfo); [DllImport("user32.dll")] - private static extern bool GetCursorInfo(ref CursorInfo cursorInfo); + static extern short GetDoubleClickTime(); [DllImport("user32.dll")] - private static extern short GetDoubleClickTime(); + static extern int GetSystemMetrics(SystemMetric smIndex); public static Mouse Instance = new Mouse(); - private DateTime lastClickTime = DateTime.Now; - private readonly short doubleClickTime = GetDoubleClickTime(); - private Point lastClickLocation; - private const int ExtraMillisecondsBecauseOfBugInWindows = 13; + DateTime lastClickTime = DateTime.Now; + readonly short doubleClickTime = GetDoubleClickTime(); + Point lastClickLocation; + const int ExtraMillisecondsBecauseOfBugInWindows = 13; - private Mouse() - { - } + Mouse() { } public virtual Point Location { @@ -77,22 +78,22 @@ public virtual MouseCursor Cursor private static int RightMouseButtonDown { - get { return (CoreAppXmlConfiguration.Instance.InvertMouseButtons ? WindowsConstants.MOUSEEVENTF_RIGHTDOWN : WindowsConstants.MOUSEEVENTF_LEFTDOWN); } + get { return GetSystemMetrics(SystemMetric.SM_SWAPBUTTON) == 0 ? WindowsConstants.MOUSEEVENTF_RIGHTDOWN : WindowsConstants.MOUSEEVENTF_LEFTDOWN; } } private static int RightMouseButtonUp { - get { return (CoreAppXmlConfiguration.Instance.InvertMouseButtons ? WindowsConstants.MOUSEEVENTF_RIGHTUP : WindowsConstants.MOUSEEVENTF_LEFTUP); } + get { return GetSystemMetrics(SystemMetric.SM_SWAPBUTTON) == 0 ? WindowsConstants.MOUSEEVENTF_RIGHTUP : WindowsConstants.MOUSEEVENTF_LEFTUP; } } private static int LeftMouseButtonDown { - get { return (CoreAppXmlConfiguration.Instance.InvertMouseButtons ? WindowsConstants.MOUSEEVENTF_LEFTDOWN : WindowsConstants.MOUSEEVENTF_RIGHTDOWN); } + get { return GetSystemMetrics(SystemMetric.SM_SWAPBUTTON) == 0 ? WindowsConstants.MOUSEEVENTF_LEFTDOWN : WindowsConstants.MOUSEEVENTF_RIGHTDOWN; } } private static int LeftMouseButtonUp { - get { return (CoreAppXmlConfiguration.Instance.InvertMouseButtons ? WindowsConstants.MOUSEEVENTF_LEFTUP : WindowsConstants.MOUSEEVENTF_RIGHTUP); } + get { return GetSystemMetrics(SystemMetric.SM_SWAPBUTTON) == 0 ? WindowsConstants.MOUSEEVENTF_LEFTUP : WindowsConstants.MOUSEEVENTF_RIGHTUP; } } public virtual void RightClick() diff --git a/src/TestStack.White/InputDevices/SystemMetric.cs b/src/TestStack.White/InputDevices/SystemMetric.cs new file mode 100644 index 00000000..7ecb2409 --- /dev/null +++ b/src/TestStack.White/InputDevices/SystemMetric.cs @@ -0,0 +1,11 @@ +namespace TestStack.White.InputDevices +{ + // ReSharper disable InconsistentNaming + public enum SystemMetric + { + /// + /// Nonzero if the meanings of the left and right mouse buttons are swapped; otherwise, 0. + /// + SM_SWAPBUTTON = 23 + } +} \ No newline at end of file diff --git a/src/TestStack.White/TestStack.White.csproj b/src/TestStack.White/TestStack.White.csproj index 7ff87c01..05976354 100644 --- a/src/TestStack.White/TestStack.White.csproj +++ b/src/TestStack.White/TestStack.White.csproj @@ -93,6 +93,7 @@ +