1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Text; |
4 |
using System.Runtime.InteropServices; |
5 |
using System.Threading; |
6 |
using System.Windows.Input; |
7 |
|
8 |
|
9 |
namespace libWin32.Win32.Input |
10 |
{ |
11 |
public class InputSender |
12 |
{ |
13 |
|
14 |
public const int KEYUP_WAIT_TIME = 0; |
15 |
|
16 |
#region INPUTTYPE CONSTANTS |
17 |
public const uint INPUT_MOUSE = 0x0; |
18 |
public const int INPUT_KEYBOARD = 0x1; |
19 |
public const int INPUT_HARDWARE = 0x2; |
20 |
#endregion |
21 |
#region KEYEVENTF CONSTANTS |
22 |
public const uint KEYEVENTF_EXTENDEDKEY = 0x1; |
23 |
public const uint KEYEVENTF_KEYDOWN = 0x0; |
24 |
public const uint KEYEVENTF_KEYUP = 0x2; |
25 |
public const uint KEYEVENTF_UNICODE = 0x4; |
26 |
public const uint KEYEVENTF_SCANCODE = 0x8; |
27 |
#endregion |
28 |
#region XBUTTON CONSTANTS |
29 |
public const uint XBUTTON1 = 0x1; |
30 |
public const uint XBUTTON2 = 0x2; |
31 |
#endregion |
32 |
#region MOUSEEVENTF CONSTANTS |
33 |
public const uint MOUSEEVENTF_MOVE = 0x1; |
34 |
public const uint MOUSEEVENTF_LEFTDOWN = 0x2; |
35 |
public const uint MOUSEEVENTF_LEFTUP = 0x4; |
36 |
public const uint MOUSEEVENTF_RIGHTDOWN = 0x8; |
37 |
public const uint MOUSEEVENTF_RIGHTUP = 0x10; |
38 |
public const uint MOUSEEVENTF_MIDDLEDOWN = 0x20; |
39 |
public const uint MOUSEEVENTF_MIDDLEUP = 0x40; |
40 |
public const uint MOUSEEVENTF_XDOWN = 0x80; |
41 |
public const uint MOUSEEVENTF_XUP = 0x100; |
42 |
public const uint MOUSEEVENTF_WHEEL = 0x800; |
43 |
public const uint MOUSEEVENTF_VIRTUALDESK = 0x4000; |
44 |
public const uint MOUSEEVENTF_ABSOLUTE = 0x8000; |
45 |
#endregion |
46 |
|
47 |
[StructLayout(LayoutKind.Explicit)] |
48 |
struct INPUT |
49 |
{ |
50 |
[FieldOffset(0)] |
51 |
public int dwType; |
52 |
[FieldOffset(4)] |
53 |
public MOUSEINPUT mouseInput; |
54 |
[FieldOffset(4)] |
55 |
public KEYBDINPUT keyboardInput; |
56 |
[FieldOffset(4)] |
57 |
public HARDWAREINPUT hardwareInput; |
58 |
} |
59 |
[StructLayout(LayoutKind.Explicit)] |
60 |
struct KEYBDINPUT |
61 |
{ |
62 |
[FieldOffset(0)] |
63 |
public short wVk; |
64 |
[FieldOffset(2)] |
65 |
public short wScan; |
66 |
[FieldOffset(4)] |
67 |
public uint dwFlags; |
68 |
[FieldOffset(8)] |
69 |
public int time; |
70 |
[FieldOffset(12)] |
71 |
public IntPtr dwExtraInfo; |
72 |
} |
73 |
[StructLayout(LayoutKind.Explicit)] |
74 |
struct HARDWAREINPUT |
75 |
{ |
76 |
[FieldOffset(0)] |
77 |
public int uMsg; |
78 |
[FieldOffset(4)] |
79 |
public short wParamL; |
80 |
[FieldOffset(6)] |
81 |
public short wParamH; |
82 |
} |
83 |
/// <summary> |
84 |
/// The mouse data structure |
85 |
/// </summary> |
86 |
[StructLayout(LayoutKind.Explicit)] |
87 |
struct MOUSEINPUT |
88 |
{ |
89 |
[FieldOffset(0)] |
90 |
public int dx; |
91 |
[FieldOffset(4)] |
92 |
public int dy; |
93 |
[FieldOffset(8)] |
94 |
public int mouseData; |
95 |
[FieldOffset(12)] |
96 |
public int dwFlags; |
97 |
[FieldOffset(16)] |
98 |
public int time; |
99 |
[FieldOffset(20)] |
100 |
public IntPtr dwExtraInfo; |
101 |
} |
102 |
|
103 |
[DllImport("user32.dll")] |
104 |
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); |
105 |
[DllImport("user32.dll", SetLastError = true)] |
106 |
static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize); |
107 |
[DllImport("user32.dll")] |
108 |
static extern bool BlockInput(bool fBlockIt); |
109 |
|
110 |
public void BlockUserInput() { BlockInput(true); } |
111 |
public void UnBlockUserInput() { BlockInput(false); } |
112 |
|
113 |
|
114 |
public static bool isExtendedVKKey(Key VKKey) |
115 |
{ |
116 |
if (VKKey == Key.Insert || VKKey == Key.Left || VKKey == Key.Home || VKKey == Key.End || |
117 |
VKKey == Key.Up || VKKey == Key.Down || VKKey == Key.PageUp || VKKey == Key.PageDown || VKKey == Key.Right) |
118 |
{ |
119 |
return true; |
120 |
} |
121 |
return false; |
122 |
|
123 |
} |
124 |
public static bool isExtendedScanCode(short ScanCode) |
125 |
{ |
126 |
DXKeysToScanCodes t = (DXKeysToScanCodes)ScanCode; |
127 |
if (ScanCode == 0) |
128 |
t = DXKeysToScanCodes.DIK_UNKOWN; |
129 |
if (t == DXKeysToScanCodes.DIK_UP || |
130 |
t == DXKeysToScanCodes.DIK_DOWN || |
131 |
t == DXKeysToScanCodes.DIK_LEFT || |
132 |
t == DXKeysToScanCodes.DIK_RIGHT || |
133 |
t == DXKeysToScanCodes.DIK_HOME || |
134 |
t == DXKeysToScanCodes.DIK_END || |
135 |
t == DXKeysToScanCodes.DIK_NUMPADENTER || |
136 |
t == DXKeysToScanCodes.DIK_RCONTROL || |
137 |
t == DXKeysToScanCodes.DIK_RMENU || |
138 |
t == DXKeysToScanCodes.DIK_LEFT || |
139 |
t == DXKeysToScanCodes.DIK_PRIOR || |
140 |
t == DXKeysToScanCodes.DIK_NEXT || |
141 |
t == DXKeysToScanCodes.DIK_INSERT || |
142 |
t == DXKeysToScanCodes.DIK_DELETE) |
143 |
{ |
144 |
return true; |
145 |
} |
146 |
return false; |
147 |
} |
148 |
static void DXKeyDown(short DXScanCode) |
149 |
{ |
150 |
INPUT inputs = new INPUT(); |
151 |
inputs.dwType = INPUT_KEYBOARD; |
152 |
|
153 |
if (isExtendedScanCode(DXScanCode)) |
154 |
{ |
155 |
inputs.keyboardInput.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYDOWN | KEYEVENTF_EXTENDEDKEY; |
156 |
} |
157 |
else |
158 |
{ |
159 |
inputs.keyboardInput.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYDOWN; |
160 |
} |
161 |
|
162 |
|
163 |
inputs.keyboardInput.wScan = DXScanCode; |
164 |
int cbSize = Marshal.SizeOf(typeof(INPUT)); |
165 |
SendInput(1, ref inputs, cbSize); |
166 |
} |
167 |
static void DXKeyUp(short DXScanCode, TimeSpan wait) |
168 |
{ |
169 |
if (wait.TotalMilliseconds > 0) |
170 |
Thread.Sleep(wait); |
171 |
|
172 |
INPUT inputs = new INPUT(); |
173 |
inputs.dwType = INPUT_KEYBOARD; |
174 |
if (isExtendedScanCode(DXScanCode)) |
175 |
{ |
176 |
inputs.keyboardInput.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY; |
177 |
} |
178 |
else |
179 |
{ |
180 |
inputs.keyboardInput.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; |
181 |
} |
182 |
inputs.keyboardInput.wScan = DXScanCode; |
183 |
int cbSize = Marshal.SizeOf(typeof(INPUT)); |
184 |
SendInput(1, ref inputs, cbSize); |
185 |
} |
186 |
|
187 |
public static void SendDXKey(short DXKey) |
188 |
{ |
189 |
PressDXKey(DXKey); |
190 |
} |
191 |
public static void SendDXKey(List<short> DXKeys) |
192 |
{ |
193 |
foreach (short DXKey in DXKeys) |
194 |
{ |
195 |
PressDXKey(DXKey); |
196 |
} |
197 |
} |
198 |
public static void PressDXKey(short DXKey) |
199 |
{ |
200 |
DXKeyDown((short)DXKey); |
201 |
DXKeyUp((short)DXKey, TimeSpan.FromMilliseconds(250)); |
202 |
} |
203 |
public static void PressDXKey(List<short> DXKeys) |
204 |
{ |
205 |
foreach (short DXKey in DXKeys) |
206 |
{ |
207 |
DXKeyDown((short)DXKey); |
208 |
DXKeyUp((short)DXKey, TimeSpan.FromMilliseconds(250)); |
209 |
} |
210 |
} |
211 |
|
212 |
public static void PressDXKey_NoRelease(short DXKey) |
213 |
{ |
214 |
DXKeyDown((short)DXKey); |
215 |
//DXKeyUp((short)DXKey, TimeSpan.FromMilliseconds(KEYUP_WAIT_TIME)); |
216 |
} |
217 |
public static void PressDXKey_NoRelease(List<short> DXKeys) |
218 |
{ |
219 |
foreach (short DXKey in DXKeys) |
220 |
{ |
221 |
DXKeyDown((short)DXKey); |
222 |
//DXKeyUp((short)DXKey, TimeSpan.FromMilliseconds(KEYUP_WAIT_TIME)); |
223 |
} |
224 |
} |
225 |
public static void ReleaseDXKey(short DXKey) |
226 |
{ |
227 |
//DXKeyDown((short)DXKey); |
228 |
DXKeyUp((short)DXKey, TimeSpan.FromMilliseconds(KEYUP_WAIT_TIME)); |
229 |
} |
230 |
public static void ReleaseDXKey(List<short> DXKeys) |
231 |
{ |
232 |
foreach (short DXKey in DXKeys) |
233 |
{ |
234 |
//DXKeyDown((short)DXKey); |
235 |
DXKeyUp((short)DXKey, TimeSpan.FromMilliseconds(KEYUP_WAIT_TIME)); |
236 |
} |
237 |
} |
238 |
} |
239 |
} |