1

我是 Lua 语言的新手,只想为罗技鼠标按钮编写一个宏,所需的操作是:按下按钮启动并连续循环宏,直到我再次按下按钮将其关闭。

我知道一个类似的问题已经发布为: Lua handling mouse event。但是我很难运行他们的示例代码。有两个问题: 1,我可以通过按下按钮 5 开始循环,但 while 循环只能通过按住按钮 5 继续。如何设置 while 循环只需按一下然后释放?2、再按5号键,重复循环不能断。它仍然保持连续运行。

我也试过这段代码

script_running = false
function OnEvent(event, arg)
    if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
        if script_running then
            script_running = false
            return
        else
            script_running = true
        end
        repeat
            OutputLogMessage("repeat\n")
            Sleep(3000)
        until not script_running
        OutputLogMessage("end\n")
    end
end

重复循环仍然无法停止。

有人可以给出一些成熟的代码示例吗?非常感谢。

4

1 回答 1

1
local flag

function OnEvent(event, arg)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
      flag = not flag
      if flag then
         repeat
            -----------------------
            -- your actions here 
            OutputLogMessage("repeat\n")
            Sleep(1000)
            -----------------------
            Sleep(15)
            local prev_flag = flag
            flag = IsMouseButtonPressed(5)
         until not prev_flag and flag
      end
   end
end
于 2020-10-08T15:08:50.273 回答