Пример #1
0
        /// <summary>
        /// Moves the wheel horizontally. Specify a negative number to scroll down.
        /// </summary>
        /// <param name="amount">The amount.</param>
        /// <returns></returns>
        public static bool MoveWheelHorizontally(int amount)
        {
            SendInputInfo[] inputs = new SendInputInfo[1];
            inputs[0].Type = (int)InputDeviceTypeEnum.Mouse;
            inputs[0].Inputs.MouseInput.MouseData = (int)amount * 120;
            inputs[0].Inputs.MouseInput.DwFlags = (uint)MouseEventEnum.HWheel;
            inputs[0].Inputs.MouseInput.DwExtraInfo = NativeMethods.GetMessageExtraInfo();

            return NativeMethods.SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0])) == 1;
        }
Пример #2
0
        /// <summary>
        /// Moves the cursor with relative coordinates.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <returns></returns>
        public static bool MoveRelative(int x, int y)
        {
            SendInputInfo[] inputs = new SendInputInfo[1];
            inputs[0].Type = (int)InputDeviceTypeEnum.Mouse;
            inputs[0].Inputs.MouseInput.Dx = x;
            inputs[0].Inputs.MouseInput.Dy = y;
            inputs[0].Inputs.MouseInput.DwFlags = (uint)MouseEventEnum.Move;
            inputs[0].Inputs.MouseInput.DwExtraInfo = NativeMethods.GetMessageExtraInfo();

            return NativeMethods.SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0])) == 1;
        }
Пример #3
0
        /// <summary>
        /// Performs a key down operation.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static bool PerformKeyDown(Keys key)
        {
            byte virtualKeyOrModifier = (byte)key;
            ushort scanCode = (ushort)NativeMethods.MapVirtualKey(virtualKeyOrModifier, 0);

            SendInputInfo[] inputs = new SendInputInfo[1];
            inputs[0].Type = (int)InputDeviceTypeEnum.Keyboard;
            inputs[0].Inputs.KeyboardInput.WVirtualKeyCode = virtualKeyOrModifier;
            inputs[0].Inputs.KeyboardInput.WScanCode = scanCode;
            inputs[0].Inputs.KeyboardInput.DwFlags = (uint)KeyboardKeyEventEnum.KeyDown | 0;

            return NativeMethods.SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0])) == 1;
        }
Пример #4
0
        /// <summary>
        /// Moves the cursor to the provided absolute coordinates.
        /// This is a WinApi call. Using Cursor.Position property is recommended.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <returns></returns>
        public static bool MoveAbsolute(int x, int y)
        {
            bool result = false;

            SendInputInfo[] inputs = new SendInputInfo[1];
            inputs[0].Type = (int)InputDeviceTypeEnum.Mouse;
            inputs[0].Inputs.MouseInput.Dx = 0;
            inputs[0].Inputs.MouseInput.Dy = 0;
            inputs[0].Inputs.MouseInput.DwFlags = (uint)MouseEventEnum.Move | (uint)MouseEventEnum.Absolute;
            inputs[0].Inputs.MouseInput.DwExtraInfo = NativeMethods.GetMessageExtraInfo();

            if (NativeMethods.SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0])) == 1)
            {
                result = MoveRelative(x, y);
            }

            return result;
        }
Пример #5
0
        /// <summary>
        /// Performs mouse up.
        /// </summary>
        /// <param name="button">The button.</param>
        /// <returns></returns>
        public static bool PerformUp(MouseButtons button)
        {
            SendInputInfo[] inputs = new SendInputInfo[1];
            inputs[0].Type = (int)InputDeviceTypeEnum.Mouse;
            inputs[0].Inputs.MouseInput.DwExtraInfo = NativeMethods.GetMessageExtraInfo();
            switch (button)
            {
                case MouseButtons.Left:
                    inputs[0].Inputs.MouseInput.DwFlags = (uint)MouseEventEnum.LeftUp;
                    break;
                case MouseButtons.Middle:
                    inputs[0].Inputs.MouseInput.DwFlags = (uint)MouseEventEnum.MiddleUp;
                    break;
                case MouseButtons.Right:
                    inputs[0].Inputs.MouseInput.DwFlags = (uint)MouseEventEnum.RightUp;
                    break;
                case MouseButtons.XButton1:
                    inputs[0].Inputs.MouseInput.DwFlags = (uint)MouseEventEnum.XUp;
                    inputs[0].Inputs.MouseInput.MouseData = (int)MouseXButtonEnum.XButton1;
                    break;
                case MouseButtons.XButton2:
                    inputs[0].Inputs.MouseInput.DwFlags = (uint)MouseEventEnum.XUp;
                    inputs[0].Inputs.MouseInput.MouseData = (int)MouseXButtonEnum.XButton2;
                    break;
            }

            return NativeMethods.SendInput(1, inputs, System.Runtime.InteropServices.Marshal.SizeOf(inputs[0])) == 1;
        }