public static void SendKeyUp(ushort key) { if (IntPtr.Size == 8) { INPUT64 input64 = new INPUT64(); input64.type = INPUT_KEYBOARD; input64.ki.wVk = key; input64.ki.wScan = 0; input64.ki.time = 0; input64.ki.dwFlags = KEY_UP; input64.ki.dwExtraInfo = IntPtr.Zero; // Key up the actual key-code SendInput64.SendInput(1, ref input64, Marshal.SizeOf(input64)); } else { INPUT32 input32 = new INPUT32(); input32.type = INPUT_KEYBOARD; input32.ki.wVk = key; input32.ki.wScan = 0; input32.ki.time = 0; input32.ki.dwFlags = KEY_UP; input32.ki.dwExtraInfo = GetMessageExtraInfo(); // Key up the actual key-code SendInput32.SendInput(1, ref input32, Marshal.SizeOf(input32)); } }
public static uint SendInput(KeyEventSequence keys, bool sendKeysIndividually, Nullable<TimeSpan> pauseBetweenKeys) { if (IntPtr.Size == 4) { INPUT32[] inputs = keys.ToInputArray32(); if (sendKeysIndividually) { INPUT32[] chunkedInput = new INPUT32[1]; uint total = 0; for (int i = 0; i < inputs.Length; ++i) { chunkedInput[0] = inputs[i]; total += Win32.SendInput32(1, chunkedInput, Marshal.SizeOf(typeof(INPUT32))); if (pauseBetweenKeys.HasValue) { Thread.Sleep(pauseBetweenKeys.Value); } } return total; } else { return Win32.SendInput32((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT32))); } } else if (IntPtr.Size == 8) { INPUT64[] inputs = keys.ToInputArray64(); if (sendKeysIndividually) { INPUT64[] chunkedInput = new INPUT64[1]; uint total = 0; for (int i = 0; i < inputs.Length; ++i) { chunkedInput[0] = inputs[i]; total += Win32.SendInput64(1, chunkedInput, Marshal.SizeOf(typeof(INPUT64))); if (pauseBetweenKeys.HasValue) { Thread.Sleep(pauseBetweenKeys.Value); } } return total; } else { return Win32.SendInput64((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT64))); } } else { throw new NotSupportedException("Can't figure out platform bitness. Pointers have this width: " + IntPtr.Size.ToString()); } }
public void MoveMouse(PointF p) { //TODO: move mouse should use a Point rather than a PointF and should get directly usable coordinates if (IntPtr.Size == 8) { var move = new INPUT64[1]; move[0] = new INPUT64(); move[0].mi.dx = (int)(p.X * 65535 / ScreenSize.Width); move[0].mi.dy = (int)(p.Y * 65535 / ScreenSize.Height); move[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE; SendInput(1, move, Marshal.SizeOf(move[0])); } else { var move = new INPUT32[1]; move[0] = new INPUT32(); move[0].mi.dx = (int)(p.X * 65535 / ScreenSize.Width); move[0].mi.dy = (int)(p.Y * 65535 / ScreenSize.Height); move[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE; SendInput(1, move, Marshal.SizeOf(move[0])); } }
static extern uint SendInput(uint nInputs, INPUT32[] pInputs, int cbSize);
public static extern uint SendInput(uint nInputs, ref INPUT32 pInputs, int cbSize);