鼠标事件-鼠标未按预期移动

时间:2019-04-27 20:13:36

标签: c# mouse hotkeys

我正在尝试使用键盘箭头键移动光标。结果是它将移动,但仅向上和向左移动。朝向屏幕的左上角。它没有按预期向下移动。

现在,如果我将xSpeed和ySpeed设置为大于3,它将可以工作,但是向下和向右移动会更慢。如果我将速度设为10,那么结果将不明显,但这对于我需要的速度来说太快了。

        [DllImport("User32.dll",EntryPoint = "mouse_event",CallingConvention = CallingConvention.Winapi)]
    internal static extern void Mouse_Event(int dwFlags,int dx,int dy,int dwData,int dwExtraInfo);

    [DllImport("User32.dll",EntryPoint = "GetSystemMetrics",CallingConvention = CallingConvention.Winapi)]
    internal static extern int InternalGetSystemMetrics(int value);

private void UpdateLoop()
    {
        if (_hotKeys.Contains(Keys.Up))
            MoveCursor(y: -YSpeed());
        if (_hotKeys.Contains(Keys.Down))
            MoveCursor(y: YSpeed());
        if (_hotKeys.Contains(Keys.Left))
            MoveCursor(x: -XSpeed());
        if (_hotKeys.Contains(Keys.Right))
            MoveCursor(x: XSpeed());
    }

private void MoveCursor(int x = 0, int y = 0)
    {
        int to_x = MousePosition.X + x;
        int to_y = MousePosition.Y + y;

        int screenWidth = InternalGetSystemMetrics(0);
        int screenHeight = InternalGetSystemMetrics(1);

        int mic_x = (int)Math.Round(to_x * 65536.0 / screenWidth);
        int mic_y = (int)Math.Round(to_y * 65536.0 / screenHeight);

        Mouse_Event(0x0001 | 0x8000, mic_x, mic_y, 0, 0);
    }

在这种情况下,XSpeed和YSpeed等于1。

        public int XSpeed()
    {
        return int.Parse(tb_x.Text);
    }

    public int YSpeed()
    {
        return int.Parse(tb_y.Text);
    }

任何帮助将不胜感激!

0 个答案:

没有答案