private static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);
public static void SendKeyPress(KeyCode keyCode) { INPUT input = new INPUT { Type = 1 }; input.Data.Keyboard = new KEYBDINPUT() { Vk = (ushort)keyCode, Scan = 0, Flags = 0, Time = 0, ExtraInfo = IntPtr.Zero, }; INPUT input2 = new INPUT { Type = 1 }; input2.Data.Keyboard = new KEYBDINPUT() { Vk = (ushort)keyCode, Scan = 0, Flags = 2, Time = 0, ExtraInfo = IntPtr.Zero }; INPUT[] inputs = new INPUT[] { input, input2 }; if (SendInput(2, inputs, Marshal.SizeOf(typeof(INPUT))) == 0) throw new Exception(); }
/// <summary> /// Release a key that is being hold down /// </summary> /// <param name="keyCode"></param> public static void SendKeyUp(KeyCode keyCode) { INPUT input = new INPUT { Type = 1 }; input.Data.Keyboard = new KEYBDINPUT(); input.Data.Keyboard.Vk = (ushort)keyCode; input.Data.Keyboard.Scan = 0; input.Data.Keyboard.Flags = 2; input.Data.Keyboard.Time = 0; input.Data.Keyboard.ExtraInfo = IntPtr.Zero; INPUT[] inputs = new INPUT[] { input }; if (SendInput(1, inputs, Marshal.SizeOf(typeof(INPUT))) == 0) throw new Exception(); }
public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);