【罗技】【Lua】如何强制退出循环

我想知道在循环中如何通过特定的按键强制退出循环,而不是等到循环结束后再退出。

我的想法是这样的:

当我按下第七个按键时,中止宏(i=4),其中包括从1到4的迭代过程。

完整脚本如下:

local isMacroRunning = false

co = coroutine.create(function()
    while true do
        if not isMacroRunning then break end
        MoveMouseRelative (5, 0)
        Sleep(150)
        MoveMouseRelative (-5, 0)
        Sleep(150)
        OutputLogMessage("Break\n")
        coroutine.yield()
    end
end)

function OnEvent(event, arg)
    if (event == "MOUSE_BUTTON_PRESSED" and arg == 8) then
        isMacroRunning = true
        RunMacro()
    elseif(event == "MOUSE_BUTTON_PRESSED" and arg == 7 and isMacroRunning) then
        isMacroRunning = false
        RunMacro()
    end
end

function RunMacro()
    if isMacroRunning then
        coroutine.resume(co)
        OutputLogMessage("Start Macro\n")
        for i = 1, 10 do
            OutputLogMessage("i = %d\n",i)
            Sleep(200)
        end
    else
        OutputLogMessage("Aborted\n")
    end
end

coroutine.resume(co)
点赞
用户2858170
用户2858170

为什么不在循环内部检查按钮是否被按下?

如果鼠标按钮被按下(IsMouseButtonPressed(7)),那么跳出循环(break)。
2021-04-14 17:02:26