private static void DoClickMouse(ClickNodeSubType clickType) { INPUT mouseInput = new INPUT() { type = 0, // Mouse input mi = new MOUSEINPUT() }; switch(clickType) { case ClickNodeSubType.LeftClick: mouseInput.mi.dwFlags = MouseEventFlags.LeftDown; SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT())); mouseInput.mi.dwFlags = MouseEventFlags.LeftUp; SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT())); break; case ClickNodeSubType.RightClick: mouseInput.mi.dwFlags = MouseEventFlags.RightDown; SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT())); mouseInput.mi.dwFlags = MouseEventFlags.RightUp; SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT())); break; case ClickNodeSubType.MiddleClick: mouseInput.mi.dwFlags = MouseEventFlags.MiddleDown; SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT())); mouseInput.mi.dwFlags = MouseEventFlags.MiddleUp; SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT())); break; } }
public static void SmoothClickMouseTo(Point dest, Int32 totalDuration, ClickNodeSubType clickType) { Point start = Cursor.Position; PointF iterPoint = start; Int32 steps = totalDuration / MouseEventDelayMS; // Find the slope of the line segment defined by start and dest PointF slope = new PointF(dest.X - start.X, dest.Y - start.Y); // Divide by the number of steps slope.X = slope.X / steps; slope.Y = slope.Y / steps; // Move the mouse to each iterative point. for(int i = 0; i < steps; i++) { iterPoint = new PointF(iterPoint.X + slope.X, iterPoint.Y + slope.Y); Cursor.Position = Point.Round(iterPoint); System.Threading.Thread.Sleep(MouseEventDelayMS + (new Random()).Next(4)); } // Move the mouse to the final destination. Cursor.Position = dest; DoClickMouse(clickType); }