public static void SendMouseInput(int positionX, int positionY, int maxX, int maxY, bool leftDown) { if (positionX > int.MaxValue) throw new ArgumentOutOfRangeException("positionX"); if (positionY > int.MaxValue) throw new ArgumentOutOfRangeException("positionY"); Input[] i = new Input[2]; // move the mouse to the position specified i[0] = new Input(); i[0].Type = InputMouse; i[0].MouseInput.X = (positionX * 65535) / maxX; i[0].MouseInput.Y = (positionY * 65535) / maxY; i[0].MouseInput.Flags = MouseEventAbsolute | MouseEventMove; // determine if we need to send a mouse down or mouse up event if (!lastLeftDown && leftDown) { i[1] = new Input(); i[1].Type = InputMouse; i[1].MouseInput.Flags = MouseEventLeftDown; } else if (lastLeftDown && !leftDown) { i[1] = new Input(); i[1].Type = InputMouse; i[1].MouseInput.Flags = MouseEventLeftUp; } lastLeftDown = leftDown; // send it off uint result = SendInput(2, i, Marshal.SizeOf(i[0])); if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); }
public static void scroll(WheelPosition pos, int amount) { Input i = new Input(); i.Type = InputMouse; if (pos == WheelPosition.HORIZONTAL) { i.MouseInput.Flags = MOUSEEVENTF_HWHEEL; } else if (pos == WheelPosition.VERTICAL) { i.MouseInput.Flags = MOUSEEVENTF_WHEEL; } i.MouseInput.MouseData = amount * WHEEL_DELTA - 1; Input[] ia = new Input[1]; ia[0] = i; // send it off uint result = SendInput(1, ia, Marshal.SizeOf(i)); if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); }
private static extern uint SendInput(uint numInputs, Input[] inputs, int size);
public static void release(MouseButton selected) { Input i = new Input(); i.Type = InputMouse; if (selected == MouseButton.LEFT) { i.MouseInput.Flags = MouseEventLeftUp; } else if (selected == MouseButton.RIGHT) { i.MouseInput.Flags = MouseEventRightUp; } else if (selected == MouseButton.MIDDLE) { i.MouseInput.Flags = MOUSEEVENTF_MIDDLEUP; } else if (selected == MouseButton.X1) { i.MouseInput.MouseData = XBUTTON1; i.MouseInput.Flags = MOUSEEVENTF_XUP; } else if (selected == MouseButton.X2) { i.MouseInput.MouseData = XBUTTON2; i.MouseInput.Flags = MOUSEEVENTF_XUP; } Input[] ia = new Input[1]; ia[0] = i; // send it off uint result = SendInput(1, ia, Marshal.SizeOf(i)); if (result == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); }