Lua Logitech 如何让脚本在按钮按下时不重复执行?

我正在尝试使下面的代码在按下或保持鼠标按钮1时仅执行一次 MoveMouseRelative 函数。我尝试删除"repeat"行但它会破坏代码。当前激活并按住鼠标1时,光标会不断向下拖动。

function OnEvent(event, arg)
    OutputLogMessage("event = %s, arg = %d\n", event, arg)
    if (event == "PROFILE_ACTIVATED") then
        EnablePrimaryMouseButtonEvents(true)
    elseif event == "PROFILE_DEACTIVATED" then
        ReleaseMouseButton(2)  -- 为了防止被卡住
    end
    if (event == "MOUSE_BUTTON_PRESSED" and arg == 5) then
        recoilx2 = not recoilx2
        spot = not spot
    end
   if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and recoilx2) then
        if recoilx2 then
            repeat
                --Sleep(35)
                Sleep(5)
                MoveMouseRelative(0, 3)
            until not IsMouseButtonPressed(1)
        end
    end
点赞
用户1847592
用户1847592

目标:编写针对罗技鼠标的 Lua 脚本,执行以下任务:

当鼠标按钮 5 被“激活”时:

  • 在 1000 毫秒(射击间隔时间)内进行一次左击。
  • 并且每 1000 毫秒将鼠标向下拉一次。

所以,如果我按住左鼠标键,则它将持续射击,但只有在射击时才会向下拉。

选择一个在游戏中从未使用过的键盘按钮,并将其设置为驱动枪的唯一方式,该键将被用于编程式开火。

我假设这个键是 P,但是您可以选择任何其他键:“f12”、“退格键”、“num9”等。

在左鼠标键按下时,游戏不做任何操作。

local fire_button = "P"

function OnEvent(event, arg)
   OutputLogMessage("event = %s, arg = %d\n", event, arg)
   if event == "PROFILE_ACTIVATED" then
      EnablePrimaryMouseButtonEvents(true)
   elseif event == "PROFILE_DEACTIVATED" then
      -- 防止鼠标按钮卡住
      for j = 1, 5 do ReleaseMouseButton(j) end
   end
   if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
      recoilx2 = not recoilx2
   end
   if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
      PressKey(fire_button)
      Sleep(50)
      ReleaseKey(fire_button)
      if recoilx2 then
         while IsMouseButtonPressed(1) do
            MoveMouseRelative(0, 25)
            local next_shot_time = GetRunningTime() + 1000
            local LMB
            repeat
               Sleep(50)
               LMB = IsMouseButtonPressed(1)
            until not LMB or GetRunningTime() >= next_shot_time
            if LMB then
               PressKey(fire_button)
               Sleep(50)
               ReleaseKey(fire_button)
            end
         end
      end
   end
end
2020-03-04 05:46:00