Search code examples
lualogitechlogitech-gaming-software

Logitech Lua Script: MoveMouseRelative not working as expected


I am trying to write a script which will spin my character 180 degrees in game while holding lctrl and right clicking. This script works, but takes forever to spin around because of the sleep timer:

EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
    if (event == "MOUSE_BUTTON_PRESSED" and arg == 2 and IsModifierPressed("lctrl")) then
        for i = 0, 96 do
            MoveMouseRelative (125,0)
            Sleep (1)
        end
    end
end

If I increase MouseMoveRelative beyond 125, it starts to move the mouse in the wrong direction. No matter what value I use (I've tried lots of values between 100 and 12,000), it always moves the mouse a very small distance either left or right.

If I eliminate the Sleep function, the results are inconsistent. It usually rotates my character between 80-140 degrees. I suspect this is because MoveMouseRelative starts by getting the current mouse position and it requires a delay before the position it gets is accurate.

Any guidance as to why MouseMoveRelative doesn't work correctly for values above 125? Or any advice on how to quickly move the mouse 12,000 relative units on the x-axis instantaneously?


Solution

  • If I increase MouseMoveRelative beyond 125, it starts to move the mouse in the wrong direction.

    The allowed range is -127...+127

    I suspect this is because MoveMouseRelative starts by getting the current mouse position

    No.
    It does not ask the current mouse position.
    It invokes SendInput() without flag MOUSEEVENTF_ABSOLUTE to simulate relative mouse movement.

    how to quickly move the mouse 12,000 relative units on the x-axis instantaneously?

    for i = 0, 96 do
        MoveMouseRelative (125,0)
    end
    

    If I eliminate the Sleep function, the results are inconsistent. It usually rotates my character between 80-140 degrees.

    Try more precise version of Sleep()

    for i = 0, 96 do
        MoveMouseRelative (125,0)
        FastSleep(1)
    end