如何在按下按钮时停止lua脚本中的重复执行?

我正在为游戏编写一个快速射击脚本。这是我的代码

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
  if IsKeyLockOn("capslock")then
    if  IsMouseButtonPressed(1)then
      repeat
        Sleep(math.random(30, 60))
        PressMouseButton(1)
        Sleep(math.random(30, 60))
        ReleaseMouseButton(1)

      until not IsMouseButtonPressed(1)
    end
  end
end

当capslock打开并且鼠标左键按住时,它会自动点击左键。 然而,有时即使我释放了鼠标左键,脚本仍会继续执行,那么有谁知道如何解决这个问题吗? 或者说,在按下“R”按钮或“shift”按钮时是否有一种方法可以停止重复循环? 谢谢

点赞
用户1847592
用户1847592

你不能同时模拟 LMB 按下和监听其状态。

最简单的解决方法是在游戏中将另一个键分配为开火键,而不是 LMB。

假设你已经将 P 键分配为开火键。

function OnEvent(event, arg)
   if event == "PROFILE_ACTIVATED" then
      EnablePrimaryMouseButtonEvents(true)
   elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
      if IsKeyLockOn("capslock") then
         repeat
            PressKey("P")
            Sleep(math.random(30, 60))
            ReleaseKey("P")
            Sleep(math.random(30, 60))
         until not IsMouseButtonPressed(1)
      end
      PressKey("P")
   elseif event == "MOUSE_BUTTON_RELEASED" and arg == 1 then
      ReleaseKey("P")
   end
end
2021-11-23 10:08:23
用户15398692
用户15398692

如果您正在使用LOVE2D,我建议您尝试love.mouse.isDown功能。您可以在此链接找到更多信息:https://love2d.org/wiki/love.mouse

2021-11-23 12:45:28