首页 > 解决方案 > 宏的受控鼠标加速

问题描述

我目前正在制作一个必须类似于人类的宏。我的基本要求是我将一组 x,y 点放入一个 for 循环中,然后它会移动到每个点。为了使这个运动平滑,我使用了一种平滑方法,可以在此处的论坛/线程中找到:使用带有 set delay C++ 的 mouse_event 平滑鼠标移动Scheff 制作的这种平滑方法效果非常好,这就是我正在使用的方法。我一直试图在其中加入一些加速度,这样一旦它离开原来的点,它就会加速,然后在到达下一个点时减速。(我想我解释得很糟糕对不起)

这是原始代码(来自 Scheff)

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;
  }
}

这是我尝试在其中包含可控加速度

int total = 0;
void smoothing(int delay, int x, int y, int acceration)
{
    int x_ = 0, y_ = 0, t_ = 0;
    for (int i = 1; i <= delay - total; ++i) {
        // i / smoothing provides the interpolation paramter in [0, 1]
        int xI = i * x / delay;
        int yI = i * y / delay;
        int tI = i * (delay / delay) + total;
        mouse_event(1, xI - x_, yI - y_, 0, 0);
        //std::cout << "X: " << xI - x_ << " Y: " << yI - y_ << " Delay: " << tI - t_ << std::endl; //Management
        AccurateSleep(tI - t_);
        x_ = xI; y_ = yI; t_ = tI;

        total++;
    }
}

我知道这是一个可怜的尝试,但这是我真正能想到的唯一方法。我不确定是否要为是否进行延迟的 x 和 y 运动添加某种加速度。(现在我回想起来,它必须是获得加速度的 x 和 y)编辑。基本上我不知道。

抱歉,解释和示例不佳。

标签: c++algorithmmathmouseeventacceleration

解决方案


假设我们想d用鼠标移动一段距离,使用初始加速度,a然后是相同幅度的减速度。类似于以下随时间变化的运动曲线:

运动曲线

在这个例子中,d=30a=5。两个运动部分(加速和减速)由d/2线分开。您可以通过以下方式定义它们:

s(t) = {  a/2 * t^2                                     if t < tm
         -a/2 * (t - tm)^2 + a * tm * (t - tm) + d/2    otherwise 

时间tm是到达中间点的时间点。这是

tm = sqrt(d / a)

这就是你所需要的。修改后的代码如下所示:

void Smoothing(int steps, int dx, int dy, int startX, int startY, double acceleration)
{
  double distance = std::sqrt(dx * dx + dy * dy);
  double tm = std::sqrt(distance / acceleration);
  for (int i = 1; i <= steps; ++i) {
    AccurateSleep(2.0 * tm / steps);
    double t = i * 2 * tm / steps;
    double s;
    if(t <= tm) {         
      s = 0.5 * acceleration * t * t;
    } else {
      s = -0.5 * acceleration * (t - tm) * (t - tm) + a * tm * (t - tm) + 0.5 * distance;
    }
    int xI = (int)std::round(startX + s * dx / distance);
    int yI = (int)std::round(startY + s * dy / distance);

    mouse_event(MOUSEEVENTF_ABSOLUTE, xI, yI, 0, 0); 
  }
}

推荐阅读