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> /// Send a key down and hold it down until sendkeyup method is called /// </summary> /// <param name="keyCode"></param> public static void SendKeyDown(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 = 0; 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(); } }
private static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);