1
votes

I'm having a little problem with my script here using a Logitech mouse. I will be using it for farming in a game.

function OnEvent(event, arg)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
    for i = 0, 300 do
            PressAndReleaseKey("f9")
            Sleep(400)
            PressAndReleaseKey("enter")
            Sleep(600)
            PressAndReleaseKey("f5")
            Sleep(50)
            PressMouseButton(1)
            Sleep(50)
            ReleaseMouseButton(1)
    end
            PressAndReleaseKey("1")
    repeat
    until IsMouseButtonPressed(3)
   end
end

So it will loop for 300 times and then press 1 when it's done, then repeat the loop again for 300 times, so on & so on. Problem I'm facing is, when I'm trying to abort the script, it will first finish the for-loop before being stopped by using Right-click button(IsMouseButtonPressed(3)), which is really hard to time (300x is a lot)

How can I pause/stop it during the for-loop, would it be possible?

2
Your repeat...until loop is useless here. Remove it.Egor Skriptunoff
Welcome to Stack Overflow! Do not vandalize your posts. By posting on this site, you've irrevocably granted the Stack Exchange network the right to distribute that content under the CC BY-SA 4.0 license for as long as it sees fit to do so. For alternatives to deletion, see: I've thought better of my question; can I delete it?Sabito 錆兎

2 Answers

0
votes

Frequently check if the button is pressed and break the loop.

Break up those long blocking Sleeps.

Instead of Sleep(400) consider doing something like

for i = 1, 400, 50 do
  Sleep(50)
  if IsMouseButtonPressed(3) then break end
end
0
votes

You can use break to jump out of your for loop when IsMouseButtonPressed(3)

    for i = 0, 300 do
        if IsMouseButtonPressed(3) then
            break -- exit for loop.
        end
        PressAndReleaseKey("f9")
        Sleep(400)
        PressAndReleaseKey("enter")
        Sleep(600)
        PressAndReleaseKey("f5")
        Sleep(50)
        PressMouseButton(1)
        Sleep(50)
        ReleaseMouseButton(1)
    end

doing it like this means you can expect a maximum delay of 1.1 seconds due to the sleep calls, for the exit to be registered.


You could change your code by adding a function to poll IsMouseButtonPressed(3) during your sleep intervals.

local function MousePollingSleep(time)
    loopCount = time / 50
    for i = loopCount, 0, -1 do
        if IsMouseButtonPressed(3) then
            return false
        end
        sleepTime = (i >= 1 and 1 or i) * 50
        Sleep(sleepTime)
    end
    return true
end

and change your for loop to

function OnEvent(event, arg)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
      repeat
          for i = 0, 300 do
              PressAndReleaseKey("f9")
              if MousePollingSleep(400) == false then break end
              PressAndReleaseKey("enter")
              if MousePollingSleep(600) == false then break end
              PressAndReleaseKey("f5")
              if MousePollingSleep(50) == false then break end
              PressMouseButton(1)
              if MousePollingSleep(50) == false then break end
              ReleaseMouseButton(1)
          end
          ReleaseMouseButton(1)
          PressAndReleaseKey("1")
      until IsMouseButtonPressed(3)
   end
end