0

Tell me how can I make my script run indefinitely when I hold down the left mouse button until I release the left mouse button. I tried using chatgpt, but it gave crooked answers and nothing worked.

function main()
    -- Define the number of repetitions
    local repetitions = 10

    for i = 1, repetitions do
        -- Mouse left button down
        PressAndReleaseMouseButton(1)  -- Left down
        Sleep(30)                       -- Delay 30 ms

        -- Mouse left button up
        ReleaseMouseButton(1)           -- Left up
        Sleep(5)                        -- Delay 5 ms

        -- Move the mouse
        MoveMouseRelative(0, 2)         -- Move right 0, down 2
    end
end

main()

And that's what chatgpt gave me:

-- Define the number of repetitions
local repetitions = 10

function main()
    OutputLogMessage("Hold 'LMB' to run the script. Release to stop.\n")

    while true do
        -- Check if the left mouse button is held down
        if IsMouseButtonPressed(1) then
            OutputLogMessage("LMB is pressed. Running actions...\n")
            
            for i = 1, repetitions do
                -- Mouse left button down
                PressAndReleaseMouseButton(1)  -- Left down
                Sleep(30)                       -- Delay 30 ms

                -- Mouse left button up
                ReleaseMouseButton(1)           -- Left up
                Sleep(5)                        -- Delay 5 ms

                -- Move the mouse
                MoveMouseRelative(0, 2)         -- Move right 0, down 2
            end
            
            -- Optionally, log that the actions are complete
            OutputLogMessage("Completed %d repetitions.\n", repetitions)
            
            -- Optional: Wait a moment before checking the button again
            Sleep(100)  -- Delay to prevent rapid retriggering
        end

        Sleep(10)  -- Prevent high CPU usage when the button is not pressed
    end
end

main()

Yes, it works, but not the way I need it.

1
  • That question is very similar to your. Does that answer solve your problem? Commented Nov 20 at 15:41

0

Your Answer

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

Browse other questions tagged or ask your own question.