Search code examples
luamouseeventmouselogitech-gaming-software

How can I make my Lua script do a natural curve down rather then juddering diagonally?


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 

I tried this code in lua however it makes it judder and need it to curve slowly down. What can I try?


Solution

  • To move smoothly you may try to jump with shorter steps and sleep for shorter periods.
    This would require special versions for Sleep and 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
    

    UPDATE
    If you don't have unused CPU core there is another version, not so smooth, but with a constant speed

    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