-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
137 lines (116 loc) · 3.6 KB
/
Copy pathNativeMethods.cs
File metadata and controls
137 lines (116 loc) · 3.6 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
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace SquadTools;
internal static class NativeMethods
{
internal const int WmHotKey = 0x0312;
internal const int VkF9 = 0x78;
internal const int VkOem3 = 0xC0;
internal const int VkControl = 0x11;
internal const int VkV = 0x56;
internal const int VkReturn = 0x0D;
[DllImport("user32.dll")]
internal static extern short GetAsyncKeyState(int virtualKey);
[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", CharSet = CharSet.Unicode)]
internal static extern int GetWindowText(IntPtr windowHandle, StringBuilder text, int maxCount);
internal static bool IsSquadForeground()
{
StringBuilder title = new(512);
GetWindowText(GetForegroundWindow(), title, title.Capacity);
return title.ToString().Contains("Squad", StringComparison.OrdinalIgnoreCase);
}
internal static bool SendMouse(MouseInputFlags flags)
{
Input[] inputs =
[
new Input
{
Type = InputType.Mouse,
Data = new InputUnion { Mouse = new MouseInput { Flags = flags } }
}
];
return SendInput(1, inputs, Marshal.SizeOf<Input>()) == 1;
}
internal static bool SendKey(ushort virtualKey, bool keyUp = false)
{
Input[] inputs =
[
new Input
{
Type = InputType.Keyboard,
Data = new InputUnion
{
Keyboard = new KeyboardInput
{
VirtualKey = virtualKey,
Flags = keyUp ? KeyboardInputFlags.KeyUp : KeyboardInputFlags.None
}
}
}
];
return SendInput(1, inputs, Marshal.SizeOf<Input>()) == 1;
}
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
}
[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;
}
}