0

I wrote a simple Lua script that left clicks after I click mouse5. The issue is, on line 12 if I include 'not' it will only repeat once, if I remove it, it will repeat forever. I want to start and end the script with mouse5 if possible. I know people have had similar issues to mine before, but I was unable to find a solution. Any ideas?

I am using the Logitech G Hub API: (https://douile.github.io/logitech-toggle-keys/APIDocs.pdf)

And this for my loop: (https://www.tutorialspoint.com/lua/lua_repeat_until_loop.htm)

My code:

function OnEvent(event, arg)
    OutputLogMessage("Event: "..event.." Arg: "..arg.."\n")
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
    if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
        repeat
            Sleep(math.random(1046, 1292))
            PressMouseButton(1)
            Sleep(math.random(27, 78)) 
            ReleaseMouseButton(1)
            Sleep(math.random(314, 664))
        until not IsMouseButtonPressed(5)
        end    
    end
end
1
  • Your code should work correctly. Please note that G5 mouse button must be bound to "Forward" action in GHUB interface. This is the default setting, You probably have changed it manually. Commented May 25, 2021 at 0:32

1 Answer 1

0

Notice your event handler function is declared a second time within itself on line 4.

Also, EnablePrimaryMouseButtonEvents() does not have to be called multiple times or with each input event - it can be placed outside the function where it will run just once. Something like the first or last line is probably a good idea.

Finally, I would argue that top-down conditions are a bit more clear. So how about giving yourself a tiny bit of time to release the button and perform a while loop if you do. If you hold the button during the re-check of the button state, it will stop the loop :

EnablePrimaryMouseButtonEvents(true)
function OnEvent(e,a)
    OutputLogMessage("Event: "..e.." Argument: "..a.."\n")
    if e=="MOUSE_BUTTON_PRESSED" and a==5 then
    Sleep(200)
    while not IsMouseButtonPressed(5) do
        Sleep(math.random(1046,1292))
        -- maybe add another check here "if IMBP(5) then return end"
        PressMouseButton(1)
        Sleep(math.random(27,78)) 
        ReleaseMouseButton(1)
        Sleep(math.random(314,664))
    end end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.