public static void Send(Keys key, bool keyDown, bool isEXTEND = false) { Debug.WriteLine(key.ToString() + " " + (keyDown ? "(DOWN)" : "(UP)")); if (IntPtr.Size == 8) { INPUT64 inp = new INPUT64(); inp.type = INPUT_KEYBOARD; inp.ki.wVk = (ushort)key; inp.ki.wScan = (short)MapVirtualKey(inp.ki.wVk, 0); inp.ki.dwFlags = (uint)(((isEXTEND) ? (KEYEVENTF_EXTENDEDKEY) : 0x0) | (keyDown ? KEYEVENTF_KEYDOWN : KEYEVENTF_KEYUP)); inp.ki.time = 0; inp.ki.dwExtraInfo = IntPtr.Zero; SendInput64(1, ref inp, Marshal.SizeOf(inp)); } else { INPUT inp = new INPUT(); inp.type = INPUT_KEYBOARD; inp.ki.wVk = (ushort)key; inp.ki.wScan = (ushort)MapVirtualKey(inp.ki.wVk, 0); inp.ki.dwFlags = (uint)(((isEXTEND) ? (KEYEVENTF_EXTENDEDKEY) : 0x0) | (keyDown ? KEYEVENTF_KEYDOWN : KEYEVENTF_KEYUP)); inp.ki.time = 0; inp.ki.dwExtraInfo = IntPtr.Zero; SendInput(1, ref inp, Marshal.SizeOf(inp)); } }
static void SendKey(short keycode) { KEYBDINPUT ki = new KEYBDINPUT() { time = 0, wScan = 0, dwExtraInfo = (IntPtr)0, wVk = keycode, dwFlags = 0 }; if (IntPtr.Size > 4) { // 64-bit environment INPUT64 ip = new INPUT64(); ip.type = 1; // Indicates a keyboard event ip.ki = ki; SendInput(1, new INPUT64[] { ip }, Marshal.SizeOf(ip)); ip.ki.dwFlags = 2; SendInput(1, new INPUT64[] { ip }, Marshal.SizeOf(ip)); } else { // 32-bit environment INPUT ip = new INPUT(); ip.type = 1; // Indicates a keyboard event ip.ki = ki; // Press.. SendInput(1, new INPUT[] { ip }, Marshal.SizeOf(ip)); ip.ki.dwFlags = 2; // And let go. SendInput(1, new INPUT[] { ip }, Marshal.SizeOf(ip)); } }
public static void Send(ScanCode scanCode, bool keyDown, bool isEXTEND = false) { Debug.WriteLine(scanCode.ToString() + " " + (keyDown ? "(DOWN)" : "(UP)")); if (IntPtr.Size == 8) { INPUT64 inp = new INPUT64(); inp.type = INPUT_KEYBOARD; inp.ki.wScan = (short)scanCode; inp.ki.dwFlags = (uint)(KEYEVENTF_SCANCODE | (keyDown ? KEYEVENTF_KEYDOWN : KEYEVENTF_KEYUP)); inp.ki.time = 0; inp.ki.dwExtraInfo = IntPtr.Zero; SendInput64(1, ref inp, Marshal.SizeOf(inp)); } else { INPUT inp = new INPUT(); inp.type = INPUT_KEYBOARD; inp.ki.wScan = (ushort)scanCode; inp.ki.dwFlags = (uint)(KEYEVENTF_SCANCODE | (keyDown ? KEYEVENTF_KEYDOWN : KEYEVENTF_KEYUP)); inp.ki.time = 0; inp.ki.dwExtraInfo = IntPtr.Zero; SendInput(1, ref inp, Marshal.SizeOf(inp)); } }
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 void DoKeyboard(VK key, KEYEVENT keyEvent) { if (IntPtr.Size < 8) { INPUT[] structInputs = new INPUT[1]; structInputs[0].type = INPUT_KEYBOARD; structInputs[0].ki.wScan = 0; structInputs[0].ki.time = 0; structInputs[0].ki.dwFlags = (uint)keyEvent; structInputs[0].ki.dwExtraInfo = IntPtr.Zero; structInputs[0].ki.wVk = (ushort)key; SendInput(1, structInputs, Marshal.SizeOf(typeof(INPUT))); } else { INPUT64[] structInputs = new INPUT64[1]; structInputs[0].type = INPUT_KEYBOARD; structInputs[0].ki.wScan = 0; structInputs[0].ki.time = 0; structInputs[0].ki.dwFlags = (uint)keyEvent; structInputs[0].ki.dwExtraInfo = IntPtr.Zero; structInputs[0].ki.wVk = (ushort)key; SendInput64(1, structInputs, Marshal.SizeOf(typeof(INPUT64))); } }
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 static void SendScanCode(uint scanCode, bool keyDown, bool isEXTEND = false) { if ((scanCode & 0x0100) == 0x0100) { SendMouseInput((MouseInput)scanCode, keyDown); return; } Debug.WriteLine(scanCode.ToString() + " " + (keyDown ? "(DOWN)" : "(UP)")); if (IntPtr.Size == 8) { INPUT64 inp = new INPUT64(); inp.type = INPUT_KEYBOARD; inp.ki.wScan = (short)(scanCode & 0xFF); inp.ki.dwFlags = (uint)(KEYEVENTF_SCANCODE | (keyDown ? KEYEVENTF_KEYDOWN : KEYEVENTF_KEYUP)); if ((scanCode & 0xE000) != 0) { inp.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; } inp.ki.time = 0; inp.ki.dwExtraInfo = IntPtr.Zero; SendInput64(1, ref inp, Marshal.SizeOf(inp)); } else { INPUT inp = new INPUT(); inp.type = INPUT_KEYBOARD; inp.ki.wScan = (ushort)(scanCode & 0xFFFF); inp.ki.dwFlags = (uint)(KEYEVENTF_SCANCODE | (keyDown ? KEYEVENTF_KEYDOWN : KEYEVENTF_KEYUP)); if ((scanCode & 0xE000) != 0) { inp.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; } inp.ki.time = 0; inp.ki.dwExtraInfo = IntPtr.Zero; SendInput(1, ref inp, Marshal.SizeOf(inp)); } }
public void SendMouseClick(int x, int y) { // x,y are absolute coordinates in units of pixels // SendInput needs normalized (0-65535) absolute coordinates // so normalize coordinates first AbsolutePixelToMickeys(ref x, ref y); if (IntPtr.Size < 8) { INPUT[] structInputs = new INPUT[2]; structInputs[0].type = INPUT_MOUSE; structInputs[0].mi.dx = x; structInputs[0].mi.dy = y; structInputs[0].mi.mouseData = 0; structInputs[0].mi.time = 0; structInputs[0].mi.dwExtraInfo = IntPtr.Zero; structInputs[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_LEFTDOWN; structInputs[1] = structInputs[0]; structInputs[1].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_LEFTUP; SendInput(2, structInputs, Marshal.SizeOf(typeof(INPUT))); } else { INPUT64[] structInputs = new INPUT64[2]; structInputs[0].type = INPUT_MOUSE; structInputs[0].mi.dx = x; structInputs[0].mi.dy = y; structInputs[0].mi.mouseData = 0; structInputs[0].mi.time = 0; structInputs[0].mi.dwExtraInfo = IntPtr.Zero; structInputs[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_LEFTDOWN; structInputs[1] = structInputs[0]; structInputs[1].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_LEFTUP; SendInput64(2, structInputs, Marshal.SizeOf(typeof(INPUT64))); } }
private void DoKeyAction(KEYBDINPUT ki, KeyActions ka) { uint test1 = 1; uint test2 = 1; if (IntPtr.Size > 4) { INPUT64 input = new INPUT64(); input.type = INPUT_KEYBOARD; input.ki = ki; if (ka == KeyActions.Hold || ka == KeyActions.Press) { test1 = SendInput(1, new INPUT64[] { input }, Marshal.SizeOf(input)); } input.ki.dwFlags |= (uint)KEYEVENTF.KEYUP; if (ka == KeyActions.Release || ka == KeyActions.Press) { test2 = SendInput(1, new INPUT64[] { input }, Marshal.SizeOf(input)); } } else { INPUT input = new INPUT(); input.type = INPUT_KEYBOARD; input.ki = ki; if (ka == KeyActions.Hold || ka == KeyActions.Press) { test1 = SendInput(1, new INPUT[] { input }, Marshal.SizeOf(input)); } input.ki.dwFlags |= (uint)KEYEVENTF.KEYUP; if (ka == KeyActions.Release || ka == KeyActions.Press) { test2 = SendInput(1, new INPUT[] { input }, Marshal.SizeOf(input)); } } if (test1 == 0 || test2 == 0) { string key = ki.wVk != 0 ? Enum.GetName(typeof(VirtualKeyCode), ki.wVk) : Enum.GetName(typeof(ScanCodeShort), ki.wScan); SendInputKeyFailedCall?.Invoke(Marshal.GetLastWin32Error().ToString(), key, ka); } }
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, INPUT64[] pInputs, int cbSize);
public static extern uint SendInput(uint nInputs, ref INPUT64 pInputs, int cbSize);
internal static void MouseActionViaSendInput(MouseEventFlags flags, uint time, int relX, int relY, uint mouseData) { // NOTE: this code yields strange behavior for mouse-wheel events and when panning a third-person view in most games // NOTE: this code also does not appear to perform mouse broadcast as expected // NOTE: only retaining this code for reference in the event someone can correct the misbehaviors, or in case there is ever a game that will only respect INPUT and not WM for mouse uint result = 0; if (IntPtr.Size == 8) { INPUT64[] input = new INPUT64[1]; input[0].InputType = InputType.INPUT_MOUSE; input[0].mi.dwExtraInfo = GetMessageExtraInfo(); input[0].mi.Flags = flags; input[0].mi.time = GetTickCount(); input[0].mi.dx = (int)relX; input[0].mi.dy = (int)relY; input[0].mi.mouseData = mouseData; result = SendInput(1, input, Marshal.SizeOf(input[0])); } else { INPUT[] input = new INPUT[1]; input[0].InputType = InputType.INPUT_MOUSE; input[0].mi.dwExtraInfo = GetMessageExtraInfo(); input[0].mi.Flags = flags; input[0].mi.time = GetTickCount(); input[0].mi.dx = (int)relX; input[0].mi.dy = (int)relY; input[0].mi.mouseData = mouseData; result = SendInput(1, input, Marshal.SizeOf(input[0])); } if (result == 0) { int err = Marshal.GetLastWin32Error(); ("SendInput(mi) Blocked, 0x" + err.ToString("X")).Log(); } }
private extern static void SendInput64(int nInputs, ref INPUT64 pInputs, int cbsize);