Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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();
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 public static bool SendSingleInput(this Native.INPUT input)
 {
     return(Native.Methods.SendInput(1, new Native.INPUT[] { input }, Marshal.SizeOf(typeof(Native.INPUT))) == 1);
 }