Search code examples
lualogitech-gaming-software

How to stop a repeated list of actions when releasing LMB


I have a list of actions as follows:

PressAndReleaseKey("f13") -- Action 1
Sleep(200)
PressAndReleaseKey("f13") -- Action 2
Sleep(200)
PressKey("f13") -- Action 3
Sleep(400)
ReleaseKey("f13") -- Action 4
Sleep(200)

I want to repeat these actions when pressing LMB and cancel all actions whenever release LMB

Eg:

  • if LMB is pressed for 300 ms and then released => only Action 1 and Action 2 will be executed (1 time),
  • if LMB is pressed for 1800 ms and then released => Action 1 and 2 will be executed twice, Action 3 will be executed once.

So pls help me, this is my script so far:

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)

   if event == "MOUSE_BUTTON_PRESSED" and arg == 1  then
        if IsMouseButtonPressed(1) then
            repeat
                PressAndReleaseKey("f13") -- Action 1
                Sleep(200)
                PressAndReleaseKey("f13") -- Action 2
                Sleep(200)
                PressKey("f13") -- Action 3
                Sleep(400)
                ReleaseKey("f13") -- Action 4
                Sleep(200)
            until not IsMouseButtonPressed(1)   
        end     
    end
end

(Option 2) In case such repeat is not possible => there is another more manual option like local that list actions up to 100 times


Solution

  • You cannot cancel any action. You can either execute it or not. Once it is triggered you cannot stop it.

    If you want to stop execution after Action 1 and Action 2 you have to check the button state after Action 2 and only execute Action 3 if the button is still pressed. Currently you're checking the button only after all actions have been executed.

    Place if not IsMouseButtonPressed(1) then break end befor any action so it will not be executed if the button is pressed.