private static void SendMouseInput(ref MouseInput mouseInput) { var iUnion = new InputUnion { mi = mouseInput }; var input = new Input { type = InputTypes.Mouse, U = iUnion }; var inputs = new[] { input }; if (Keyboard.SendInput(1, inputs, Marshal.SizeOf(input)) != 1) throw new Win32Exception(Marshal.GetLastWin32Error()); }
/*public static VirtualKeys[] GetKeysPressed() { return GetVirtualKeyboardState().Select((s, i) => new { State = s, Code = i }).Where(sc => sc.State.Pressed).Select(sc => (VirtualKeys)sc.Code).ToArray(); }*/ /// <summary> /// Stimulate a key press (one couple of key down and and key up) /// </summary> /// <param name="key">The key to stimulate a key press</param> /// <returns>If the function succeeds it returns true; otherwise, returns false</returns> public static bool SendKeyPress(VirtualKeys key) { var kInput1 = new KeyboardInput { wVk = key, dwFlags = 0 /*, time = time/2 */ }; var input1 = new Input { type = InputTypes.Keyboard, U = new InputUnion { ki = kInput1 } }; var kInput2 = new KeyboardInput { wVk = key, dwFlags = KeyEventFlags.KeyUp /*, time = time/2 */ }; var input2 = new Input { type = InputTypes.Keyboard, U = new InputUnion { ki = kInput2 } }; return SendInput(2, new[] { input1, input2 }, Marshal.SizeOf(input1)) == 2; }
static int SendKeyUp(VirtualKeys key) { var kInput = new KeyboardInput { wVk = key, dwFlags = KeyEventFlags.KeyUp }; var input = new Input { type = InputTypes.Keyboard, U = new InputUnion { ki = kInput } }; return SendInput(1, new[] { input }, Marshal.SizeOf(input)); }
/// <summary> /// Stimulate a key combination press /// </summary> /// <param name="keys">The key combination</param> /// <returns>If the function succeeds it returns true; otherwise, returns false</returns> public static bool SendKeyCombination(VirtualKeys[] keys) { var length = keys.Length; if (length > 0) { var doubleLength = 2 * length /*, partTime = time / doubleLength*/; var inputs = new Input[doubleLength]; for (var i = 0; i < length; i++) { var kbInput = new KeyboardInput { wVk = keys[i], dwFlags = 0 /*, time = partTime */ }; inputs[i] = new Input { type = InputTypes.Keyboard, U = new InputUnion { ki = kbInput } }; } for (var i = length - 1; i >= 0; i--) { var kbInput = new KeyboardInput { wVk = keys[i], dwFlags = KeyEventFlags.KeyUp /*, time = partTime */ }; inputs[doubleLength - i - 1] = new Input { type = InputTypes.Keyboard, U = new InputUnion { ki = kbInput } }; } return SendInput(doubleLength, inputs, Marshal.SizeOf(inputs[0])) == doubleLength; } return true; }