lualogitechlogitech-gaming-software

How to get this logitech lua script to move my mouse left, then to the right and keep on repeating that until mouse button 1 is no longer pressed


I have picked up learning lua. This is my first programming language. I have written this script myself using logitechs lua api documentation to learn the language.

I wrote 2 similar codes to try and approach my problem from two different ways. Both have failed.

Code#1

EnablePrimaryMouseButtonEvents(true)

function OnEvent(event, arg)
    --OutputLogMessage("Event: "..event.." Arg: "..arg.."\n")
    if IsKeyLockOn("capslock") then
        repeat
            if IsMouseButtonPressed(1) then
                repeat
                    PressAndReleaseKey("p")
                    Sleep(10)
                    MoveMouseRelative(-10,0)
                    Sleep(10)
                    MoveMouseRelative(10,0)
                    Sleep(10)
                until not IsMouseButtonPressed(1)
            end
        until not IsKeyLockOn("capslock")
    end
end

This code is meant to repeatedly press the keyboard button "p" and repeatedly move the mouse left, then right until LMB is no longer being pressed. The method I was going for was to use MoveMouseRelative to go left, then pause for 10ms-Sleep(10) then use MoveMouseRelative to go right and just repeat that loop. What I noticed is that it is moving from left to right too fast, and when I increased the delay of the sleep, it also increased the delay of "P" being pressed. So I think I need to find a way to make the program move from left to right slower, without affecting the speed at which "P" is pressed

Now the 2nd attempt Code#2

EnablePrimaryMouseButtonEvents(true)

function OnEvent(event, arg)
    --OutputLogMessage("Event: "..event.." Arg: "..arg.."\n")
    if IsKeyLockOn("capslock") then
        repeat
            if IsMouseButtonPressed(1) then
                repeat
                    PressAndReleaseKey("p")
                    Sleep(10)
                        for i = 1, 20 do
                            MoveMouseRelative(10,0)
                        end
                        for i = 1,20 do
                            MoveMouseRelative(-10,0)
                        end
                until not IsMouseButtonPressed(1)
            end
        until not IsKeyLockOn("capslock")
    end
end

This code does the same thing basically and it does it in the way I want pretty much. The problem is, when I hold down my mouse button for long, when I release the mouse button my mouse is still moving from left to right for a while. So I need a way to make it so that when I release my mouse button it stops moving from left to right no matter what.


Solution

  • Regarding your earlier comment about making the mouse move smoothly, could you elaborate more on what you mean by repeating move a bit + sleep a bit in a loop.

    Here is an example how to make smoothest mouse movement

    First, define the following function before OnEvent:

    function SmoothMouseMovement(x_speed, y_speed, duration)
        local last_x, last_y = 0, 0
        local tm_start = GetRunningTime()
        repeat
            Sleep(5)
            if not IsMouseButtonPressed(1) then break end
            local tm = math.min(GetRunningTime() - tm_start, duration)
            local x = math.floor(x_speed * tm)
            local y = math.floor(y_speed * tm)
            if x ~= last_x or y ~= last_y then
                MoveMouseRelative(x - last_x, y - last_y)
                last_x = x
                last_y = y
            end
        until tm >= duration
    end
    

    Second, replace your old loops for i = 1,20 do with the following code:

    local x_speed = 1.5  -- pixels per millisecond
    local duration = 200 -- milliseconds
    SmoothMouseMovement(x_speed, 0, duration)
    local x_speed = -1.5  -- pixels per millisecond
    local duration = 200  -- milliseconds
    SmoothMouseMovement(x_speed, 0, duration)