如何使我的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中尝试了这段代码,但它会颤动和需要缓慢的下降。我该尝试什么?

点赞
用户1847592
用户1847592

为了平滑移动,你可以尝试跳短步并睡短时间。

这需要特殊版本的 SleepMoveMouseRelative

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
2021-06-23 06:24:29