返回提交历史
Added
LICENSE
+12
-0
Deleted
LICENSE.txt
+0
-21
Modified
XFE各类拓展.NetCore.InputSimulator/InputSimulator.cs
+180
-22
Added
XFE各类拓展.NetCore.InputSimulator/MouseButton.cs
+20
-0
Modified
XFE各类拓展.NetCore.InputSimulator/XFE各类拓展.NetCore.InputSimulator.csproj
+28
-0
XFEstudio/XFEExtension.NetCore.InputSimulator
添加鼠标模拟支持
dc466a8
代码差异
5 个文件
+240
-43
@@ -0,0 +1,12 @@
1
MIT License
2
3
版权所有(C)[2023] [XFEstudio]
4
5
在此授予任何获得本软件和相关文档文件(以下简称“软件”)副本的人免费使用软件的权利,不受限制,
6
包括不限制权利的权利,不受限制地处理软件,以及在软件的软件内和外部,无论是否符合以下条件的软件。
7
8
以上版权声明和本许可声明应包含在所有软件的副本或实质性部分中。
9
10
软件按“原样”提供,不提供任何形式的保证,无论是明示的、默示的还是法定的。
11
在使用本软件时,作者或版权持有人不对任何直接、间接、偶然、特殊、惩罚性或后果性的损害负责,
12
无论是因使用本软件或其他方式(包括但不限于使用本软件引起的数据丢失或利润损失)。
@@ -1,21 +0,0 @@
1
MIT License
2
3
Copyright (c) [year] [fullname]
4
5
Permission is hereby granted, free of charge, to any person obtaining a copy
6
of this software and associated documentation files (the "Software"), to deal
7
in the Software without restriction, including without limitation the rights
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
copies of the Software, and to permit persons to whom the Software is
10
furnished to do so, subject to the following conditions:
11
12
The above copyright notice and this permission notice shall be included in all
13
copies or substantial portions of the Software.
14
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
SOFTWARE.
@@ -3,39 +3,74 @@ using System.Runtime.InteropServices;
3
3
4
4
namespace XFE各类拓展.NetCore.InputSimulator;
5
5
6
public enum MouseButtons
7
{
8
Left,
9
Right,
10
Middle
11
}
12
13
public static class InputSimulator
6
/// <summary>
7
/// 模拟用户输入
8
/// </summary>
9
public static partial class InputSimulator
14
10
{
15
11
#region DLL引用
16
[DllImport("user32.dll", SetLastError = true)]
17
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, IntPtr dwExtraInfo);
18
[DllImport("user32.dll")]
19
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, IntPtr dwExtraInfo);
12
[LibraryImport("user32.dll", SetLastError = true)]
13
private static partial void keybd_event(byte bVk, byte bScan, uint dwFlags, IntPtr dwExtraInfo);
14
[LibraryImport("user32.dll")]
15
private static partial void mouse_event(uint dwFlags, int dx, int dy, uint dwData, IntPtr dwExtraInfo);
16
[LibraryImport("user32.dll")]
17
private static partial int GetSystemMetrics(int nIndex);
18
[LibraryImport("user32.dll")]
19
static partial void SetCursorPos(int x, int y);
20
20
[DllImport("user32.dll")]
21
21
[return: MarshalAs(UnmanagedType.Bool)]
22
static extern bool SetCursorPos(int x, int y);
22
static extern bool GetCursorPos(out Point lpPoint);
23
23
#endregion
24
24
#region 常量
25
private const int KEYEVENTF_KEYDOWN = 0x0000; // Key down flag
26
private const int KEYEVENTF_KEYUP = 0x0002; // Key up flag
27
private const uint MOUSEEVENTF_MOVE = 0x0001;
28
private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
29
private const uint MOUSEEVENTF_LEFTUP = 0x0004;
30
private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
31
private const uint MOUSEEVENTF_RIGHTUP = 0x0010;
25
private const int KEYEVENTF_KEYDOWN = 0x0000; // 按键按下
26
private const int KEYEVENTF_KEYUP = 0x0002; // 按键松开
27
private const uint MOUSEEVENTF_MOVE = 0x0001; // 鼠标移动
28
private const uint MOUSEEVENTF_LEFTDOWN = 0x0002; // 鼠标左键按下
29
private const uint MOUSEEVENTF_LEFTUP = 0x0004; // 鼠标左键松开
30
private const uint MOUSEEVENTF_RIGHTDOWN = 0x0008; // 鼠标右键按下
31
private const uint MOUSEEVENTF_RIGHTUP = 0x0010; // 鼠标右键松开
32
private const uint MOUSEEVENTF_MIDDOWN = 0x0020; // 鼠标中键按下
33
private const uint MOUSEEVENTF_MIDUP = 0x0040; // 鼠标中键松开
34
private const uint MOUSEEVENTF_WHEEL = 0x0800; // 鼠标滚轮
35
private const int SM_CXSCREEN = 0; // 屏幕宽度
36
private const int SM_CYSCREEN = 1; // 屏幕高度
32
37
#endregion
38
#region 键盘方法
39
/// <summary>
40
/// 按下某个按键
41
/// </summary>
42
/// <remarks>
43
/// 如果需要输入一组按键,请使用<see cref="InputKeys(string)"/>
44
/// </remarks>
45
/// <param name="key">按键</param>
33
46
public static void PressKey(char key)
34
47
{
35
48
byte keyCode = (byte)key;
36
49
keybd_event(keyCode, 0, KEYEVENTF_KEYDOWN, IntPtr.Zero);
37
50
keybd_event(keyCode, 0, KEYEVENTF_KEYUP, IntPtr.Zero);
38
51
}
52
/// <summary>
53
/// 按下某个按键
54
/// </summary>
55
/// <remarks>
56
/// 如果需要输入一组按键,请使用<see cref="InputKeys(string)"/>
57
/// </remarks>
58
/// <param name="key">按键</param>
59
/// <param name="holdTime">按下时间</param>
60
public static async Task PressKeyAsync(char key, int holdTime)
61
{
62
byte keyCode = (byte)key;
63
keybd_event(keyCode, 0, KEYEVENTF_KEYDOWN, IntPtr.Zero);
64
await Task.Delay(holdTime);
65
keybd_event(keyCode, 0, KEYEVENTF_KEYUP, IntPtr.Zero);
66
}
67
/// <summary>
68
/// 输入一组按键
69
/// </summary>
70
/// <remarks>
71
/// 如果需要输入延迟,请使用<see cref="InputKeysAsync(string, int, int)"/>
72
/// </remarks>
73
/// <param name="keys">待输入按键</param>
39
74
public static void InputKeys(string keys)
40
75
{
41
76
foreach (var key in keys)
@@ -45,8 +80,131 @@ public static class InputSimulator
45
80
keybd_event(keyCode, 0, KEYEVENTF_KEYUP, IntPtr.Zero);
46
81
}
47
82
}
48
static void MouseClick(MouseButtons button, int x, int y)
83
/// <summary>
84
/// 输入一组按键
85
/// </summary>
86
/// <param name="keys">待输入按键</param>
87
/// <param name="holdTime">每个按键按住的时间</param>
88
/// <param name="delay">按键间延迟</param>
89
/// <returns></returns>
90
public static async Task InputKeysAsync(string keys, int holdTime = 0, int delay = 0)
49
91
{
50
92
foreach (var key in keys)
93
{
94
byte keyCode = (byte)key;
95
keybd_event(keyCode, 0, KEYEVENTF_KEYDOWN, IntPtr.Zero);
96
await Task.Delay(holdTime);
97
keybd_event(keyCode, 0, KEYEVENTF_KEYUP, IntPtr.Zero);
98
await Task.Delay(delay);
99
}
100
}
101
#endregion
102
#region 鼠标方法
103
/// <summary>
104
/// 相对移动
105
/// </summary>
106
/// <remarks>
107
/// 如果需要绝对移动请使用<see cref="LocateTo(int, int)"/>方法
108
/// </remarks>
109
/// <param name="x">X偏移</param>
110
/// <param name="y">Y偏移</param>
111
public static void Move(int x, int y)
112
{
113
mouse_event(MOUSEEVENTF_MOVE, x, y, 0, IntPtr.Zero);
51
114
}
115
/// <summary>
116
/// 将鼠标定位至
117
/// </summary>
118
/// <remarks>
119
/// 如果需要相对移动请使用<see cref="Move(int, int)"/>方法
120
/// </remarks>
121
/// <param name="x">X坐标</param>
122
/// <param name="y">Y坐标</param>
123
public static void LocateTo(int x, int y)
124
{
125
SetCursorPos(x, y);
126
}
127
/// <summary>
128
/// 将鼠标定位至
129
/// </summary>
130
/// <remarks>
131
/// 如果需要相对移动请使用<see cref="Move(int, int)"/>方法
132
/// </remarks>
133
/// <param name="point">定位到的位置</param>
134
public static void LocateTo(Point point)
135
{
136
LocateTo(point.X, point.Y);
137
}
138
/// <summary>
139
/// 模拟鼠标点击
140
/// </summary>
141
/// <param name="mouseButton">鼠标按键</param>
142
public static void MouseClick(MouseButton mouseButton)
143
{
144
switch (mouseButton)
145
{
146
case MouseButton.Left:
147
mouse_event(MOUSEEVENTF_LEFTDOWN, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
148
mouse_event(MOUSEEVENTF_LEFTUP, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
149
break;
150
case MouseButton.Right:
151
mouse_event(MOUSEEVENTF_RIGHTDOWN, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
152
mouse_event(MOUSEEVENTF_RIGHTUP, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
153
break;
154
case MouseButton.Middle:
155
mouse_event(MOUSEEVENTF_MIDDOWN, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
156
mouse_event(MOUSEEVENTF_MIDUP, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
157
break;
158
default:
159
break;
160
}
161
}
162
/// <summary>
163
/// 模拟鼠标点击
164
/// </summary>
165
/// <param name="mouseButton">鼠标按键</param>
166
/// <param name="holdTime">鼠标按键按住时间</param>
167
public static async Task MouseClickAsync(MouseButton mouseButton, int holdTime)
168
{
169
switch (mouseButton)
170
{
171
case MouseButton.Left:
172
mouse_event(MOUSEEVENTF_LEFTDOWN, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
173
await Task.Delay(holdTime);
174
mouse_event(MOUSEEVENTF_LEFTUP, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
175
break;
176
case MouseButton.Right:
177
mouse_event(MOUSEEVENTF_RIGHTDOWN, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
178
await Task.Delay(holdTime);
179
mouse_event(MOUSEEVENTF_RIGHTUP, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
180
break;
181
case MouseButton.Middle:
182
mouse_event(MOUSEEVENTF_MIDDOWN, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
183
await Task.Delay(holdTime);
184
mouse_event(MOUSEEVENTF_MIDUP, GetMousePosition().X, GetMousePosition().Y, 0, IntPtr.Zero);
185
break;
186
default:
187
break;
188
}
189
}
190
#endregion
191
#region 系统信息获取方法
192
/// <summary>
193
/// 获取屏幕的宽高
194
/// </summary>
195
/// <returns>屏幕宽高</returns>
196
public static Point GetScreenSize()
197
{
198
return new Point(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
199
}
200
/// <summary>
201
/// 获取鼠标的当前位置
202
/// </summary>
203
/// <returns>鼠标位置</returns>
204
public static Point GetMousePosition()
205
{
206
GetCursorPos(out Point point);
207
return point;
208
}
209
#endregion
52
210
}
@@ -0,0 +1,20 @@
1
namespace XFE各类拓展.NetCore.InputSimulator;
2
3
/// <summary>
4
/// 鼠标按键
5
/// </summary>
6
public enum MouseButton
7
{
8
/// <summary>
9
/// 鼠标左键
10
/// </summary>
11
Left,
12
/// <summary>
13
/// 鼠标右键
14
/// </summary>
15
Right,
16
/// <summary>
17
/// 鼠标中键
18
/// </summary>
19
Middle
20
}
@@ -4,6 +4,34 @@
4
4
<TargetFramework>net8.0</TargetFramework>
5
5
<ImplicitUsings>enable</ImplicitUsings>
6
6
<Nullable>enable</Nullable>
7
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8
<GenerateDocumentationFile>True</GenerateDocumentationFile>
9
<SignAssembly>True</SignAssembly>
10
<AssemblyOriginatorKeyFile>C:\Users\XFEstudio\Desktop\work\C#\GitHub\XFEstudio\XFEExtension.NetCore.InputSimulator\XFEstudio.pfx</AssemblyOriginatorKeyFile>
11
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
12
<Title>XFE各类拓展.NetCore.InputSimulator</Title>
13
<Authors>XFEstudio</Authors>
14
<Company>寰宇朽力网络科技</Company>
15
<Description>用于实现模拟键盘输入的包</Description>
16
<Copyright>XFE工作室版权所有</Copyright>
17
<PackageProjectUrl>https://github.com/XFEstudio/XFEExtension.NetCore.InputSimulator</PackageProjectUrl>
18
<PackageReadmeFile>README.md</PackageReadmeFile>
19
<RepositoryUrl>https://github.com/XFEstudio/XFEExtension.NetCore.InputSimulator</RepositoryUrl>
20
<PackageTags>XFE;拓展;键盘;按键;模拟输入;Input</PackageTags>
21
<PackageReleaseNotes>新增对模拟鼠标操作的支持</PackageReleaseNotes>
22
<PackageLicenseFile>LICENSE</PackageLicenseFile>
23
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
7
24
</PropertyGroup>
8
25
26
<ItemGroup>
27
<None Include="..\LICENSE">
28
<Pack>True</Pack>
29
<PackagePath>\</PackagePath>
30
</None>
31
<None Include="..\README.md">
32
<Pack>True</Pack>
33
<PackagePath>\</PackagePath>
34
</None>
35
</ItemGroup>
36
9
37
</Project>