/// <summary> /// Clicks a mouse button at the specified coordinates. It can also hold down a mouse button, turn the mouse wheel, or move the mouse. /// </summary> /// <param name="options">Can be one or more of the following options: /// <list type="bullet"> /// <item><term><c>X</c> or <c>Y</c></term>: <description>the coordinates;</description></item> /// <item><term>Button</term>: <description><c>Left</c> (default), <c>Middle</c>, <c>Right</c>, <c>X1</c> or <c>X2</c>;</description></item> /// <item><term>Wheel</term>: <description><c>WheelUp</c>, <c>WheelDown</c>, <c>WheelLeft</c> or <c>WheelRight</c>;</description></item> /// <item><term>Count</term>: <description>number of times to send the click;</description></item> /// <item><term><c>Down</c> or <c>Up</c></term>: <description>if omitted send a down click followed by an up release;</description></item> /// <item><term><c>Relative</c></term>: <description>treat the coordinates as relative offsets from the current mouse position.</description></item> /// </list> /// </param> public static void Click(object[] options) { string ParamLine = string.Empty; var MousePos = new Point(0, 0); int ClickCount = 1; const string delimiter = ","; var RE_Coord = new Regex(@"(\d*?)\s*,\s*(\d*)[\s,\,]*", RegexOptions.IgnoreCase); var RE_Num = new Regex(@"\d+", RegexOptions.IgnoreCase); CaptureCollection Out; Match Match; //rebuild Argument string, as we have to parse this in a special way foreach (var option in options) { if (option is string) ParamLine += (string)option + delimiter; else if (option is double) ParamLine += ((int)(double)option) + delimiter; } ParamLine = ParamLine.ToLower().Substring(0, ParamLine.Length - 1); //search coordinates, move mouse, remove them if (RE_Coord.IsMatch(ParamLine)) { Match = RE_Coord.Match(ParamLine); MousePos.X = Convert.ToInt32(Match.Groups[1].Value); MousePos.Y = Convert.ToInt32(Match.Groups[2].Value); ParamLine = RE_Coord.Replace(ParamLine, string.Empty); //remove coord if (coords.Mouse == CoordModeType.Relative) { WindowsAPI.RECT rect; WindowsAPI.GetWindowRect(WindowsAPI.GetForegroundWindow(), out rect); MousePos.X += rect.Left; MousePos.Y += rect.Top; } Cursor.Position = MousePos; } //click count if (RE_Num.IsMatch(ParamLine)) { Out = RE_Num.Match(ParamLine).Captures; ClickCount = Convert.ToInt32(Out[0].Value); if (ClickCount <= 0) return; } if (Environment.OSVersion.Platform == PlatformID.Win32NT) { var aInput = new WindowsAPI.INPUT[2]; //right or left mouse if (ParamLine.Contains(Keyword_Right)) { aInput[0].i.m.dwFlags = (uint)WindowsAPI.MOUSEEVENTF.RIGHTDOWN; aInput[1].i.m.dwFlags = (uint)WindowsAPI.MOUSEEVENTF.RIGHTUP; } else { aInput[0].i.m.dwFlags = (uint)WindowsAPI.MOUSEEVENTF.LEFTDOWN; aInput[1].i.m.dwFlags = (uint)WindowsAPI.MOUSEEVENTF.LEFTUP; } //down event aInput[0].type = WindowsAPI.INPUT_MOUSE; aInput[0].i.m.dwExtraInfo = IntPtr.Zero; aInput[0].i.m.mouseData = 0; aInput[0].i.m.time = 0; aInput[0].i.m.dx = MousePos.X; aInput[0].i.m.dy = MousePos.Y; //up event aInput[1].type = WindowsAPI.INPUT_MOUSE; aInput[1].i.m.dwExtraInfo = IntPtr.Zero; aInput[1].i.m.mouseData = 0; aInput[1].i.m.time = 0; aInput[1].i.m.dx = MousePos.X; aInput[1].i.m.dy = MousePos.Y; if (ParamLine.Contains(Keyword_Up)) { //just send the up event: aInput[0] = aInput[1]; for (int i = 1; ClickCount >= i; i++) WindowsAPI.SendInput(1, aInput, Marshal.SizeOf(typeof(WindowsAPI.INPUT))); } else if (ParamLine.Contains(Keyword_Down)) { //just send the down event: for (int i = 1; ClickCount >= i; i++) WindowsAPI.SendInput(1, aInput, Marshal.SizeOf(typeof(WindowsAPI.INPUT))); } else //send both events: for (int i = 1; ClickCount >= i; i++) WindowsAPI.SendInput((uint)aInput.Length, aInput, Marshal.SizeOf(typeof(WindowsAPI.INPUT))); } }
/// <summary> /// Clicks a mouse button at the specified coordinates. It can also hold down a mouse button, turn the mouse wheel, or move the mouse. /// </summary> /// <param name="options">Can be one or more of the following options: /// <list type="bullet"> /// <item><term><c>X</c> or <c>Y</c></term>: <description>the coordinates;</description></item> /// <item><term>Button</term>: <description><c>Left</c> (default), <c>Middle</c>, <c>Right</c>, <c>X1</c> or <c>X2</c>;</description></item> /// <item><term>Wheel</term>: <description><c>WheelUp</c>, <c>WheelDown</c>, <c>WheelLeft</c> or <c>WheelRight</c>;</description></item> /// <item><term>Count</term>: <description>number of times to send the click;</description></item> /// <item><term><c>Down</c> or <c>Up</c></term>: <description>if omitted send a down click followed by an up release;</description></item> /// <item><term><c>Relative</c></term>: <description>treat the coordinates as relative offsets from the current mouse position.</description></item> /// </list> /// </param> public static void Click(object[] options) { string ParamLine = string.Empty; var MousePos = new Point(0, 0); int ClickCount = 1; const string delimiter = ","; var RE_Coord = new Regex(@"(\d*?)\s*,\s*(\d*)[\s,\,]*", RegexOptions.IgnoreCase); var RE_Num = new Regex(@"\d+", RegexOptions.IgnoreCase); CaptureCollection Out; Match Match; //rebuild Argument string, as we have to parse this in a special way foreach (var option in options) { if (option is string) { ParamLine += (string)option + delimiter; } else if (option is double) { ParamLine += ((int)(double)option) + delimiter; } } ParamLine = ParamLine.ToLower().Substring(0, ParamLine.Length - 1); //search coordinates, move mouse, remove them if (RE_Coord.IsMatch(ParamLine)) { Match = RE_Coord.Match(ParamLine); MousePos.X = Convert.ToInt32(Match.Groups[1].Value); MousePos.Y = Convert.ToInt32(Match.Groups[2].Value); ParamLine = RE_Coord.Replace(ParamLine, string.Empty); //remove coord if (coords.Mouse == CoordModeType.Relative) { var foreGroundWindow = Window.WindowItemProvider.Instance.ActiveWindow; if (foreGroundWindow != null) { var location = foreGroundWindow.Location; MousePos.X += location.X; MousePos.Y += location.Y; } } Cursor.Position = MousePos; } //click count if (RE_Num.IsMatch(ParamLine)) { Out = RE_Num.Match(ParamLine).Captures; ClickCount = Convert.ToInt32(Out[0].Value); if (ClickCount <= 0) { return; } } if (Environment.OSVersion.Platform == PlatformID.Win32NT) { var aInput = new WindowsAPI.INPUT[2]; //right or left mouse if (ParamLine.Contains(Keyword_Right)) { aInput[0].i.m.dwFlags = (uint)WindowsAPI.MOUSEEVENTF.RIGHTDOWN; aInput[1].i.m.dwFlags = (uint)WindowsAPI.MOUSEEVENTF.RIGHTUP; } else { aInput[0].i.m.dwFlags = (uint)WindowsAPI.MOUSEEVENTF.LEFTDOWN; aInput[1].i.m.dwFlags = (uint)WindowsAPI.MOUSEEVENTF.LEFTUP; } //down event aInput[0].type = WindowsAPI.INPUT_MOUSE; aInput[0].i.m.dwExtraInfo = IntPtr.Zero; aInput[0].i.m.mouseData = 0; aInput[0].i.m.time = 0; aInput[0].i.m.dx = MousePos.X; aInput[0].i.m.dy = MousePos.Y; //up event aInput[1].type = WindowsAPI.INPUT_MOUSE; aInput[1].i.m.dwExtraInfo = IntPtr.Zero; aInput[1].i.m.mouseData = 0; aInput[1].i.m.time = 0; aInput[1].i.m.dx = MousePos.X; aInput[1].i.m.dy = MousePos.Y; if (ParamLine.Contains(Keyword_Up)) { //just send the up event: aInput[0] = aInput[1]; for (int i = 1; ClickCount >= i; i++) { WindowsAPI.SendInput(1, aInput, Marshal.SizeOf(typeof(WindowsAPI.INPUT))); } } else if (ParamLine.Contains(Keyword_Down)) { //just send the down event: for (int i = 1; ClickCount >= i; i++) { WindowsAPI.SendInput(1, aInput, Marshal.SizeOf(typeof(WindowsAPI.INPUT))); } } else //send both events: { for (int i = 1; ClickCount >= i; i++) { WindowsAPI.SendInput((uint)aInput.Length, aInput, Marshal.SizeOf(typeof(WindowsAPI.INPUT))); } } } }