| 176 |
pfowler |
1 |
using System;
|
|
|
2 |
using System.Collections.Generic;
|
|
|
3 |
using System.Linq;
|
|
|
4 |
using System.Runtime.InteropServices;
|
|
|
5 |
using System.Text;
|
|
|
6 |
using System.Threading.Tasks;
|
|
|
7 |
|
|
|
8 |
namespace nitdcscore {
|
|
|
9 |
public class OSInput {
|
|
|
10 |
INPUT[] inputs;
|
|
|
11 |
private bool control = false;
|
|
|
12 |
private VirtualKeyCodes key;
|
|
|
13 |
|
|
|
14 |
public OSInput(VirtualKeyCodes key, bool control = false) {
|
|
|
15 |
this.key = key;
|
|
|
16 |
this.control = control;
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
public void Send() {
|
|
|
20 |
if (control) {
|
|
|
21 |
this.inputs = new INPUT[2];
|
|
|
22 |
inputs[0].type = INPUT_KEYBOARD;
|
|
|
23 |
inputs[0].InputUnion.ki.time = 0;
|
|
|
24 |
inputs[0].InputUnion.ki.wVK = 0;
|
|
|
25 |
inputs[0].InputUnion.ki.dwFlags = KEYEVENTF_SCANCODE;
|
|
|
26 |
inputs[0].InputUnion.ki.wScan = (ushort)MapVirtualKey((uint)VirtualKeyCodes.VK_CONTROL, 0);
|
|
|
27 |
inputs[0].InputUnion.ki.dwExtraInfo = GetMessageExtraInfo();
|
|
|
28 |
|
|
|
29 |
inputs[1].type = INPUT_KEYBOARD;
|
|
|
30 |
inputs[1].InputUnion.ki.time = 0;
|
|
|
31 |
inputs[1].InputUnion.ki.wVK = 0;
|
|
|
32 |
inputs[1].InputUnion.ki.dwFlags = KEYEVENTF_SCANCODE;
|
|
|
33 |
inputs[1].InputUnion.ki.wScan = (ushort)MapVirtualKey((uint)key, 0);
|
|
|
34 |
inputs[1].InputUnion.ki.dwExtraInfo = GetMessageExtraInfo();
|
|
|
35 |
|
|
|
36 |
} else {
|
|
|
37 |
this.inputs = new INPUT[1];
|
|
|
38 |
inputs[1].type = INPUT_KEYBOARD;
|
|
|
39 |
inputs[1].InputUnion.ki.time = 0;
|
|
|
40 |
inputs[1].InputUnion.ki.wVK = 0;
|
|
|
41 |
inputs[1].InputUnion.ki.dwFlags = KEYEVENTF_SCANCODE;
|
|
|
42 |
inputs[1].InputUnion.ki.wScan = (ushort)MapVirtualKey((uint)key, 0);
|
|
|
43 |
inputs[1].InputUnion.ki.dwExtraInfo = GetMessageExtraInfo();
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
SendInput((uint)inputs.Count(), inputs, Marshal.SizeOf(typeof(INPUT)));
|
|
|
48 |
System.Threading.Thread.Sleep(5);
|
|
|
49 |
|
|
|
50 |
for (var i = 0; i < inputs.Count(); i++) {
|
|
|
51 |
inputs[i].InputUnion.ki.dwFlags |= KEYEVENTF_KEYUP;
|
|
|
52 |
}
|
|
|
53 |
Array.Reverse(inputs);
|
|
|
54 |
SendInput((uint)inputs.Count(), inputs, Marshal.SizeOf(typeof(INPUT)));
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public const int INPUT_MOUSE = 0;
|
|
|
58 |
public const int INPUT_KEYBOARD = 1;
|
|
|
59 |
public const int INPUT_HARDWARE = 3;
|
|
|
60 |
public const int KEYEVENTF_KEYUP = 0x0002;
|
|
|
61 |
public const int KEYEVENTF_SCANCODE = 0x0008;
|
|
|
62 |
|
|
|
63 |
[StructLayout(LayoutKind.Sequential)]
|
|
|
64 |
public struct INPUT {
|
|
|
65 |
internal uint type;
|
|
|
66 |
internal InputUnion InputUnion;
|
|
|
67 |
internal static int Size {
|
|
|
68 |
get { return Marshal.SizeOf(typeof(INPUT)); }
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
[StructLayout(LayoutKind.Explicit)]
|
|
|
73 |
internal struct InputUnion {
|
|
|
74 |
[FieldOffset(0)]
|
|
|
75 |
internal MOUSEINPUT mi;
|
|
|
76 |
[FieldOffset(0)]
|
|
|
77 |
internal KEYBDINPUT ki;
|
|
|
78 |
[FieldOffset(0)]
|
|
|
79 |
internal HARDWAREINPUT hi;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
[StructLayout(LayoutKind.Sequential)]
|
|
|
83 |
public struct KEYBDINPUT {
|
|
|
84 |
public ushort wVK;
|
|
|
85 |
public ushort wScan;
|
|
|
86 |
public uint dwFlags;
|
|
|
87 |
public uint time;
|
|
|
88 |
public IntPtr dwExtraInfo;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
[StructLayout(LayoutKind.Sequential)]
|
|
|
92 |
public struct HARDWAREINPUT {
|
|
|
93 |
private uint uMsg;
|
|
|
94 |
private ushort wParamL;
|
|
|
95 |
private ushort wParamH;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
[StructLayout(LayoutKind.Sequential)]
|
|
|
99 |
public struct MOUSEINPUT {
|
|
|
100 |
private int dx;
|
|
|
101 |
private int dy;
|
|
|
102 |
private uint mouseData;
|
|
|
103 |
private uint dwFlags;
|
|
|
104 |
private uint time;
|
|
|
105 |
private IntPtr dwExtraInfo;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
[DllImport("User32.dll", SetLastError = true)]
|
|
|
109 |
public static extern uint SendInput(uint numInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] input, int structSize);
|
|
|
110 |
|
|
|
111 |
[DllImport("User32.dll")]
|
|
|
112 |
public static extern uint MapVirtualKey(uint uCode, uint uMapType);
|
|
|
113 |
|
|
|
114 |
[DllImport("User32.dll")]
|
|
|
115 |
public static extern IntPtr GetMessageExtraInfo();
|
|
|
116 |
|
|
|
117 |
public enum VirtualKeyCodes : ushort {
|
|
|
118 |
VK_BACKSPACE = 0x08,
|
|
|
119 |
VK_TAB = 0x09,
|
|
|
120 |
VK_ENTER = 0x0D,
|
|
|
121 |
VK_SHIFT = 0x10,
|
|
|
122 |
VK_CONTROL = 0x11,
|
|
|
123 |
VK_ALT = 0x12,
|
|
|
124 |
VK_PAUSE = 0x13,
|
|
|
125 |
VK_CAPSLOCK = 0x14,
|
|
|
126 |
VK_ESCAPE = 0x1B,
|
|
|
127 |
VK_SPACE = 0x20,
|
|
|
128 |
VK_PAGEUP = 0x21,
|
|
|
129 |
VK_PAGEDOWN = 0x22,
|
|
|
130 |
VK_END = 0x23,
|
|
|
131 |
VK_HOME = 0x24,
|
|
|
132 |
VK_LEFT = 0x25,
|
|
|
133 |
VK_UP = 0x26,
|
|
|
134 |
VK_RIGHT = 0x27,
|
|
|
135 |
VK_DOWN = 0x28,
|
|
|
136 |
VK_SELECT = 0x29,
|
|
|
137 |
VK_PRINT = 0x2A,
|
|
|
138 |
VK_PRINTSCR = 0x2C,
|
|
|
139 |
VK_INSERT = 0x2D,
|
|
|
140 |
VK_DELETE = 0x2E,
|
|
|
141 |
VK_0 = 0x30,
|
|
|
142 |
VK_1 = 0x31,
|
|
|
143 |
VK_2 = 0x32,
|
|
|
144 |
VK_3 = 0x33,
|
|
|
145 |
VK_4 = 0x34,
|
|
|
146 |
VK_5 = 0x35,
|
|
|
147 |
VK_6 = 0x36,
|
|
|
148 |
VK_7 = 0x37,
|
|
|
149 |
VK_8 = 0x38,
|
|
|
150 |
VK_9 = 0x39,
|
|
|
151 |
VK_F1 = 0x70,
|
|
|
152 |
VK_F2 = 0x71,
|
|
|
153 |
VK_F3 = 0x72,
|
|
|
154 |
VK_F4 = 0x73,
|
|
|
155 |
VK_F5 = 0x74,
|
|
|
156 |
VK_F6 = 0x75,
|
|
|
157 |
VK_F7 = 0x76,
|
|
|
158 |
VK_F8 = 0x77,
|
|
|
159 |
VK_F9 = 0x78,
|
|
|
160 |
VK_F10 = 0x79,
|
|
|
161 |
VK_F11 = 0x7a,
|
|
|
162 |
VK_F12 = 0x7b,
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
}
|