我目前正在制作必须像人一样的宏。我的基本要求是,将x,y点的数组放入for循环中,然后移动到每个点。为了使此动作平滑,我正在使用一种可以在论坛/线程中找到的平滑方法:Smooth Mouse Movement using mouse_event with set delay C++ 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)编辑。基本上我不知道

很抱歉提供不完善的解释和示例。

最佳答案

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

c&#43;&#43; - 宏的受控鼠标加速-LMLPHP

在此的示例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);
  }
}

关于c++ - 宏的受控鼠标加速,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59365291/

10-14 17:45