添加脚本冷却时间

function OnEvent(event, arg)

EnablePrimaryMouseButtonEvents(1)

    if (event == "MOUSE_BUTTON_PRESSED" and arg == 1) do
        PressAndReleaseKey("lalt")
            Sleep(15000)
end
end

我有一个脚本,在我按下左键时按下Alt键。但是我不希望每次都执行。

我想添加一个15秒的“冷却时间”,这样脚本不会在15秒内重复执行。

我添加的sleep函数在PressAndReleaseKey之前运行。

我该如何交换它们的顺序?

点赞
用户1847592
用户1847592
local prev_time = -math.huge

function OnEvent(event, arg)
   if event == "PROFILE_ACTIVATED" then
      EnablePrimaryMouseButtonEvents(true)
   elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
      local this_time = GetRunningTime()
      if this_time - prev_time > 15000 then
         prev_time = this_time
         PressKey("lalt")
         Sleep(50)
         ReleaseKey("lalt")
      end
   end
end
local prev_time = -math.huge

-- 当有事件触发时,执行该函数
function OnEvent(event, arg)
   -- 如果激活了该配置文件
   if event == "PROFILE_ACTIVATED" then
      -- 启用鼠标左键事件
      EnablePrimaryMouseButtonEvents(true)
   -- 如果鼠标按键按下,并且是左键(arg=1)
   elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
      -- 获取当前时间
      local this_time = GetRunningTime()
      -- 如果距离上次按下时间超过了15秒
      if this_time - prev_time > 15000 then
         -- 记录该次按下时间
         prev_time = this_time
         -- 按下并释放左alt键(鼠标左键无法直接代替alt键)
         PressKey("lalt")
         Sleep(50)
         ReleaseKey("lalt")
      end
   end
end
2020-06-08 15:07:46