You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何配置鼠标宏实现首次执行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 InterruptableSleep function once instead of duplicating it in both blocks
  • Wrap Block A and Block B into separate reusable functions (RunBlockA() and RunBlockB())
  • 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:

  1. Helper Function: InterruptableSleep is defined once, so we don't have redundant code. It checks if the mouse button is released during sleep and returns true if interrupted.
  2. Block Functions: RunBlockA() and RunBlockB() contain your original code, each returning true if any sleep was interrupted (meaning you released the button mid-execution).
  3. Execution Flow: When you press the button (MOUSE_BUTTON_PRESSED event with arg == 20), we first run RunBlockA(). If it finishes without being interrupted, we enter a loop that keeps running RunBlockB() 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

火山引擎 最新活动