luamacroslogitech-gaming-software

Why does loop dont work in below Lua code?


So I am trying to write a simple lua script for my project where I need to slide my mouse continuously upwards so that I can draw vertical lines easily when I press LMB but catch is that I want this to work only when RMB is pressed and held. So the flow would be like I press and hold RMB and then press LMB which will constantly slide my mouse upward until Either LMB or RMB is released. So for doing this I came up with below code. Also I have added Num lock so I just toggle this script when I need it.

function OnEvent(event, arg)
    if IsKeyLockOn("numlock") then -- check if Num Lock is on
        if event == "MOUSE_BUTTON_PRESSED" and arg == 2 then -- check if RMB is pressed
            repeat
                if IsMouseButtonPressed(1) then -- check if LMB is pressed
                    MoveMouseRelative(0, -10) -- move mouse upwards by 10 pixels
                    Sleep(10)
                end
            until not IsMouseButtonPressed(2) -- keep moving mouse while RMB is held down
        end
    end
end

But this code seems to not work. Maybe someone can point me where I m going wrong. Thanks!

I m expecting is that when I press and hold RMB this script starts working such that only when ever I press and hold LMB its starts sliding mouse upwards until LMB or RMB is released.


Solution

  • Change button no from 2 to 3:

    function OnEvent(event, arg)
        if IsKeyLockOn("numlock") then -- check if Num Lock is on
            if event == "MOUSE_BUTTON_PRESSED" and arg == 2 then -- check if RMB is pressed
                repeat
                    if IsMouseButtonPressed(1) then -- check if LMB is pressed
                        MoveMouseRelative(0, -10) -- move mouse upwards by 10 pixels
                    end
                    Sleep(10)
                until not IsMouseButtonPressed(3) -- keep moving mouse while RMB is held down
            end
        end
    end