Repository navigation
Expand file tree
/
Copy pathSendInputMouse.cs
More file actions
194 lines (175 loc) · 6.14 KB
/
Copy pathSendInputMouse.cs
File metadata and controls
194 lines (175 loc) · 6.14 KB
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
using System.Threading;
using ProcessNET.Native;
using ProcessNET.Native.Types;
using ProcessNET.Utilities;
namespace ProcessNET.Windows.Mouse
{
/// <summary>
/// Class defining a virtual mouse using the API SendInput.
/// </summary>
public class SendInputMouse : IMouse
{
/// <summary>
/// Initializes a new instance of a child of the <see cref="SendInputMouse" /> class.
/// </summary>
/// <param name="window">The reference of the <see cref="IWindow" /> object.</param>
public SendInputMouse(IWindow window)
{
Window = window;
}
protected IWindow Window { get; set; }
/// <summary>
/// Clicks the left button of the mouse at the current cursor position.
/// </summary>
public void ClickLeft()
{
PressLeft();
ReleaseLeft();
}
/// <summary>
/// Clicks the middle button of the mouse at the current cursor position.
/// </summary>
public void ClickMiddle()
{
PressMiddle();
ReleaseMiddle();
}
/// <summary>
/// Clicks the right button of the mouse at the current cursor position.
/// </summary>
public void ClickRight()
{
PressRight();
ReleaseRight();
}
/// <summary>
/// Double clicks the left button of the mouse at the current cursor position.
/// </summary>
public void DoubleClickLeft()
{
ClickLeft();
Thread.Sleep(10);
ClickLeft();
}
/// <summary>
/// Moves the cursor at the specified coordinate from the position of the window.
/// </summary>
/// <param name="x">The x-coordinate.</param>
/// <param name="y">The y-coordinate.</param>
public void MoveTo(int x, int y)
{
MoveToAbsolute(Window.X + x, Window.Y + y);
}
/// <summary>
/// Presses the left button of the mouse at the current cursor position.
/// </summary>
public void PressLeft()
{
var input = CreateInput();
input.Mouse.Flags = MouseFlags.LeftDown;
WindowHelper.SendInput(input);
}
/// <summary>
/// Presses the middle button of the mouse at the current cursor position.
/// </summary>
public void PressMiddle()
{
var input = CreateInput();
input.Mouse.Flags = MouseFlags.MiddleDown;
WindowHelper.SendInput(input);
}
/// <summary>
/// Presses the right button of the mouse at the current cursor position.
/// </summary>
public void PressRight()
{
var input = CreateInput();
input.Mouse.Flags = MouseFlags.RightDown;
WindowHelper.SendInput(input);
}
/// <summary>
/// Releases the left button of the mouse at the current cursor position.
/// </summary>
public void ReleaseLeft()
{
var input = CreateInput();
input.Mouse.Flags = MouseFlags.LeftUp;
WindowHelper.SendInput(input);
}
/// <summary>
/// Releases the middle button of the mouse at the current cursor position.
/// </summary>
public void ReleaseMiddle()
{
var input = CreateInput();
input.Mouse.Flags = MouseFlags.MiddleUp;
WindowHelper.SendInput(input);
}
/// <summary>
/// Releases the right button of the mouse at the current cursor position.
/// </summary>
public void ReleaseRight()
{
var input = CreateInput();
input.Mouse.Flags = MouseFlags.RightUp;
WindowHelper.SendInput(input);
}
/// <summary>
/// Scrolls horizontally using the wheel of the mouse at the current cursor position.
/// </summary>
/// <param name="delta">The amount of wheel movement.</param>
public void ScrollHorizontally(int delta = 120)
{
var input = CreateInput();
input.Mouse.Flags = MouseFlags.HWheel;
input.Mouse.MouseData = delta;
WindowHelper.SendInput(input);
}
/// <summary>
/// Scrolls vertically using the wheel of the mouse at the current cursor position.
/// </summary>
/// <param name="delta">The amount of wheel movement.</param>
public void ScrollVertically(int delta = 120)
{
var input = CreateInput();
input.Mouse.Flags = MouseFlags.Wheel;
input.Mouse.MouseData = delta;
WindowHelper.SendInput(input);
}
/// <summary>
/// Moves the cursor at the specified coordinate.
/// </summary>
/// <param name="x">The x-coordinate.</param>
/// <param name="y">The y-coordinate.</param>
protected void MoveToAbsolute(int x, int y)
{
var input = CreateInput();
input.Mouse.DeltaX = CalculateAbsoluteCoordinateX(x);
input.Mouse.DeltaY = CalculateAbsoluteCoordinateY(y);
input.Mouse.Flags = MouseFlags.Move | MouseFlags.Absolute;
input.Mouse.MouseData = 0;
WindowHelper.SendInput(input);
}
/// <summary>
/// Calculates the x-coordinate with the system metric.
/// </summary>
private static int CalculateAbsoluteCoordinateX(int x)
{
return x*65536/User32.GetSystemMetrics(SystemMetrics.CxScreen);
}
/// <summary>
/// Calculates the y-coordinate with the system metric.
/// </summary>
private static int CalculateAbsoluteCoordinateY(int y)
{
return y*65536/User32.GetSystemMetrics(SystemMetrics.CyScreen);
}
/// <summary>
/// Create an <see cref="Input" /> structure for mouse event.
/// </summary>
private static Input CreateInput()
{
return new Input(InputTypes.Mouse);
}
}
}