使用具有设置延迟的mouse_event进行平滑的鼠标移动C ++ [英] Smooth Mouse Movement using mouse_event with set delay C++

查看:246
本文介绍了使用具有设置延迟的mouse_event进行平滑的鼠标移动C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码鼠标宏.它需要在屏幕上的每个点之间以设置的延迟满足某些点.例如,它必须在132毫秒内移动(x 14,y 30).我遇到的问题是mouse_event跳转到准确的位置,所以我需要包括某种平滑方法,使其平稳地移动到每个点. (平滑的运动是更好的宏观).目前我使用平滑每个运动的此方法.

I'm coding a mouse macro. It needs to meet certain points on the screen in a set delay between each point. For exammple, it must move (x 14, y 30) in 132ms. The issue I'm having is mouse_event jumps to that exact position so I need to include some sort of smoothing method so that it moves smoothly to each point. (the smoother the movement is the better the macro). Currently I am using this method of smoothing each movement.

此工作得很好,但它也有其局限性,例如,如果它需要向左移动10个像素和平滑被设定为20,它将继续跳跃.

This works well but it has its limitations for example if it needs to move 10 pixels left and the smoothing is set to 20 it will continue to jump.

有人知道平滑鼠标移动的更准确方法吗? (要求准确,平滑)

Does anyone know of a more accurate method of smoothing mouse movements? (requirements accurate, smooth)

void Smoothing(int smoothing, int delay, int x, int y) {
    for (int i = 0; i < smoothing; i++) {
        mouse_event(1, x / smoothing, y / smoothing, 0, 0);
        AccurateSleep(delay / smoothing);
    }
    mouse_event(1, x % smoothing, y % smoothing, 0, 0);
    Sleep(delay % smoothing);
}


推荐答案

线性插值其他答案中提到的)时,strong> 是我的第一个想法.

Linear Interpolation was my first thought when I read the question (as well as mentioned in the other answer).

用于内插的一般表现公式是:

A general formular for interpolation is:

&NBSP;&NBSP;&NBSP;&NBSP; X =(1 - T)· X <子> 0 + T· X <子> 1

    x = (1 - t) · x0 + t · x1

&NBSP;&NBSP;&NBSP;&NBSP; X ...插补值
&NBSP;&NBSP;&NBSP;&NBSP; X <子> 0 ...起始值
&NBSP;&NBSP;&NBSP;&NBSP; X <子> 1 ...目的地值
&NBSP;&NBSP;&NBSP;&NBSP; ...在范围内插参数[0,1]

    x ... interpolated value
    x0 ... start value
    x1 ... destination value
    t ... interpolation parameter in range [0, 1]

我甚至打算时,我意识到一些事实,可能会形成可能的限制(其中OP遗憾的是没有明确提到).

I even intended to write this as answer when I realized some facts that might form possible constraints (which the OP unfortunately didn't mention explicitly).

  1. 所有运算都是关于整数值.所以,做整数运算可以是优选的.
  2. 以及该被调用的增量值.这可以通过由OP使用的API来决定.

所以,我想两次,并提出以下 MCVE 以类似的OP问题:

So, I thought twice and made the following MCVE to resemble OPs problem:

#include <iostream>

static int xMouse = 0, yMouse = 0, t = 0;

void mouse_event(int _1, int dx, int dy, int _4, int _5)
{
  xMouse += dx; yMouse += dy;
  std::cout << "mouse_event(" << _1 << ", " << dx << ", " << dy << ", " << _4 << ", " << _5 << "): "
    << xMouse << ", " << yMouse << '\n';
}

void AccurateSleep(int delay)
{
  t += delay;
  std::cout << "AccurateSleep(" << delay << "): " << t << '\n';

}

void Sleep(int delay)
{
  t += delay;
  std::cout << "Sleep(" << delay << "): " << t << '\n';
}

void Smoothing(int smoothing, int delay, int x, int y)
{
    for (int i = 0; i < smoothing; i++) {
        mouse_event(1, x / smoothing, y / smoothing, 0, 0);
        AccurateSleep(delay / smoothing);
    }
    mouse_event(1, x % smoothing, y % smoothing, 0, 0);
    Sleep(delay % smoothing);
}

#define PRINT_AND_DO(...) std::cout << #__VA_ARGS__ << ";\n"; __VA_ARGS__ 

int main()
{
  PRINT_AND_DO(xMouse = 0; yMouse = 0; t = 0);
  PRINT_AND_DO(Smoothing(10, 132, 14, 30));
  PRINT_AND_DO(xMouse = 0; yMouse = 0; t = 0);
  PRINT_AND_DO(Smoothing(20, 15, 10, 0));
}

输出:

xMouse = 0; yMouse = 0; t = 0;
Smoothing(10, 132, 14, 30);
mouse_event(1, 1, 3, 0, 0): 1, 3
AccurateSleep(13): 13
mouse_event(1, 1, 3, 0, 0): 2, 6
AccurateSleep(13): 26
mouse_event(1, 1, 3, 0, 0): 3, 9
AccurateSleep(13): 39
mouse_event(1, 1, 3, 0, 0): 4, 12
AccurateSleep(13): 52
mouse_event(1, 1, 3, 0, 0): 5, 15
AccurateSleep(13): 65
mouse_event(1, 1, 3, 0, 0): 6, 18
AccurateSleep(13): 78
mouse_event(1, 1, 3, 0, 0): 7, 21
AccurateSleep(13): 91
mouse_event(1, 1, 3, 0, 0): 8, 24
AccurateSleep(13): 104
mouse_event(1, 1, 3, 0, 0): 9, 27
AccurateSleep(13): 117
mouse_event(1, 1, 3, 0, 0): 10, 30
AccurateSleep(13): 130
mouse_event(1, 4, 0, 0, 0): 14, 30
Sleep(2): 132

xMouse = 0; yMouse = 0; t = 0;
Smoothing(20, 15, 10, 0);
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 10, 0, 0, 0): 10, 0
Sleep(15): 15

然后我修改实现上述与一些调整的具体情况所提到的内插式:

Then I modified Smoothing() implementing the above mentioned interpolation formula with some adjustments to the specific situation:

  1. 对于(有在范围[1,平滑处理])被使用.
  2. 虽然循环确实为内插的每个,前一次迭代的值保持并用于计算Δ值对于
  3. 的函数调用
  4. 当然,操作的顺序是很重要的,因为这是整数运算.因此,xI = i * x / smoothing不等同于xI = i / smoothing * x. (即交换性并不受这些积分操作提供.)
  1. For t, i / smoothing (with i in range [1, smoothing]) is used.
  2. While the loop does the interpolation for each i, the values of previous iteration are kept and used to compute delta values for the function calls of mouse_event() and AccurateSleep().
  3. Of course, the order of operations is important as this is integer arithmetic. Hence, xI = i * x / smoothing is not equivalent to xI = i / smoothing * x. (I.e. commutativity is not provided by these integral operations.)

修饰的:

The modified Smoothing():

void Smoothing(int smoothing, int delay, int x, int y)
{
  int x_ = 0, y_ = 0, t_ = 0;
  for (int i = 1; i <= smoothing; ++i) {
    // i / smoothing provides the interpolation paramter in [0, 1]
    int xI = i * x / smoothing;
    int yI = i * y / smoothing;
    int tI = i * delay / smoothing;
    mouse_event(1, xI - x_, yI - y_, 0, 0);
    AccurateSleep(tI - t_);
    x_ = xI; y_ = yI; t_ = tI;
  }
}

输出:

xMouse = 0; yMouse = 0; t = 0;
Smoothing(10, 132, 14, 30);
mouse_event(1, 1, 3, 0, 0): 1, 3
AccurateSleep(13): 13
mouse_event(1, 1, 3, 0, 0): 2, 6
AccurateSleep(13): 26
mouse_event(1, 2, 3, 0, 0): 4, 9
AccurateSleep(13): 39
mouse_event(1, 1, 3, 0, 0): 5, 12
AccurateSleep(13): 52
mouse_event(1, 2, 3, 0, 0): 7, 15
AccurateSleep(14): 66
mouse_event(1, 1, 3, 0, 0): 8, 18
AccurateSleep(13): 79
mouse_event(1, 1, 3, 0, 0): 9, 21
AccurateSleep(13): 92
mouse_event(1, 2, 3, 0, 0): 11, 24
AccurateSleep(13): 105
mouse_event(1, 1, 3, 0, 0): 12, 27
AccurateSleep(13): 118
mouse_event(1, 2, 3, 0, 0): 14, 30
AccurateSleep(14): 132

xMouse = 0; yMouse = 0; t = 0;
Smoothing(20, 15, 10, 0);
mouse_event(1, 0, 0, 0, 0): 0, 0
AccurateSleep(0): 0
mouse_event(1, 1, 0, 0, 0): 1, 0
AccurateSleep(1): 1
mouse_event(1, 0, 0, 0, 0): 1, 0
AccurateSleep(1): 2
mouse_event(1, 1, 0, 0, 0): 2, 0
AccurateSleep(1): 3
mouse_event(1, 0, 0, 0, 0): 2, 0
AccurateSleep(0): 3
mouse_event(1, 1, 0, 0, 0): 3, 0
AccurateSleep(1): 4
mouse_event(1, 0, 0, 0, 0): 3, 0
AccurateSleep(1): 5
mouse_event(1, 1, 0, 0, 0): 4, 0
AccurateSleep(1): 6
mouse_event(1, 0, 0, 0, 0): 4, 0
AccurateSleep(0): 6
mouse_event(1, 1, 0, 0, 0): 5, 0
AccurateSleep(1): 7
mouse_event(1, 0, 0, 0, 0): 5, 0
AccurateSleep(1): 8
mouse_event(1, 1, 0, 0, 0): 6, 0
AccurateSleep(1): 9
mouse_event(1, 0, 0, 0, 0): 6, 0
AccurateSleep(0): 9
mouse_event(1, 1, 0, 0, 0): 7, 0
AccurateSleep(1): 10
mouse_event(1, 0, 0, 0, 0): 7, 0
AccurateSleep(1): 11
mouse_event(1, 1, 0, 0, 0): 8, 0
AccurateSleep(1): 12
mouse_event(1, 0, 0, 0, 0): 8, 0
AccurateSleep(0): 12
mouse_event(1, 1, 0, 0, 0): 9, 0
AccurateSleep(1): 13
mouse_event(1, 0, 0, 0, 0): 9, 0
AccurateSleep(1): 14
mouse_event(1, 1, 0, 0, 0): 10, 0
AccurateSleep(1): 15

现场演示上coliru

注意:

在最后一次迭代与完成使得结果在1.因此,最后的内插步骤产生的准确值&ndash的;没有后校正是必要的像OP的原始的方法.

The last iteration is done with i == smoothing so that i / smoothing results in 1. Hence, the last interpolation step yields the exact values – no post-correction is necessary like in OPs original approach.

这篇关于使用具有设置延迟的mouse_event进行平滑的鼠标移动C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆