-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
289 lines (247 loc) · 7.95 KB
/
Copy pathNativeMethods.cs
File metadata and controls
289 lines (247 loc) · 7.95 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace SquadTools;
internal static class NativeMethods
{
internal const int WhKeyboardLowLevel = 13;
internal const int WmKeyDown = 0x0100;
internal const int WmSystemKeyDown = 0x0104;
internal const int WmHotKey = 0x0312;
internal const int VkF8 = 0x77;
internal const int VkF9 = 0x78;
internal const int VkF10 = 0x79;
internal const int VkOem3 = 0xC0;
internal const int VkControl = 0x11;
internal const int VkLeftShift = 0xA0;
internal const int VkW = 0x57;
internal const int VkV = 0x56;
internal const int VkReturn = 0x0D;
[DllImport("user32.dll")]
internal static extern short GetAsyncKeyState(int virtualKey);
[DllImport("user32.dll")]
internal static extern uint MapVirtualKey(uint virtualKey, uint mapType);
internal const uint MapVkToScanCode = 0;
[ThreadStatic]
private static int lastInputError;
internal static int LastInputError => lastInputError;
[DllImport("user32.dll", SetLastError = true)]
internal static extern uint SendInput(uint inputCount, Input[] inputs, int inputSize);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool RegisterHotKey(IntPtr windowHandle, int id, uint modifiers, uint virtualKey);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool UnregisterHotKey(IntPtr windowHandle, int id);
[DllImport("user32.dll")]
internal static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
internal static extern uint GetWindowThreadProcessId(IntPtr windowHandle, out uint processId);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr SetWindowsHookEx(
int hookId,
LowLevelKeyboardProc callback,
IntPtr moduleHandle,
uint threadId);
[DllImport("user32.dll")]
internal static extern IntPtr CallNextHookEx(IntPtr hookHandle, int code, IntPtr message, IntPtr data);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool UnhookWindowsHookEx(IntPtr hookHandle);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
internal static extern IntPtr GetModuleHandle(string? moduleName);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetWindowPos(
IntPtr windowHandle,
IntPtr insertAfter,
int x,
int y,
int width,
int height,
uint flags);
internal delegate IntPtr LowLevelKeyboardProc(int code, IntPtr message, IntPtr data);
internal static bool IsSquadForeground()
{
IntPtr foregroundWindow = GetForegroundWindow();
if (foregroundWindow == IntPtr.Zero)
{
return false;
}
GetWindowThreadProcessId(foregroundWindow, out uint processId);
if (processId != 0)
{
try
{
using Process process = Process.GetProcessById((int)processId);
string processName = process.ProcessName;
if (processName.StartsWith("SquadGame", StringComparison.OrdinalIgnoreCase) ||
processName.Equals("Squad", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
catch (ArgumentException)
{
return false;
}
catch (InvalidOperationException)
{
return false;
}
catch (Win32Exception)
{
return false;
}
}
return false;
}
internal static bool SendMouse(MouseInputFlags flags)
{
Input[] inputs =
[
new Input
{
Type = InputType.Mouse,
Data = new InputUnion { Mouse = new MouseInput { Flags = flags } }
}
];
return SendInputs(inputs);
}
internal static bool SendKey(ushort virtualKey, bool keyUp = false)
{
Input[] inputs =
[
CreateKeyboardInput(virtualKey, keyUp)
];
return SendInputs(inputs);
}
internal static bool SendKeyPress(ushort virtualKey)
{
return SendInputs(
[
CreateKeyboardInput(virtualKey, false),
CreateKeyboardInput(virtualKey, true)
]);
}
internal static bool SendChord(ushort modifier, ushort virtualKey)
{
return SendInputs(
[
CreateKeyboardInput(modifier, false),
CreateKeyboardInput(virtualKey, false),
CreateKeyboardInput(virtualKey, true),
CreateKeyboardInput(modifier, true)
]);
}
internal static string DescribeLastInputError()
{
return lastInputError == 0
? "Windows 未提供具体错误,游戏也可能过滤了合成输入"
: $"Win32 错误 {lastInputError}:{new Win32Exception(lastInputError).Message}";
}
internal static void KeepWindowTopMost(IntPtr windowHandle)
{
const uint noMove = 0x0002;
const uint noSize = 0x0001;
const uint noActivate = 0x0010;
SetWindowPos(windowHandle, new IntPtr(-1), 0, 0, 0, 0, noMove | noSize | noActivate);
}
private static bool SendInputs(Input[] inputs)
{
lastInputError = 0;
uint sent = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<Input>());
if (sent == (uint)inputs.Length)
{
return true;
}
lastInputError = Marshal.GetLastWin32Error();
return false;
}
private static Input CreateKeyboardInput(ushort virtualKey, bool keyUp)
{
return new Input
{
Type = InputType.Keyboard,
Data = new InputUnion
{
Keyboard = new KeyboardInput
{
VirtualKey = 0,
ScanCode = (ushort)MapVirtualKey(virtualKey, MapVkToScanCode),
Flags = KeyboardInputFlags.ScanCode |
(keyUp ? KeyboardInputFlags.KeyUp : KeyboardInputFlags.None)
}
}
};
}
internal enum InputType : uint
{
Mouse = 0,
Keyboard = 1
}
[Flags]
internal enum MouseInputFlags : uint
{
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010
}
[Flags]
internal enum KeyboardInputFlags : uint
{
None = 0,
KeyUp = 0x0002,
ScanCode = 0x0008
}
[StructLayout(LayoutKind.Sequential)]
internal struct Input
{
public InputType Type;
public InputUnion Data;
}
[StructLayout(LayoutKind.Explicit)]
internal struct InputUnion
{
[FieldOffset(0)]
public MouseInput Mouse;
[FieldOffset(0)]
public KeyboardInput Keyboard;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MouseInput
{
public int X;
public int Y;
public uint MouseData;
public MouseInputFlags Flags;
public uint Time;
public UIntPtr ExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct KeyboardInput
{
public ushort VirtualKey;
public ushort ScanCode;
public KeyboardInputFlags Flags;
public uint Time;
public UIntPtr ExtraInfo;
}
[Flags]
internal enum LowLevelKeyboardFlags : uint
{
None = 0,
Injected = 0x00000010
}
[StructLayout(LayoutKind.Sequential)]
internal struct LowLevelKeyboardInput
{
public uint VirtualKey;
public uint ScanCode;
public LowLevelKeyboardFlags Flags;
public uint Time;
public UIntPtr ExtraInfo;
}
}