using System; using System.Threading; using WindowsInput.Native; namespace WindowsInput { /// /// Implements the interface by calling the an to simulate Mouse gestures. /// public class MouseSimulator : IMouseSimulator { private const int MouseWheelClickSize = 120; private readonly IInputSimulator _inputSimulator; /// /// The instance of the to use for dispatching messages. /// private readonly IInputMessageDispatcher _messageDispatcher; /// /// Initializes a new instance of the class using an instance of a for dispatching messages. /// /// The that owns this instance. public MouseSimulator(IInputSimulator inputSimulator) { if (inputSimulator == null) throw new ArgumentNullException("inputSimulator"); _inputSimulator = inputSimulator; _messageDispatcher = new WindowsInputMessageDispatcher(); } /// /// Initializes a new instance of the class using the specified for dispatching messages. /// /// The that owns this instance. /// The to use for dispatching messages. /// If null is passed as the . internal MouseSimulator(IInputSimulator inputSimulator, IInputMessageDispatcher messageDispatcher) { if (inputSimulator == null) throw new ArgumentNullException("inputSimulator"); if (messageDispatcher == null) throw new InvalidOperationException( string.Format("The {0} cannot operate with a null {1}. Please provide a valid {1} instance to use for dispatching {2} messages.", typeof(MouseSimulator).Name, typeof(IInputMessageDispatcher).Name, typeof(INPUT).Name)); _inputSimulator = inputSimulator; _messageDispatcher = messageDispatcher; } /// /// Gets the instance for simulating Keyboard input. /// /// The instance. public IKeyboardSimulator Keyboard { get { return _inputSimulator.Keyboard; } } /// /// Sends the list of messages using the instance. /// /// The of messages to send. private void SendSimulatedInput(INPUT[] inputList) { _messageDispatcher.DispatchInput(inputList); } /// /// Simulates mouse movement by the specified distance measured as a delta from the current mouse location in pixels. /// /// The distance in pixels to move the mouse horizontally. /// The distance in pixels to move the mouse vertically. public IMouseSimulator MoveMouseBy(int pixelDeltaX, int pixelDeltaY) { var inputList = new InputBuilder().AddRelativeMouseMovement(pixelDeltaX, pixelDeltaY).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates mouse movement to the specified location on the primary display device. /// /// The destination's absolute X-coordinate on the primary display device where 0 is the extreme left hand side of the display device and 65535 is the extreme right hand side of the display device. /// The destination's absolute Y-coordinate on the primary display device where 0 is the top of the display device and 65535 is the bottom of the display device. public IMouseSimulator MoveMouseTo(double absoluteX, double absoluteY) { var inputList = new InputBuilder().AddAbsoluteMouseMovement((int)Math.Truncate(absoluteX), (int)Math.Truncate(absoluteY)).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates mouse movement to the specified location on the Virtual Desktop which includes all active displays. /// /// The destination's absolute X-coordinate on the virtual desktop where 0 is the left hand side of the virtual desktop and 65535 is the extreme right hand side of the virtual desktop. /// The destination's absolute Y-coordinate on the virtual desktop where 0 is the top of the virtual desktop and 65535 is the bottom of the virtual desktop. public IMouseSimulator MoveMouseToPositionOnVirtualDesktop(double absoluteX, double absoluteY) { var inputList = new InputBuilder().AddAbsoluteMouseMovementOnVirtualDesktop((int)Math.Truncate(absoluteX), (int)Math.Truncate(absoluteY)).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse left button down gesture. /// public IMouseSimulator LeftButtonDown() { var inputList = new InputBuilder().AddMouseButtonDown(MouseButton.LeftButton).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse left button up gesture. /// public IMouseSimulator LeftButtonUp() { var inputList = new InputBuilder().AddMouseButtonUp(MouseButton.LeftButton).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse left-click gesture. /// public IMouseSimulator LeftButtonClick() { var inputList = new InputBuilder().AddMouseButtonClick(MouseButton.LeftButton).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse left button double-click gesture. /// public IMouseSimulator LeftButtonDoubleClick() { var inputList = new InputBuilder().AddMouseButtonDoubleClick(MouseButton.LeftButton).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse right button down gesture. /// public IMouseSimulator RightButtonDown() { var inputList = new InputBuilder().AddMouseButtonDown(MouseButton.RightButton).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse right button up gesture. /// public IMouseSimulator RightButtonUp() { var inputList = new InputBuilder().AddMouseButtonUp(MouseButton.RightButton).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse right button click gesture. /// public IMouseSimulator RightButtonClick() { var inputList = new InputBuilder().AddMouseButtonClick(MouseButton.RightButton).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse right button double-click gesture. /// public IMouseSimulator RightButtonDoubleClick() { var inputList = new InputBuilder().AddMouseButtonDoubleClick(MouseButton.RightButton).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse X button down gesture. /// /// The button id. public IMouseSimulator XButtonDown(int buttonId) { var inputList = new InputBuilder().AddMouseXButtonDown(buttonId).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse X button up gesture. /// /// The button id. public IMouseSimulator XButtonUp(int buttonId) { var inputList = new InputBuilder().AddMouseXButtonUp(buttonId).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse X button click gesture. /// /// The button id. public IMouseSimulator XButtonClick(int buttonId) { var inputList = new InputBuilder().AddMouseXButtonClick(buttonId).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse X button double-click gesture. /// /// The button id. public IMouseSimulator XButtonDoubleClick(int buttonId) { var inputList = new InputBuilder().AddMouseXButtonDoubleClick(buttonId).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates mouse vertical wheel scroll gesture. /// /// The amount to scroll in clicks. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. public IMouseSimulator VerticalScroll(int scrollAmountInClicks) { var inputList = new InputBuilder().AddMouseVerticalWheelScroll(scrollAmountInClicks * MouseWheelClickSize).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Simulates a mouse horizontal wheel scroll gesture. Supported by Windows Vista and later. /// /// The amount to scroll in clicks. A positive value indicates that the wheel was rotated to the right; a negative value indicates that the wheel was rotated to the left. public IMouseSimulator HorizontalScroll(int scrollAmountInClicks) { var inputList = new InputBuilder().AddMouseHorizontalWheelScroll(scrollAmountInClicks * MouseWheelClickSize).ToArray(); SendSimulatedInput(inputList); return this; } /// /// Sleeps the executing thread to create a pause between simulated inputs. /// /// The number of milliseconds to wait. public IMouseSimulator Sleep(int millsecondsTimeout) { Thread.Sleep(millsecondsTimeout); return this; } /// /// Sleeps the executing thread to create a pause between simulated inputs. /// /// The time to wait. public IMouseSimulator Sleep(TimeSpan timeout) { Thread.Sleep(timeout); return this; } } }