static extern UInt32 SendInput(UInt32 numberOfInputs, INPUT[] inputs, Int32 sizeOfInputStructure);
/// <summary> /// Performs mouse move simulation. /// </summary> /// <param name="dX"></param> /// <param name="dY"></param> private static void SimulateMouseMove(int dX, int dY, bool absolutePosition) { var input = new INPUT(); input.Type = (uint)InputType.MOUSE; input.Data.Mouse = new MOUSEINPUT(); input.Data.Mouse.dwFlags = MOUSEEVENTF.MOVE; input.Data.Mouse.mouseData = 0; input.Data.Mouse.dx = dX; input.Data.Mouse.dy = dY; input.Data.Mouse.time = 0; input.Data.Mouse.dwExtraInfo = IntPtr.Zero; if (absolutePosition) { //if(System.Windows.SystemParameters.Screen > 1 double VirtualScreenX = System.Windows.SystemParameters.VirtualScreenWidth; double VirtualScreenY = System.Windows.SystemParameters.VirtualScreenHeight; double PrimaryScreenWidth = System.Windows.SystemParameters.PrimaryScreenWidth; double PrimaryScreenHeight = System.Windows.SystemParameters.PrimaryScreenHeight; double PrimaryScreenLeft = VirtualScreenX - PrimaryScreenWidth; double PrimaryScreenTop = VirtualScreenY - PrimaryScreenHeight; input.Data.Mouse.dx = (int)((VirtualScreenX - PrimaryScreenLeft) * 65535 / PrimaryScreenWidth); input.Data.Mouse.dy = (int)((VirtualScreenY - PrimaryScreenTop) * 65535 / PrimaryScreenHeight); input.Data.Mouse.dwFlags |= MOUSEEVENTF.ABSOLUTE; } INPUT[] inputList = new INPUT[1]; inputList[0] = input; var numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(INPUT))); if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The mouse move simulation was not successful.")); }
/// <summary> /// Calls the Win32 SendInput method with a unicode char to simulate the key up /// </summary> /// <param name="text">The unicode char to simulate</param> public static void SimulateUnicodeCharacterUp(char text) { var up = new INPUT(); up.Type = (UInt32)InputType.KEYBOARD; up.Data.Keyboard = new KEYBDINPUT(); up.Data.Keyboard.Vk = 0; up.Data.Keyboard.Scan = (UInt16)text; up.Data.Keyboard.Flags = (UInt32)(KeyboardFlag.KEYUP | KeyboardFlag.UNICODE); up.Data.Keyboard.Time = 0; up.Data.Keyboard.ExtraInfo = IntPtr.Zero; INPUT[] inputList = new INPUT[1]; inputList[0] = up; var numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(INPUT))); if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The unicode character up simulation for {0} was not successful.", text)); }
/// <summary> /// Calls the Win32 SendInput method with a stream of KeyDown and KeyUp messages in order to simulate /// uninterrupted unicode text entry. /// </summary> /// <param name="text">The text to be simulated.</param> public static void SimulateUninteruptedUnicodeString(string text) { if (text.Length > UInt32.MaxValue / 2) throw new ArgumentException(string.Format("The text parameter is too long. It must be less than {0} characters.", UInt32.MaxValue / 2), "text"); int len = text.Length; INPUT[] inputList = new INPUT[len * 2]; for (int x = 0; x < len; x++) { UInt16 scanCode = text[x]; var down = new INPUT(); down.Type = (UInt32)InputType.KEYBOARD; down.Data.Keyboard = new KEYBDINPUT(); down.Data.Keyboard.Vk = 0; down.Data.Keyboard.Scan = (UInt16)scanCode; down.Data.Keyboard.Flags = (UInt32)KeyboardFlag.UNICODE; down.Data.Keyboard.Time = 0; down.Data.Keyboard.ExtraInfo = IntPtr.Zero; var up = new INPUT(); up.Type = (UInt32)InputType.KEYBOARD; up.Data.Keyboard = new KEYBDINPUT(); up.Data.Keyboard.Vk = 0; up.Data.Keyboard.Scan = (UInt16)scanCode; up.Data.Keyboard.Flags = (UInt32)(KeyboardFlag.KEYUP | KeyboardFlag.UNICODE); up.Data.Keyboard.Time = 0; up.Data.Keyboard.ExtraInfo = IntPtr.Zero; inputList[2 * x] = down; inputList[2 * x + 1] = up; } var numberOfSuccessfulSimulatedInputs = SendInput((UInt32)(len * 2), inputList, Marshal.SizeOf(typeof(INPUT))); if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The unicode string simulation for {0} was not successful.", text)); }
/// <summary> /// Performs Mouse scroll up/down simulation. /// </summary> /// <param name="scroll">The number of times to scroll. Possible value to scroll up; negative to scroll down</param> public static void SimulateMouseWheel(int scroll) { const int WHEEL_DELTA = 120; INPUT input = new INPUT(); input.Type = (uint)InputType.MOUSE; input.Data.Mouse = new MOUSEINPUT(); input.Data.Mouse.dwFlags = MOUSEEVENTF.WHEEL; input.Data.Mouse.mouseData = scroll * WHEEL_DELTA; input.Data.Mouse.dx = 0; input.Data.Mouse.dy = 0; input.Data.Mouse.time = 0; input.Data.Mouse.dwExtraInfo = IntPtr.Zero; INPUT[] inputList = new INPUT[1]; inputList[0] = input; var numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(INPUT))); if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The mouse move simulation was not successful.")); }
/// <summary> /// Performs Mouse button up/down simulation. /// </summary> /// <param name="mouseEvent">The mouse opperation to simulate</param> /// <param name="xButton">When mouseEvent is either XDOWN or XUP, xButton refers to either XBUTTON1 or XBUTTON2</param> public static void SimulateMouseButton(MOUSEEVENTF mouseEvent, XButton xButton = XButton.None) { var input = new INPUT(); input.Type = (uint)InputType.MOUSE; input.Data.Mouse = new MOUSEINPUT(); input.Data.Mouse.dwFlags = mouseEvent; if (mouseEvent == MOUSEEVENTF.XDOWN || mouseEvent == MOUSEEVENTF.XUP) { switch (xButton) { case XButton.XBUTTON1: input.Data.Mouse.mouseData = (int)XButton.XBUTTON1; break; case XButton.XBUTTON2: input.Data.Mouse.mouseData = (int)XButton.XBUTTON2; break; default: input.Data.Mouse.mouseData = 0; break; } } else { input.Data.Mouse.mouseData = 0; } input.Data.Mouse.dx = 0; input.Data.Mouse.dy = 0; input.Data.Mouse.time = 0; input.Data.Mouse.dwExtraInfo = IntPtr.Zero; INPUT[] inputList = new INPUT[1]; inputList[0] = input; var numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(INPUT))); if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The mouse simulation for {0} was not successful.", mouseEvent.ToString())); }
/// <summary> /// Calls the Win32 SendInput method to simulate a Key UP. /// </summary> /// <param name="keyCode">The VirtualKeyCode to lift up</param> public static void SimulateKeyUp(VirtualKey keyCode) { var up = new INPUT(); up.Type = (UInt32)InputType.KEYBOARD; up.Data.Keyboard = new KEYBDINPUT(); up.Data.Keyboard.Vk = (UInt16)keyCode; up.Data.Keyboard.Scan = (ushort)MapVirtualKeyEx((UInt16)keyCode, MapVirtualKeyMapTypes.MAPVK_VK_TO_VSC_EX, IntPtr.Zero); up.Data.Keyboard.Flags = (UInt32)KeyboardFlag.KEYUP; up.Data.Keyboard.Time = 0; up.Data.Keyboard.ExtraInfo = IntPtr.Zero; INPUT[] inputList = new INPUT[1]; inputList[0] = up; var numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(INPUT))); if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The key up simulation for {0} was not successful.", keyCode)); }
/// <summary> /// Calls the Win32 SendInput method to simulate a Key UP. /// </summary> /// <param name="keyCode">The VirtualKeyCode to lift up</param> public static void SimulateKeyUp(VirtualKey keyCode, ushort scancode) { var up = new INPUT(); up.Type = (UInt32)InputType.KEYBOARD; up.Data.Keyboard = new KEYBDINPUT(); up.Data.Keyboard.Vk = (UInt16)keyCode; up.Data.Keyboard.Scan = scancode; up.Data.Keyboard.Flags = (UInt32)KeyboardFlag.KEYUP; if (((scancode >> 8) == 0xe0) || ((scancode >> 8) == 0xe1)) up.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.EXTENDEDKEY; up.Data.Keyboard.Time = 0; up.Data.Keyboard.ExtraInfo = IntPtr.Zero; INPUT[] inputList = new INPUT[1]; inputList[0] = up; var numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(INPUT))); if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The key up simulation for {0} was not successful.", keyCode)); }
/// <summary> /// Calls the Win32 SendInput method with a KeyDown and KeyUp message in the same input sequence in order to simulate a Key PRESS. /// </summary> /// <param name="keyCode">The VirtualKeyCode to press</param> public static void SimulateKeyPress(VirtualKey keyCode) { var down = new INPUT(); down.Type = (UInt32)InputType.KEYBOARD; down.Data.Keyboard = new KEYBDINPUT(); down.Data.Keyboard.Vk = (UInt16)keyCode; down.Data.Keyboard.Scan = 0; down.Data.Keyboard.Flags = 0; down.Data.Keyboard.Time = 0; down.Data.Keyboard.ExtraInfo = IntPtr.Zero; var up = new INPUT(); up.Type = (UInt32)InputType.KEYBOARD; up.Data.Keyboard = new KEYBDINPUT(); up.Data.Keyboard.Vk = (UInt16)keyCode; up.Data.Keyboard.Scan = 0; up.Data.Keyboard.Flags = (UInt32)KeyboardFlag.KEYUP; up.Data.Keyboard.Time = 0; up.Data.Keyboard.ExtraInfo = IntPtr.Zero; INPUT[] inputList = new INPUT[2]; inputList[0] = down; inputList[1] = up; var numberOfSuccessfulSimulatedInputs = SendInput(2, inputList, Marshal.SizeOf(typeof(INPUT))); if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The key press simulation for {0} was not successful.", keyCode)); }