lua - 我怎样才能让我的 Lua 脚本自然地向下弯曲而不是对角抖动?
问题描述
EnablePrimaryMouseButtonEvents (true);
function OnEvent(event,arg)
if IsKeyLockOn("numlock")then
if IsMouseButtonPressed(3)then
repeat
if IsMouseButtonPressed(1) then
repeat
MoveMouseRelative(0,11)
Sleep(15)
MoveMouseRelative(-1,0)
until not IsMouseButtonPressed(1)
end
until not IsMouseButtonPressed(3)
end
end
end
我在 lua 中尝试了这段代码,但是它使它颤抖并且需要它慢慢向下弯曲。我可以尝试什么?
解决方案
为了平稳移动,您可以尝试以更短的步数跳跃并睡更短的时间。
这将需要特殊版本Sleep
和MoveMouseRelative
:
do
local frac_x, frac_y = 0, 0
local orig_MoveMouseRelative = MoveMouseRelative
function MoveMouseRelative(x, y)
frac_x, frac_y = frac_x + x, frac_y + y
x, y = math.floor(frac_x), math.floor(frac_y)
frac_x, frac_y = frac_x - x, frac_y - y
while x ~= 0 or y ~= 0 do
local dx = math.min(127, math.max(-127, x))
local dy = math.min(127, math.max(-127, y))
x, y = x - dx, y - dy
orig_MoveMouseRelative(dx, dy)
end
end
local function busyloop(final_ctr)
final_ctr = final_ctr - final_ctr%1
local ctr, prev_ms, ms0, ctr0 = 0
while ctr ~= final_ctr do
local ms = GetRunningTime()
if prev_ms and ms ~= prev_ms then
if not ms0 then
ms0, ctr0 = ms, ctr
elseif final_ctr < 0 and ms - ms0 > 500 then
return (ctr - ctr0) / (ms - ms0)
end
end
prev_ms = ms
ctr = ctr + 1
end
end
local coefficient = busyloop(-1)
local orig_Sleep = Sleep
function Sleep(ms)
if ms > 30 then
return orig_Sleep(ms)
else
return busyloop(ms * coefficient)
end
end
end
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event,arg)
if IsKeyLockOn("numlock")then
if IsMouseButtonPressed(3)then
repeat
if IsMouseButtonPressed(1) then
repeat
MoveMouseRelative(-0.1, 1.1)
Sleep(1.5) -- should be about 1ms for really smooth movement
until not IsMouseButtonPressed(1)
end
until not IsMouseButtonPressed(3)
end
end
end
更新
如果您没有未使用的 CPU 内核,还有另一个版本,不是那么流畅,但速度恒定
do
local frac_x, frac_y, prev_time = 0, 0
function StartMoving()
prev_time = GetRunningTime()
end
function MoveMouseForAWhile(x, y)
Sleep(1)
local time = GetRunningTime()
time, prev_time = time - prev_time, time
frac_x, frac_y = frac_x + time * x, frac_y + time * y
x, y = math.floor(frac_x), math.floor(frac_y)
frac_x, frac_y = frac_x - x, frac_y - y
while x ~= 0 or y ~= 0 do
local dx = math.min(127, math.max(-127, x))
local dy = math.min(127, math.max(-127, y))
x, y = x - dx, y - dy
MoveMouseRelative(dx, dy)
end
end
end
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event,arg)
if IsKeyLockOn("numlock")then
if IsMouseButtonPressed(3)then
repeat
if IsMouseButtonPressed(1) then
local speed = 1.5
StartMoving()
repeat
MoveMouseForAWhile(-0.1 * speed, 1.1 * speed)
until not IsMouseButtonPressed(1)
end
until not IsMouseButtonPressed(3)
end
end
end
推荐阅读
- sql - 如何在 DB2 中引用隐式命名的列
- regex - 正则表达式,在反斜杠字符之前停止?
- python - 我的 CNN 图像识别模型产生波动的验证损失
- c# - 在单例类的 get 实例属性中使用 setter 是否是一种好习惯
- java - 如何在 JDL 中使用字符串值定义枚举器?
- python - Python 中的 LSTM 生成平面预测
- java - 保存和检索 v4 片段状态
- c++ - 这种尝试在 C++ 中使用更 Python 风格的装饰器是否可靠/安全
- json - 将 Pandas 数据帧作为 JSON 对象上传到 Cloud Storage
- angular - 如何在角度 7 中将数组数据插入到 ng2-google-chart 的图表中