-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMouseSimulator.cs
270 lines (241 loc) · 11.9 KB
/
MouseSimulator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Threading;
using XPloit.Windows.Api.Native;
namespace XPloit.Windows.Api
{
/// <summary>
/// Implements the <see cref="IMouseSimulator"/> interface by calling the an <see cref="IInputMessageDispatcher"/> to simulate Mouse gestures.
/// </summary>
public class MouseSimulator : IMouseSimulator
{
private const int MouseWheelClickSize = 120;
private readonly IInputSimulator _inputSimulator;
/// <summary>
/// The instance of the <see cref="IInputMessageDispatcher"/> to use for dispatching <see cref="INPUT"/> messages.
/// </summary>
private readonly IInputMessageDispatcher _messageDispatcher;
/// <summary>
/// Initializes a new instance of the <see cref="MouseSimulator"/> class using an instance of a <see cref="WindowsInputMessageDispatcher"/> for dispatching <see cref="INPUT"/> messages.
/// </summary>
/// <param name="inputSimulator">The <see cref="IInputSimulator"/> that owns this instance.</param>
public MouseSimulator(IInputSimulator inputSimulator)
{
if (inputSimulator == null) throw new ArgumentNullException("inputSimulator");
_inputSimulator = inputSimulator;
_messageDispatcher = new WindowsInputMessageDispatcher();
}
/// <summary>
/// Initializes a new instance of the <see cref="MouseSimulator"/> class using the specified <see cref="IInputMessageDispatcher"/> for dispatching <see cref="INPUT"/> messages.
/// </summary>
/// <param name="inputSimulator">The <see cref="IInputSimulator"/> that owns this instance.</param>
/// <param name="messageDispatcher">The <see cref="IInputMessageDispatcher"/> to use for dispatching <see cref="INPUT"/> messages.</param>
/// <exception cref="InvalidOperationException">If null is passed as the <paramref name="messageDispatcher"/>.</exception>
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;
}
/// <summary>
/// Gets the <see cref="IKeyboardSimulator"/> instance for simulating Keyboard input.
/// </summary>
/// <value>The <see cref="IKeyboardSimulator"/> instance.</value>
public IKeyboardSimulator Keyboard { get { return _inputSimulator.Keyboard; } }
/// <summary>
/// Sends the list of <see cref="INPUT"/> messages using the <see cref="IInputMessageDispatcher"/> instance.
/// </summary>
/// <param name="inputList">The <see cref="System.Array"/> of <see cref="INPUT"/> messages to send.</param>
private void SendSimulatedInput(INPUT[] inputList)
{
_messageDispatcher.DispatchInput(inputList);
}
/// <summary>
/// Simulates mouse movement by the specified distance measured as a delta from the current mouse location in pixels.
/// </summary>
/// <param name="pixelDeltaX">The distance in pixels to move the mouse horizontally.</param>
/// <param name="pixelDeltaY">The distance in pixels to move the mouse vertically.</param>
public IMouseSimulator MoveMouseBy(int pixelDeltaX, int pixelDeltaY)
{
var inputList = new InputBuilder().AddRelativeMouseMovement(pixelDeltaX, pixelDeltaY).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates mouse movement to the specified location on the primary display device.
/// </summary>
/// <param name="absoluteX">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.</param>
/// <param name="absoluteY">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.</param>
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;
}
/// <summary>
/// Simulates mouse movement to the specified location on the Virtual Desktop which includes all active displays.
/// </summary>
/// <param name="absoluteX">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.</param>
/// <param name="absoluteY">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.</param>
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;
}
/// <summary>
/// Simulates a mouse left button down gesture.
/// </summary>
public IMouseSimulator LeftButtonDown()
{
var inputList = new InputBuilder().AddMouseButtonDown(MouseButton.LeftButton).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse left button up gesture.
/// </summary>
public IMouseSimulator LeftButtonUp()
{
var inputList = new InputBuilder().AddMouseButtonUp(MouseButton.LeftButton).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse left-click gesture.
/// </summary>
public IMouseSimulator LeftButtonClick()
{
var inputList = new InputBuilder().AddMouseButtonClick(MouseButton.LeftButton).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse left button double-click gesture.
/// </summary>
public IMouseSimulator LeftButtonDoubleClick()
{
var inputList = new InputBuilder().AddMouseButtonDoubleClick(MouseButton.LeftButton).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse right button down gesture.
/// </summary>
public IMouseSimulator RightButtonDown()
{
var inputList = new InputBuilder().AddMouseButtonDown(MouseButton.RightButton).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse right button up gesture.
/// </summary>
public IMouseSimulator RightButtonUp()
{
var inputList = new InputBuilder().AddMouseButtonUp(MouseButton.RightButton).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse right button click gesture.
/// </summary>
public IMouseSimulator RightButtonClick()
{
var inputList = new InputBuilder().AddMouseButtonClick(MouseButton.RightButton).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse right button double-click gesture.
/// </summary>
public IMouseSimulator RightButtonDoubleClick()
{
var inputList = new InputBuilder().AddMouseButtonDoubleClick(MouseButton.RightButton).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse X button down gesture.
/// </summary>
/// <param name="buttonId">The button id.</param>
public IMouseSimulator XButtonDown(int buttonId)
{
var inputList = new InputBuilder().AddMouseXButtonDown(buttonId).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse X button up gesture.
/// </summary>
/// <param name="buttonId">The button id.</param>
public IMouseSimulator XButtonUp(int buttonId)
{
var inputList = new InputBuilder().AddMouseXButtonUp(buttonId).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse X button click gesture.
/// </summary>
/// <param name="buttonId">The button id.</param>
public IMouseSimulator XButtonClick(int buttonId)
{
var inputList = new InputBuilder().AddMouseXButtonClick(buttonId).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse X button double-click gesture.
/// </summary>
/// <param name="buttonId">The button id.</param>
public IMouseSimulator XButtonDoubleClick(int buttonId)
{
var inputList = new InputBuilder().AddMouseXButtonDoubleClick(buttonId).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates mouse vertical wheel scroll gesture.
/// </summary>
/// <param name="scrollAmountInClicks">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.</param>
public IMouseSimulator VerticalScroll(int scrollAmountInClicks)
{
var inputList = new InputBuilder().AddMouseVerticalWheelScroll(scrollAmountInClicks * MouseWheelClickSize).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Simulates a mouse horizontal wheel scroll gesture. Supported by Windows Vista and later.
/// </summary>
/// <param name="scrollAmountInClicks">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.</param>
public IMouseSimulator HorizontalScroll(int scrollAmountInClicks)
{
var inputList = new InputBuilder().AddMouseHorizontalWheelScroll(scrollAmountInClicks * MouseWheelClickSize).ToArray();
SendSimulatedInput(inputList);
return this;
}
/// <summary>
/// Sleeps the executing thread to create a pause between simulated inputs.
/// </summary>
/// <param name="millsecondsTimeout">The number of milliseconds to wait.</param>
public IMouseSimulator Sleep(int millsecondsTimeout)
{
Thread.Sleep(millsecondsTimeout);
return this;
}
/// <summary>
/// Sleeps the executing thread to create a pause between simulated inputs.
/// </summary>
/// <param name="timeout">The time to wait.</param>
public IMouseSimulator Sleep(TimeSpan timeout)
{
Thread.Sleep(timeout);
return this;
}
}
}