c++ - 宏的受控鼠标加速
问题描述
我目前正在制作一个必须类似于人类的宏。我的基本要求是我将一组 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)编辑。基本上我不知道。
抱歉,解释和示例不佳。
解决方案
假设我们想d
用鼠标移动一段距离,使用初始加速度,a
然后是相同幅度的减速度。类似于以下随时间变化的运动曲线:
在这个例子中,d=30
和a=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);
}
}
推荐阅读
- typescript - Is there a way to get the keys of an interface where interface[key] is a type?
- c++ - 多态性和在 C++ 中定义不同对象的列表
- regex - 如何创建通用正则表达式,以便可以在 scala spark 中提取所有组结果
- python - Using a blank line as an indicator value to end the input in while loop
- python - why filter function in python3 always returns empty result?
- r - R / ggplot2 / add total value(sum) above barplot with each value in fill
- javascript - Setting meta description on blog post with nuxt and NetlifyCMS
- javascript - 我们可以在 Head Tag 中调用函数吗?
- flutter - 即使我创建了有状态小部件,为什么没有定义函数 setState()?
- c# - UWP:如何动态选择文本框中最后输入的单词并更改其突出显示颜色?