Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- // using UnityEngine;
- class InputTest //: MonoBehaviour
- {
- [DllImport("user32.dll", SetLastError = true)]
- private static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
- [StructLayout(LayoutKind.Sequential)]
- private struct INPUT
- {
- public SendInputEventType type;
- public MouseKeybdhardwareInputUnion mkhi;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct POINT
- {
- public int X;
- public int Y;
- public POINT(int x, int y)
- {
- this.X = x;
- this.Y = y;
- }
- }
- private enum SendInputEventType : int
- {
- InputMouse,
- InputKeyboard,
- InputHardware
- }
- [StructLayout(LayoutKind.Explicit)]
- private struct MouseKeybdhardwareInputUnion
- {
- [FieldOffset(0)]
- public MouseInputData mi;
- [FieldOffset(0)]
- public KEYBDINPUT ki;
- [FieldOffset(0)]
- public HARDWAREINPUT hi;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct KEYBDINPUT
- {
- public KeybdEventFlags wVk;//ushort
- public ushort wScan;
- public uint dwFlags;
- public uint time;
- public IntPtr dwExtraInfo;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct HARDWAREINPUT
- {
- public int uMsg;
- public short wParamL;
- public short wParamH;
- }
- private struct MouseInputData
- {
- public int dx;
- public int dy;
- public int mouseData;
- public MouseEventFlags dwFlags;
- public uint time;
- public IntPtr dwExtraInfo;
- }
- [Flags]
- private enum MouseEventFlags : uint
- {
- MOUSEEVENTF_MOVE = 0x0001,
- MOUSEEVENTF_LEFTDOWN = 0x0002,
- MOUSEEVENTF_LEFTUP = 0x0004,
- MOUSEEVENTF_RIGHTDOWN = 0x0008,
- MOUSEEVENTF_RIGHTUP = 0x0010,
- MOUSEEVENTF_MIDDLEDOWN = 0x0020,
- MOUSEEVENTF_MIDDLEUP = 0x0040,
- MOUSEEVENTF_XDOWN = 0x0080,
- MOUSEEVENTF_XUP = 0x0100,
- MOUSEEVENTF_WHEEL = 0x0800,
- MOUSEEVENTF_HWHEEL = 0x01000,
- MOUSEEVENTF_VIRTUALDESK = 0x4000,
- MOUSEEVENTF_ABSOLUTE = 0x8000
- }
- [Flags]
- private enum KeybdEventFlags : uint
- {
- VK_BACK = 0x0008,
- VK_TAB = 0x0009,
- VK_CLEAR = 0x000C,
- VK_RETURN = 0x000D,
- VK_SHIFT = 0x0010,
- VK_CONTROL = 0x0011,
- VK_MENU = 0x0012,
- VK_PAUSE = 0x0013,
- VK_CAPITAL = 0x0014, //CAPS LOCK Key
- VK_ESCAPE = 0x001B,
- VK_SPACE = 0x0020,
- VK_PRIOR = 0x0021, //PAGE UP Key
- VK_NEXT = 0x0022, //PAGE DOWN Key
- VK_END = 0x0023,
- VK_HOME = 0x0024,
- VK_LEFT = 0x0025, //LEFT ARROW Key
- VK_UP = 0x0026, // UP ARROW Key
- VK_RIGHT = 0x0027, // RIGHT ARROW Key
- VK_DOWN = 0x0028, // DOWN ARROW Key
- VK_SELECT = 0x0029,
- VK_PRINT = 0x2A,
- VK_EXECUTE = 0x002B, //EXECUTE key
- VK_SNAPSHOT = 0x002C, //PRINT SCREEN Key
- VK_INSERT = 0x002D,
- VK_DELETE = 0x002E,
- VK_HELP = 0x002F,
- VK_0_KEY = 0x0030,
- VK_1_KEY = 0x0031,
- VK_2_KEY = 0x0032,
- VK_3_KEY = 0x0033,
- VK_4_KEY = 0x0034,
- VK_5_KEY = 0x0035,
- VK_6_KEY = 0x0036,
- VK_7_KEY = 0x0037,
- VK_8_KEY = 0x0038,
- VK_9_KEY = 0x0039,
- VK_A_KEY = 0x0041,
- VK_B_KEY = 0x0042,
- VK_C_KEY = 0x0043,
- VK_D_KEY = 0x0044,
- VK_E_KEY = 0x0045,
- VK_F_KEY = 0x0046,
- VK_G_KEY = 0x0047,
- VK_H_KEY = 0x0048,
- VK_I_KEY = 0x0049,
- VK_J_KEY = 0x004A,
- VK_K_KEY = 0x004B,
- VK_L_KEY = 0x004C,
- VK_M_KEY = 0x004D,
- VK_N_KEY = 0x004E,
- VK_O_KEY = 0x004F,
- VK_P_KEY = 0x0050,
- VK_Q_KEY = 0x0051,
- VK_R_KEY = 0x0052,
- VK_S_KEY = 0x0053,
- VK_T_KEY = 0x0054,
- VK_U_KEY = 0x0055,
- VK_V_KEY = 0x0056,
- VK_W_KEY = 0x0057,
- VK_X_KEY = 0x0058,
- VK_Y_KEY = 0x0059,
- VK_Z_KEY = 0x005A,
- VK_LWIN = 0x005B,
- VK_RWIN = 0x005C,
- VK_APPS = 0x005D,
- VK_SLEEP = 0x005F,
- VK_NUMPAD0 = 0x0060,
- VK_NUMPAD1 = 0x0061,
- VK_NUMPAD2 = 0x0062,
- VK_NUMPAD3 = 0x0063,
- VK_NUMPAD4 = 0x0064,
- VK_NUMPAD5 = 0x0065,
- VK_NUMPAD6 = 0x0066,
- VK_NUMPAD7 = 0x0067,
- VK_NUMPAD8 = 0x0068,
- VK_NUMPAD9 = 0x0069,
- VK_MULTIPLY = 0x006A,
- VK_ADD = 0x006B,
- VK_SEPARATOR = 0x006C,
- VK_SUBTRACT = 0x006D,
- VK_DECIMAL = 0x006E,
- VK_DIVIDE = 0x006F,
- VK_F1 = 0x0070,
- VK_F2 = 0x0071,
- VK_F3 = 0x0072,
- VK_F4 = 0x0073,
- VK_F5 = 0x0074,
- VK_F6 = 0x0075,
- VK_F7 = 0x0076,
- VK_F8 = 0x0077,
- VK_F9 = 0x0078,
- VK_F10 = 0x0079,
- VK_F11 = 0x007A,
- VK_F12 = 0x007B,
- VK_NUMLOCK = 0x0090,
- VK_SCROLL = 0x0091,
- VK_LSHIFT = 0x00A0,
- VK_RSHIFT = 0x00A1,
- VK_LCONTROL = 0x00A2,
- VK_RCONTROL = 0x00A3,
- VK_LMENU = 0x00A4,
- VK_RMENU = 0x00A5,
- VK_OEM_1 = 0x00BA, //For the US standard keyboard, the ';:' key
- VK_OEM_PLUS = 0x00BB,
- VK_OEM_COMMA = 0x00BC,
- VK_OEM_MINUS = 0x00BD,
- VK_OEM_PERIOD = 0x00BE, //For any country/region, the '.' key
- VK_OEM_2 = 0x00BF, //For the US standard keyboard, the '/?' key
- VK_OEM_3 = 0x00C0, //For the US standard keyboard, the '`~' key
- VK_OEM_4 = 0x00DB, //For the US standard keyboard, the '[{' key
- VK_OEM_5 = 0x00DC, //For the US standard keyboard, the '\|' key
- VK_OEM_6 = 0x00DD, //For the US standard keyboard, the ']}' key
- VK_OEM_7 = 0x00DE, //For the US standard keyboard, the 'single-quote/double-quote' key
- VK_OEM_8 = 0x00DF // Used for miscellaneous characters; it can vary by keyboard.
- }
- private static void SimulateKeyboardInput(KeybdEventFlags eventFlags, uint keyFlag = 0x0000, ushort scan = 0, int keybdData = 0)
- {
- /*
- INPUT[] keybdInput = new INPUT[4];
- keybdInput[0].type = SendInputEventType.InputKeyboard;
- keybdInput[0].mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
- keybdInput[0].mkhi.ki.dwFlags = 0x0;
- keybdInput[0].mkhi.ki.wScan = 0;
- keybdInput[0].mkhi.ki.dwExtraInfo = IntPtr.Zero;
- keybdInput[1].type = SendInputEventType.InputKeyboard;
- keybdInput[1].mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
- keybdInput[1].mkhi.ki.dwFlags = 0x0;
- keybdInput[1].mkhi.ki.wScan = 0;
- keybdInput[1].mkhi.ki.dwExtraInfo = IntPtr.Zero;
- keybdInput[2].type = SendInputEventType.InputKeyboard;
- keybdInput[2].mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
- keybdInput[2].mkhi.ki.dwFlags = 0x0;
- keybdInput[2].mkhi.ki.wScan = 0;
- keybdInput[2].mkhi.ki.dwExtraInfo = IntPtr.Zero;
- keybdInput[3].type = SendInputEventType.InputKeyboard;
- keybdInput[3].mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
- keybdInput[3].mkhi.ki.dwFlags = 0x0;
- keybdInput[3].mkhi.ki.wScan = 0;
- keybdInput[3].mkhi.ki.dwExtraInfo = IntPtr.Zero;
- SendInput(4, ref keybdInput, Marshal.SizeOf(keybdInput));
- //
- SimulateKeybdInput3(KeybdEventFlags.VK_LSHIFT, 0, 0, 0);
- SimulateKeybdInput3(KeybdEventFlags.VK_U_KEY, KEYEVENTF_KEYUP, 0, 0);
- SimulateKeybdInput3(KeybdEventFlags.VK_LSHIFT, KEYEVENTF_KEYUP, 0, 0);
- */
- }
- public static void SimulateButtonPress()
- {
- INPUT keybdInput = new INPUT();
- keybdInput.type = SendInputEventType.InputKeyboard;
- //keybdInput.mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
- keybdInput.mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
- keybdInput.mkhi.ki.dwFlags = 0x0008;
- //keybdInput.mkhi.ki.wScan = 0x2A;
- keybdInput.mkhi.ki.dwExtraInfo = IntPtr.Zero;
- SendInput(1, ref keybdInput, Marshal.SizeOf(keybdInput));
- Console.WriteLine("Shift ws pressed");
- //Debug.Log("Shift ws pressed");
- INPUT keybdInput1 = new INPUT();
- keybdInput1.type = SendInputEventType.InputKeyboard;
- keybdInput1.mkhi.ki.wVk = KeybdEventFlags.VK_F_KEY;
- keybdInput1.mkhi.ki.dwFlags = 0x0008;
- keybdInput1.mkhi.ki.wScan = 0x21;
- keybdInput1.mkhi.ki.dwExtraInfo = IntPtr.Zero;
- SendInput(1, ref keybdInput1, Marshal.SizeOf(keybdInput1));
- Console.WriteLine("F ws pressed");
- // Debug.Log("F was pressed");
- INPUT keybdInput3 = new INPUT();
- keybdInput3.type = SendInputEventType.InputKeyboard;
- keybdInput3.mkhi.ki.wVk = KeybdEventFlags.VK_F_KEY;
- keybdInput3.mkhi.ki.dwFlags = 0x0002 | 0x0008;
- keybdInput3.mkhi.ki.wScan = 0x21;
- keybdInput3.mkhi.ki.dwExtraInfo = IntPtr.Zero;
- SendInput(1, ref keybdInput3, Marshal.SizeOf(keybdInput3));
- Console.WriteLine("f ws unpressed");
- //Debug.Log("F was un pressed");
- INPUT keybdInput2 = new INPUT();
- keybdInput2.type = SendInputEventType.InputKeyboard;
- keybdInput2.mkhi.ki.wVk = KeybdEventFlags.VK_SHIFT;
- //keybdInput2.mkhi.ki.wVk = KeybdEventFlags.VK_P_KEY;
- keybdInput2.mkhi.ki.dwFlags = 0x0002 | 0x0008;
- //keybdInput2.mkhi.ki.wScan = 0x2A;
- keybdInput2.mkhi.ki.dwExtraInfo = IntPtr.Zero;
- SendInput(1, ref keybdInput2, Marshal.SizeOf(keybdInput2));
- Console.WriteLine("Shift ws unpressed");
- // Debug.Log("Shift was un presed");
- INPUT keybdInput5 = new INPUT();
- keybdInput5.type = SendInputEventType.InputKeyboard;
- keybdInput5.mkhi.ki.wVk = KeybdEventFlags.VK_Q_KEY;
- keybdInput5.mkhi.ki.dwFlags = 0x0002;
- keybdInput5.mkhi.ki.wScan = 0x10;
- keybdInput5.mkhi.ki.dwExtraInfo = IntPtr.Zero;
- SendInput(1, ref keybdInput5, Marshal.SizeOf(keybdInput5));
- Console.WriteLine("Q ws pressed");
- INPUT keybdInput6 = new INPUT();
- keybdInput6.type = SendInputEventType.InputKeyboard;
- keybdInput6.mkhi.ki.wVk = KeybdEventFlags.VK_Q_KEY;
- keybdInput6.mkhi.ki.dwFlags = 0x0002 | 0x0008;
- keybdInput6.mkhi.ki.wScan = 0x10;
- keybdInput6.mkhi.ki.dwExtraInfo = IntPtr.Zero;
- SendInput(1, ref keybdInput6, Marshal.SizeOf(keybdInput6));
- Console.WriteLine("Q ws unpressed");
- //
- //
- //
- //
- //
- //
- //
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- for(int i=0; i<10; ++i)
- {
- InputTest.SimulateButtonPress();
- Console.ReadLine();
- Console.WriteLine("\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement