forked from yangzhongke/NetAutoGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsMouseController.cs
More file actions
141 lines (129 loc) · 4.28 KB
/
Copy pathWindowsMouseController.cs
File metadata and controls
141 lines (129 loc) · 4.28 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
using NetAutoGUI.Internals;
using System;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using Vanara.PInvoke;
namespace NetAutoGUI.Windows
{
[SupportedOSPlatform("windows")]
internal class WindowsMouseController : AbstractMouseController
{
public override void Click(int? x = null, int? y = null, MouseButtonType button = MouseButtonType.Left,
int clicks = 1, double intervalInSeconds = 0)
{
if (clicks <= 0)
{
throw new ArgumentOutOfRangeException(nameof(clicks), "clicks should be positive.");
}
TryMoveTo(x, y);
for (int i = 0; i < clicks; i++)
{
if (button == MouseButtonType.Left)
{
MouseHelper.LeftButtonClick();
}
else if (button == MouseButtonType.Middle)
{
MouseHelper.MiddleButtonClick();
}
else if (button == MouseButtonType.Right)
{
MouseHelper.RightButtonClick();
}
Thread.Sleep((int)(intervalInSeconds * 1000));
}
Thread.Sleep(100);
}
public override async Task ClickAsync(int? x = null, int? y = null,
MouseButtonType button = MouseButtonType.Left,
int clicks = 1,
double intervalInSeconds = 0, CancellationToken cancellationToken = default)
{
if (clicks <= 0)
{
throw new ArgumentOutOfRangeException(nameof(clicks), "clicks should be positive.");
}
TryMoveTo(x, y);
for (int i = 0; i < clicks; i++)
{
if (button == MouseButtonType.Left)
{
MouseHelper.LeftButtonClick();
}
else if (button == MouseButtonType.Middle)
{
MouseHelper.MiddleButtonClick();
}
else if (button == MouseButtonType.Right)
{
MouseHelper.RightButtonClick();
}
await Task.Delay((int)(intervalInSeconds * 1000), cancellationToken);
}
await Task.Delay(100, cancellationToken);
}
public override void MouseDown(int? x = null, int? y = null, MouseButtonType button = MouseButtonType.Left)
{
TryMoveTo(x, y);
if (button == MouseButtonType.Left)
{
MouseHelper.LeftButtonDown();
}
else if (button == MouseButtonType.Middle)
{
MouseHelper.MiddleButtonDown();
}
else if (button == MouseButtonType.Right)
{
MouseHelper.RightButtonDown();
}
}
public override void MouseUp(int? x = null, int? y = null, MouseButtonType button = MouseButtonType.Left)
{
TryMoveTo(x, y);
if (button == MouseButtonType.Left)
{
MouseHelper.LeftButtonUp();
}
else if (button == MouseButtonType.Middle)
{
MouseHelper.MiddleButtonUp();
}
else if (button == MouseButtonType.Right)
{
MouseHelper.RightButtonUp();
}
}
private void TryMoveTo(int? x, int? y)
{
int destX, destY;
if (x == null && y == null)
{
(destX, destY) = Position();
}
else if (x != null && y != null)
{
(destX, destY) = (x.Value, y.Value);
}
else
{
throw new ArgumentException("Either x or y are all null or none of them are null");
}
MoveTo(destX, destY);
}
public override void MoveTo(int x, int y)
{
MouseHelper.MoveTo(x, y);
}
public override Location Position()
{
User32.GetCursorPos(out POINT point);
return new Location(point.X, point.Y);
}
public override void Scroll(int value)
{
MouseHelper.Scroll(value);
}
}
}