lualogitech-gaming-software

Using a Lua G-hub mouse click script


I want to click a mouse5 button and let a script do a loop that press a keyboard Y key with a random delay time. Until I click a mouse button5 again it must stop a loop. I have tried for a while and can't fix it, it became a infinite loop.

Here the code I tried to write:

function OnEvent(event, arg) 
    OutputLogMessage("event = %s, arg = %s\n", event, arg) 
    -- mousebutton 5 has been pressed
    if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then                
        for i=1,10 do         
            PressKey("y")
            Sleep(math.random(60, 130))
            ReleaseKey("y")  
            if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
                break
            end         
        end      
    end
end

Solution

  • You can use IsMouseButtonPressed(5) to determine if MB5 is pressed for the second time.

    local is_active
    
    function OnEvent(event, arg)
       OutputLogMessage("event = %s, arg = %s\n", event, arg)
       if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
          is_active = not is_active
          if is_active then
             repeat
                Sleep(math.random(30, 60))
                PressKey("y")
                Sleep(math.random(30, 60))
                ReleaseKey("y")
                local MB5, second_MB5_press = IsMouseButtonPressed(5)
                second_MB5_press, is_active = MB5 and not is_active, MB5
             until second_MB5_press
          end
       end
    end