如何配置鼠标宏实现首次执行Block A后重复执行Block B的循环逻辑
Solution for Your Mouse Macro Execution Flow
I see exactly what you need here—first run Block A once when you press the mouse button, then keep repeating Block B until you let go. Let's refactor your code to make this logic work cleanly.
Key Changes We'll Make:
- Define the
InterruptableSleepfunction once instead of duplicating it in both blocks - Wrap Block A and Block B into separate reusable functions (
RunBlockA()andRunBlockB()) - Control the execution flow in
OnEvent: first run Block A, then loop Block B until the mouse button is released
Here's the revised code:
-- Define this helper function once, no need to repeat it local function InterruptableSleep(ms) local tm = GetRunningTime() + ms while GetRunningTime() < tm do Sleep(5) if not IsMouseButtonPressed(4) then return true end end end -- Block A logic wrapped in a function local function RunBlockA() PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("3") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(500) then return true end PressAndReleaseKey("0") if InterruptableSleep(400) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("4") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("3") if InterruptableSleep(500) then return true end PressAndReleaseKey("0") if InterruptableSleep(400) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("5") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(500) then return true end PressAndReleaseKey("0") if InterruptableSleep(400) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("R") if InterruptableSleep(500) then return true end PressAndReleaseKey("0") if InterruptableSleep(400) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("4") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("4") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("4") end -- Block B logic wrapped in a function local function RunBlockB() -- FIRST ROUND PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(900) then return true end if InterruptableSleep(500) then return true end PressAndReleaseKey("0") if InterruptableSleep(400) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("3") if InterruptableSleep(900) then return true end if InterruptableSleep(500) then return true end PressAndReleaseKey("0") if InterruptableSleep(400) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("5") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("4") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("3") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("4") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("4") if InterruptableSleep(900) then return true end -- SECOND ROUND PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(900) then return true end if InterruptableSleep(500) then return true end PressAndReleaseKey("0") if InterruptableSleep(400) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("3") if InterruptableSleep(900) then return true end if InterruptableSleep(500) then return true end PressAndReleaseKey("0") if InterruptableSleep(400) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("5") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("4") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("4") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("2") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("4") if InterruptableSleep(900) then return true end PressAndReleaseMouseButton(1) if InterruptableSleep(math.random(110, 125)) then return true end PressAndReleaseKey("3") if InterruptableSleep(900) then return true end end function OnEvent(event, arg) if event == "MOUSE_BUTTON_PRESSED" and arg == 20 then -- First run Block A once local interrupted = RunBlockA() -- If Block A wasn't interrupted, start repeating Block B while not interrupted and IsMouseButtonPressed(4) do interrupted = RunBlockB() end end end
How It Works:
- Helper Function:
InterruptableSleepis defined once, so we don't have redundant code. It checks if the mouse button is released during sleep and returnstrueif interrupted. - Block Functions:
RunBlockA()andRunBlockB()contain your original code, each returningtrueif any sleep was interrupted (meaning you released the button mid-execution). - Execution Flow: When you press the button (
MOUSE_BUTTON_PRESSEDevent witharg == 20), we first runRunBlockA(). If it finishes without being interrupted, we enter a loop that keeps runningRunBlockB()until either the block is interrupted or you release the mouse button.
This exactly matches your desired flow: A → B → B → B → ... until you let go of the button.
内容的提问来源于stack exchange,提问作者Maike




