luamouselogitechlogitech-gaming-software

How to make this Logitech Mouse LUA script work better?


I want to make a simple macro in LUA to Logitech mouse. I am complete novice and I just pasted few lines together from few sources.

EnablePrimaryMouseButtonEvents(true);

function OnEvent(event, arg)
    if IsKeyLockOn("numlock") then
        if IsMouseButtonPressed(3) then
            repeat
                if IsMouseButtonPressed(1) then
                    repeat
                        PressKey("P")
                        MoveMouseRelative(0, 13)
                        Sleep(10)
                        MoveMouseRelative(0, 13)
                        Sleep(10)
                        MoveMouseRelative(0, 13)
                        Sleep(10)
                        MoveMouseRelative(0, 13)
                        Sleep(10)
                        MoveMouseRelative(0, 13)
                        Sleep(10)
                        ReleaseKey("P")

                        Sleep(200)
                    until not IsMouseButtonPressed(1)
                end
            until not IsMouseButtonPressed(3)

        elseif not IsMouseButtonPressed(3) then
            repeat
                if IsMouseButtonPressed(1) then
                    repeat
                        PressKey("P")
                        MoveMouseRelative(0, 15)
                        Sleep(30)
                        MoveMouseRelative(0, 12)
                        Sleep(10)
                        MoveMouseRelative(0, 12)
                        Sleep(10)
                        MoveMouseRelative(0, 12)
                        Sleep(10)
                        MoveMouseRelative(0, 12)
                        Sleep(130)
                        ReleaseKey("P")
                        Sleep(200)
                    until not IsMouseButtonPressed(1)
                end
            until IsMouseButtonPressed(3)
        end
    end
end

First of all, it looks like a mess but kind of works, sorry about it I am complete novice to it. Now the problems I am having, is that;

  1. I used numlock to enable or disable the whole thing, because I could not figure out how to turn it ON and OFF with single MouseButtonKey, it either played forever, or I broke the whole thing. Which is ok, if not for a weird bug that after I turn off numlock I have to press MouseButton 2 to turn it off for some reason.

  2. I thought that what I want to achieve would be really simple, I want to have 4 functions that;

Function 1 MoveMouseRelative repeated every ()ms
Function 2 MoveMouseRelative repeated every ()ms
Function 3 PressKey("P") repeated every ()ms
Function 4 PressKey("P") repeated every ()ms

And combine it that so If I press MouseButtonPressed(1) it does Function 1+3, so it clicks P and drags mouse down at the same time, but with different sleep() intervals that would be stated in separate functions and else if I press and MouseButtonPressed(3)(Right click) + MouseButtonPressed(1) it would do 2+4 functions instead, and would repeat while holding and stop repeating if I released buttons.

Is that even possible? I got sucked into this code and can't stop thinking how to fix for last week, but having no education and simply guessing how thing would play out I hit a wall I cannot breach.

Thank you very much for any help if all.


Solution

  • EnablePrimaryMouseButtonEvents(true)
    
    local sequence_LMB     = {{x=0,y=15, t=30}, {x=0,y=12, t=10}, {x=0,y=12, t=10}, {x=0,y=12, t=10}, {x=0,y=12, t=130}}
    local sequence_LMB_RMB = {{x=0,y=13, t=10}, {x=0,y=13, t=10}, {x=0,y=13, t=10}, {x=0,y=13, t=10}, {x=0,y=13, t=10}}
    
    function OnEvent(event, arg)
       if event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsKeyLockOn("numlock") then
          repeat
             PressKey("P")
             local sequence = IsMouseButtonPressed(3) and sequence_LMB_RMB or sequence_LMB
             for i, step in ipairs(sequence) do
                MoveMouseRelative(step.x, step.y)
                Sleep(step.t)
             end
             ReleaseKey("P")
             Sleep(200)
          until not IsMouseButtonPressed(1)
       end
    end