首页 > 解决方案 > 我怎样才能让我的 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 中尝试了这段代码,但是它使它颤抖并且需要它慢慢向下弯曲。我可以尝试什么?

标签: luamouseeventmouselogitech-gaming-software

解决方案


为了平稳移动,您可以尝试以更短的步数跳跃并睡更短的时间。
这将需要特殊版本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

推荐阅读