Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.21 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. // using UnityEngine;
  7.  
  8.  
  9. class InputTest //: MonoBehaviour
  10. {
  11. [DllImport("user32.dll", SetLastError = true)]
  12. private static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
  13.  
  14. [StructLayout(LayoutKind.Sequential)]
  15. private struct INPUT
  16. {
  17. public SendInputEventType type;
  18. public MouseKeybdhardwareInputUnion mkhi;
  19. }
  20.  
  21. [StructLayout(LayoutKind.Sequential)]
  22. private struct POINT
  23. {
  24. public int X;
  25. public int Y;
  26.  
  27. public POINT(int x, int y)
  28. {
  29. this.X = x;
  30. this.Y = y;
  31. }
  32. }
  33.  
  34. private enum SendInputEventType : int
  35. {
  36. InputMouse,
  37. InputKeyboard,
  38. InputHardware
  39. }
  40.  
  41. [StructLayout(LayoutKind.Explicit)]
  42. private struct MouseKeybdhardwareInputUnion
  43. {
  44. [FieldOffset(0)]
  45. public MouseInputData mi;
  46.  
  47. [FieldOffset(0)]
  48. public KEYBDINPUT ki;
  49.  
  50. [FieldOffset(0)]
  51. public HARDWAREINPUT hi;
  52. }
  53.  
  54. [StructLayout(LayoutKind.Sequential)]
  55. private struct KEYBDINPUT
  56. {
  57. public KeybdEventFlags wVk;//ushort
  58. public ushort wScan;
  59. public uint dwFlags;
  60. public uint time;
  61. public IntPtr dwExtraInfo;
  62. }
  63.  
  64. [StructLayout(LayoutKind.Sequential)]
  65. private struct HARDWAREINPUT
  66. {
  67. public int uMsg;
  68. public short wParamL;
  69. public short wParamH;
  70. }
  71. private struct MouseInputData
  72. {
  73. public int dx;
  74. public int dy;
  75. public int mouseData;
  76. public MouseEventFlags dwFlags;
  77. public uint time;
  78. public IntPtr dwExtraInfo;
  79. }
  80.  
  81. [Flags]
  82. private enum MouseEventFlags : uint
  83. {
  84. MOUSEEVENTF_MOVE = 0x0001,
  85. MOUSEEVENTF_LEFTDOWN = 0x0002,
  86. MOUSEEVENTF_LEFTUP = 0x0004,
  87. MOUSEEVENTF_RIGHTDOWN = 0x0008,
  88. MOUSEEVENTF_RIGHTUP = 0x0010,
  89. MOUSEEVENTF_MIDDLEDOWN = 0x0020,
  90. MOUSEEVENTF_MIDDLEUP = 0x0040,
  91. MOUSEEVENTF_XDOWN = 0x0080,
  92. MOUSEEVENTF_XUP = 0x0100,
  93. MOUSEEVENTF_WHEEL = 0x0800,
  94. MOUSEEVENTF_HWHEEL = 0x01000,
  95. MOUSEEVENTF_VIRTUALDESK = 0x4000,
  96. MOUSEEVENTF_ABSOLUTE = 0x8000
  97. }
  98.  
  99. [Flags]
  100. private enum KeybdEventFlags : uint
  101. {
  102. VK_BACK = 0x0008,
  103. VK_TAB = 0x0009,
  104. VK_CLEAR = 0x000C,
  105. VK_RETURN = 0x000D,
  106. VK_SHIFT = 0x0010,
  107. VK_CONTROL = 0x0011,
  108. VK_MENU = 0x0012,
  109. VK_PAUSE = 0x0013,
  110. VK_CAPITAL = 0x0014, //CAPS LOCK Key
  111. VK_ESCAPE = 0x001B,
  112. VK_SPACE = 0x0020,
  113. VK_PRIOR = 0x0021, //PAGE UP Key
  114. VK_NEXT = 0x0022, //PAGE DOWN Key
  115. VK_END = 0x0023,
  116. VK_HOME = 0x0024,
  117. VK_LEFT = 0x0025, //LEFT ARROW Key
  118. VK_UP = 0x0026, // UP ARROW Key
  119. VK_RIGHT = 0x0027, // RIGHT ARROW Key
  120. VK_DOWN = 0x0028, // DOWN ARROW Key
  121. VK_SELECT = 0x0029,
  122. VK_PRINT = 0x2A,
  123. VK_EXECUTE = 0x002B, //EXECUTE key
  124. VK_SNAPSHOT = 0x002C, //PRINT SCREEN Key
  125. VK_INSERT = 0x002D,
  126. VK_DELETE = 0x002E,
  127. VK_HELP = 0x002F,
  128. VK_0_KEY = 0x0030,
  129. VK_1_KEY = 0x0031,
  130. VK_2_KEY = 0x0032,
  131. VK_3_KEY = 0x0033,
  132. VK_4_KEY = 0x0034,
  133. VK_5_KEY = 0x0035,
  134. VK_6_KEY = 0x0036,
  135. VK_7_KEY = 0x0037,
  136. VK_8_KEY = 0x0038,
  137. VK_9_KEY = 0x0039,
  138. VK_A_KEY = 0x0041,
  139. VK_B_KEY = 0x0042,
  140. VK_C_KEY = 0x0043,
  141. VK_D_KEY = 0x0044,
  142. VK_E_KEY = 0x0045,
  143. VK_F_KEY = 0x0046,
  144. VK_G_KEY = 0x0047,
  145. VK_H_KEY = 0x0048,
  146. VK_I_KEY = 0x0049,
  147. VK_J_KEY = 0x004A,
  148. VK_K_KEY = 0x004B,
  149. VK_L_KEY = 0x004C,
  150. VK_M_KEY = 0x004D,
  151. VK_N_KEY = 0x004E,
  152. VK_O_KEY = 0x004F,
  153. VK_P_KEY = 0x0050,
  154. VK_Q_KEY = 0x0051,
  155. VK_R_KEY = 0x0052,
  156. VK_S_KEY = 0x0053,
  157. VK_T_KEY = 0x0054,
  158. VK_U_KEY = 0x0055,
  159. VK_V_KEY = 0x0056,
  160. VK_W_KEY = 0x0057,
  161. VK_X_KEY = 0x0058,
  162. VK_Y_KEY = 0x0059,
  163. VK_Z_KEY = 0x005A,
  164. VK_LWIN = 0x005B,
  165. VK_RWIN = 0x005C,
  166. VK_APPS = 0x005D,
  167. VK_SLEEP = 0x005F,
  168. VK_NUMPAD0 = 0x0060,
  169. VK_NUMPAD1 = 0x0061,
  170. VK_NUMPAD2 = 0x0062,
  171. VK_NUMPAD3 = 0x0063,
  172. VK_NUMPAD4 = 0x0064,
  173. VK_NUMPAD5 = 0x0065,
  174. VK_NUMPAD6 = 0x0066,
  175. VK_NUMPAD7 = 0x0067,
  176. VK_NUMPAD8 = 0x0068,
  177. VK_NUMPAD9 = 0x0069,
  178. VK_MULTIPLY = 0x006A,
  179. VK_ADD = 0x006B,
  180. VK_SEPARATOR = 0x006C,
  181. VK_SUBTRACT = 0x006D,
  182. VK_DECIMAL = 0x006E,
  183. VK_DIVIDE = 0x006F,
  184. VK_F1 = 0x0070,
  185. VK_F2 = 0x0071,
  186. VK_F3 = 0x0072,
  187. VK_F4 = 0x0073,
  188. VK_F5 = 0x0074,
  189. VK_F6 = 0x0075,
  190. VK_F7 = 0x0076,
  191. VK_F8 = 0x0077,
  192. VK_F9 = 0x0078,
  193. VK_F10 = 0x0079,
  194. VK_F11 = 0x007A,
  195. VK_F12 = 0x007B,
  196. VK_NUMLOCK = 0x0090,
  197. VK_SCROLL = 0x0091,
  198. VK_LSHIFT = 0x00A0,
  199. VK_RSHIFT = 0x00A1,
  200. VK_LCONTROL = 0x00A2,
  201. VK_RCONTROL = 0x00A3,
  202. VK_LMENU = 0x00A4,
  203. VK_RMENU = 0x00A5,
  204. VK_OEM_1 = 0x00BA, //For the US standard keyboard, the ';:' key
  205. VK_OEM_PLUS = 0x00BB,
  206. VK_OEM_COMMA = 0x00BC,
  207. VK_OEM_MINUS = 0x00BD,
  208. VK_OEM_PERIOD = 0x00BE, //For any country/region, the '.' key
  209. VK_OEM_2 = 0x00BF, //For the US standard keyboard, the '/?' key
  210. VK_OEM_3 = 0x00C0, //For the US standard keyboard, the '`~' key
  211. VK_OEM_4 = 0x00DB, //For the US standard keyboard, the '[{' key
  212. VK_OEM_5 = 0x00DC, //For the US standard keyboard, the '\|' key
  213. VK_OEM_6 = 0x00DD, //For the US standard keyboard, the ']}' key
  214. VK_OEM_7 = 0x00DE, //For the US standard keyboard, the 'single-quote/double-quote' key
  215. VK_OEM_8 = 0x00DF // Used for miscellaneous characters; it can vary by keyboard.
  216. }
  217.  
  218. private static void SimulateKeyboardInput(KeybdEventFlags eventFlags, uint keyFlag = 0x0000, ushort scan = 0, int keybdData = 0)
  219. {
  220.  
  221.  
  222. /*
  223. INPUT[] keybdInput = new INPUT[4];
  224.  
  225. keybdInput[0].type = SendInputEventType.InputKeyboard;
  226. keybdInput[0].mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
  227. keybdInput[0].mkhi.ki.dwFlags = 0x0;
  228. keybdInput[0].mkhi.ki.wScan = 0;
  229. keybdInput[0].mkhi.ki.dwExtraInfo = IntPtr.Zero;
  230.  
  231. keybdInput[1].type = SendInputEventType.InputKeyboard;
  232. keybdInput[1].mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
  233. keybdInput[1].mkhi.ki.dwFlags = 0x0;
  234. keybdInput[1].mkhi.ki.wScan = 0;
  235. keybdInput[1].mkhi.ki.dwExtraInfo = IntPtr.Zero;
  236.  
  237. keybdInput[2].type = SendInputEventType.InputKeyboard;
  238. keybdInput[2].mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
  239. keybdInput[2].mkhi.ki.dwFlags = 0x0;
  240. keybdInput[2].mkhi.ki.wScan = 0;
  241. keybdInput[2].mkhi.ki.dwExtraInfo = IntPtr.Zero;
  242.  
  243. keybdInput[3].type = SendInputEventType.InputKeyboard;
  244. keybdInput[3].mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
  245. keybdInput[3].mkhi.ki.dwFlags = 0x0;
  246. keybdInput[3].mkhi.ki.wScan = 0;
  247. keybdInput[3].mkhi.ki.dwExtraInfo = IntPtr.Zero;
  248.  
  249. SendInput(4, ref keybdInput, Marshal.SizeOf(keybdInput));
  250. //
  251.  
  252. SimulateKeybdInput3(KeybdEventFlags.VK_LSHIFT, 0, 0, 0);
  253. SimulateKeybdInput3(KeybdEventFlags.VK_U_KEY, KEYEVENTF_KEYUP, 0, 0);
  254. SimulateKeybdInput3(KeybdEventFlags.VK_LSHIFT, KEYEVENTF_KEYUP, 0, 0);
  255. */
  256. }
  257.  
  258. public static void SimulateButtonPress()
  259. {
  260.  
  261. INPUT keybdInput = new INPUT();
  262.  
  263. keybdInput.type = SendInputEventType.InputKeyboard;
  264. //keybdInput.mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
  265. keybdInput.mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
  266. keybdInput.mkhi.ki.dwFlags = 0x0008;
  267. //keybdInput.mkhi.ki.wScan = 0x2A;
  268. keybdInput.mkhi.ki.dwExtraInfo = IntPtr.Zero;
  269.  
  270. SendInput(1, ref keybdInput, Marshal.SizeOf(keybdInput));
  271. Console.WriteLine("Shift ws pressed");
  272. //Debug.Log("Shift ws pressed");
  273.  
  274. INPUT keybdInput1 = new INPUT();
  275.  
  276. keybdInput1.type = SendInputEventType.InputKeyboard;
  277. keybdInput1.mkhi.ki.wVk = KeybdEventFlags.VK_F_KEY;
  278. keybdInput1.mkhi.ki.dwFlags = 0x0008;
  279. keybdInput1.mkhi.ki.wScan = 0x21;
  280. keybdInput1.mkhi.ki.dwExtraInfo = IntPtr.Zero;
  281.  
  282. SendInput(1, ref keybdInput1, Marshal.SizeOf(keybdInput1));
  283. Console.WriteLine("F ws pressed");
  284. // Debug.Log("F was pressed");
  285.  
  286. INPUT keybdInput3 = new INPUT();
  287.  
  288. keybdInput3.type = SendInputEventType.InputKeyboard;
  289. keybdInput3.mkhi.ki.wVk = KeybdEventFlags.VK_F_KEY;
  290. keybdInput3.mkhi.ki.dwFlags = 0x0002 | 0x0008;
  291. keybdInput3.mkhi.ki.wScan = 0x21;
  292. keybdInput3.mkhi.ki.dwExtraInfo = IntPtr.Zero;
  293.  
  294. SendInput(1, ref keybdInput3, Marshal.SizeOf(keybdInput3));
  295. Console.WriteLine("f ws unpressed");
  296. //Debug.Log("F was un pressed");
  297.  
  298. INPUT keybdInput2 = new INPUT();
  299.  
  300. keybdInput2.type = SendInputEventType.InputKeyboard;
  301. keybdInput2.mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
  302. //keybdInput2.mkhi.ki.wVk = KeybdEventFlags.VK_P_KEY;
  303. keybdInput2.mkhi.ki.dwFlags = 0x0002 | 0x0008;
  304. //keybdInput2.mkhi.ki.wScan = 0x2A;
  305. keybdInput2.mkhi.ki.dwExtraInfo = IntPtr.Zero;
  306.  
  307. SendInput(1, ref keybdInput2, Marshal.SizeOf(keybdInput2));
  308. Console.WriteLine("Shift ws unpressed");
  309. // Debug.Log("Shift was un presed");
  310.  
  311. INPUT keybdInput5 = new INPUT();
  312.  
  313. keybdInput5.type = SendInputEventType.InputKeyboard;
  314. keybdInput5.mkhi.ki.wVk = KeybdEventFlags.VK_Q_KEY;
  315. keybdInput5.mkhi.ki.dwFlags = 0x0002;
  316. keybdInput5.mkhi.ki.wScan = 0x10;
  317. keybdInput5.mkhi.ki.dwExtraInfo = IntPtr.Zero;
  318.  
  319. SendInput(1, ref keybdInput5, Marshal.SizeOf(keybdInput5));
  320. Console.WriteLine("Q ws pressed");
  321. INPUT keybdInput6 = new INPUT();
  322.  
  323. keybdInput6.type = SendInputEventType.InputKeyboard;
  324. keybdInput6.mkhi.ki.wVk = KeybdEventFlags.VK_Q_KEY;
  325. keybdInput6.mkhi.ki.dwFlags = 0x0002 | 0x0008;
  326. keybdInput6.mkhi.ki.wScan = 0x10;
  327. keybdInput6.mkhi.ki.dwExtraInfo = IntPtr.Zero;
  328.  
  329. SendInput(1, ref keybdInput6, Marshal.SizeOf(keybdInput6));
  330. Console.WriteLine("Q ws unpressed");
  331.  
  332. //
  333. //
  334. //
  335. //
  336. //
  337. //
  338. //
  339. }
  340. }
  341.  
  342.  
  343. class Program
  344. {
  345. static void Main(string[] args)
  346. {
  347. for(int i=0; i<10; ++i)
  348. {
  349. InputTest.SimulateButtonPress();
  350.  
  351. Console.ReadLine();
  352. Console.WriteLine("\n");
  353. }
  354. }
  355. }
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement