private void PressOrReleaseKey(VirtualKeyShort keyCode, bool down) { var ki = new NativeMethods.KEYBDINPUT(); ki.wVk = keyCode; if (!down) ki.dwFlags = NativeMethods.KEYEVENTF.KEYUP; var input = new NativeMethods.INPUT(); input.type = NativeMethods.INPUT_KEYBOARD; input.U.ki = ki; NativeMethods.INPUT[] inputs = { input }; if (NativeMethods.SendInput((uint)inputs.Length, inputs, NativeMethods.INPUT.Size) == 0) throw new System.ComponentModel.Win32Exception(); }
public void WriteText(string characters) { NativeMethods.INPUT[] inputs = new NativeMethods.INPUT[2 * characters.Length]; for (int i = 0; i < inputs.Length; i++) { var ki = new NativeMethods.KEYBDINPUT(); ki.dwFlags = NativeMethods.KEYEVENTF.UNICODE; if (i % 2 == 1) ki.dwFlags |= NativeMethods.KEYEVENTF.KEYUP; ki.wScan = (short)characters[i / 2]; var input = new NativeMethods.INPUT(); input.type = NativeMethods.INPUT_KEYBOARD; input.U.ki = ki; inputs[i] = input; } if (NativeMethods.SendInput((uint)inputs.Length, inputs, NativeMethods.INPUT.Size) == 0) throw new System.ComponentModel.Win32Exception(); }
private void DoMouseInput(int x, int y, bool absoluteCoordinates, bool? mouseDown) { // TODO: Maybe we should instead send WM_MOUSEMOVE, WM_LBUTTONDOWN etc. // messages directly to the destination window so that we don't need to // position the mouse cursor which makes it harder e.g. to // click on the "Stop" button of the simulator. // Convert the screen coordinates into mouse coordinates. Coordinates cs = new Coordinates(x, y); cs = GetMouseCoordinatesFromScreenCoordinates(cs); var mi = new NativeMethods.MOUSEINPUT(); mi.dx = cs.X; mi.dy = cs.Y; if (absoluteCoordinates) mi.dwFlags |= NativeMethods.MOUSEEVENTF.ABSOLUTE; if (!(!absoluteCoordinates && x == 0 && y == 0)) { // A movement occured. mi.dwFlags |= NativeMethods.MOUSEEVENTF.MOVE; } if (mouseDown.HasValue) { mi.dwFlags |= mouseDown.Value ? NativeMethods.MOUSEEVENTF.LEFTDOWN : NativeMethods.MOUSEEVENTF.LEFTUP; } var input = new NativeMethods.INPUT(); input.type = NativeMethods.INPUT_MOUSE; input.U.mi = mi; NativeMethods.INPUT[] inputs = { input }; if (NativeMethods.SendInput(1, inputs, NativeMethods.INPUT.Size) == 0) throw new System.ComponentModel.Win32Exception(); }