public Task PressKeyboardButton(int keyCode) { if (keyCode > short.MaxValue) { return(Task.CompletedTask); } var keyDown = new Native.INPUT() { Type = Native.Constants.INPUT_KEYBOARD, }; keyDown.Data.Keyboard.wVk = (short)keyCode; if (IsExtendedKey((Native.VirtualKey)keyCode)) { keyDown.Data.Keyboard.dwFlags |= Native.KEYEVENTF.EXTENDEDKEY; } keyDown.Data.Keyboard.dwExtraInfo = InputUtils.InstancePointer; if (keyDown.SendSingleInput()) { pressedKeyCodes.Add(keyCode); } return(Task.CompletedTask); }
public Task Wheel(int delta, MouseWheelDirection direction) { var wheel = new Native.INPUT() { Type = Native.Constants.INPUT_MOUSE, }; wheel.Data.Mouse.dwFlags = direction == MouseWheelDirection.Horizontal ? Native.MOUSEEVENTF.HWHEEL : Native.MOUSEEVENTF.WHEEL; wheel.Data.Mouse.mouseData = delta; wheel.SendSingleInput(); return(Task.CompletedTask); }
public Task MoveMouse(Point position) { var movement = new Native.INPUT() { Type = Native.Constants.INPUT_MOUSE, }; var virtualDesktopPosition = ToVirtualDesktopCoordinates(position); movement.Data.Mouse.dwFlags = Native.MOUSEEVENTF.ABSOLUTE | Native.MOUSEEVENTF.MOVE | Native.MOUSEEVENTF.VIRTUALDESK; movement.Data.Mouse.dx = virtualDesktopPosition.X; movement.Data.Mouse.dy = virtualDesktopPosition.Y; movement.Data.Mouse.dwExtraInfo = InputUtils.InstancePointer; movement.SendSingleInput(); return(Task.CompletedTask); }
private void ReleaseAllPressed() { var events = pressedKeyCodes.Select(keyCode => { var keyUp = new Native.INPUT() { Type = Native.Constants.INPUT_KEYBOARD, }; keyUp.Data.Keyboard.wVk = (short)keyCode; keyUp.Data.Keyboard.dwFlags = Native.KEYEVENTF.KEYUP; if (IsExtendedKey((Native.VirtualKey)keyCode)) { keyUp.Data.Keyboard.dwFlags |= Native.KEYEVENTF.EXTENDEDKEY; } keyUp.Data.Keyboard.dwExtraInfo = InputUtils.InstancePointer; return(keyUp); }); events.SendInputs(); pressedKeyCodes.Clear(); }
public Task ReleaseMouseButton(MouseButton button) { var mouseUp = new Native.INPUT() { Type = Native.Constants.INPUT_MOUSE, }; switch (button) { case MouseButton.Left: mouseUp.Data.Mouse.dwFlags = Native.MOUSEEVENTF.LEFTUP; break; case MouseButton.Middle: mouseUp.Data.Mouse.dwFlags = Native.MOUSEEVENTF.MIDDLEUP; break; case MouseButton.Right: mouseUp.Data.Mouse.dwFlags = Native.MOUSEEVENTF.RIGHTUP; break; case MouseButton.XButton1: mouseUp.Data.Mouse.dwFlags = Native.MOUSEEVENTF.XUP; mouseUp.Data.Mouse.mouseData = Native.Constants.XBUTTON1; break; case MouseButton.XButton2: mouseUp.Data.Mouse.dwFlags = Native.MOUSEEVENTF.XUP; mouseUp.Data.Mouse.mouseData = Native.Constants.XBUTTON2; break; } mouseUp.Data.Mouse.dwExtraInfo = InputUtils.InstancePointer; mouseUp.SendSingleInput(); return(Task.CompletedTask); }
public static bool SendSingleInput(this Native.INPUT input) { return(Native.Methods.SendInput(1, new Native.INPUT[] { input }, Marshal.SizeOf(typeof(Native.INPUT))) == 1); }