我释放Logitech G Hub脚本后立即停止

问题描述

是否可以使代码在我释放鼠标按钮1后立即停止运行,而不必等到循环完成?

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event,arg)
  if IsKeyLockOn("capslock")then
    if IsMouseButtonpressed(1) then
      repeat
        MoveMouseRelative(-69,99)
        Sleep(133)
        MoveMouseRelative(11,91)
        Sleep(133)
        MoveMouseRelative(-109,84)
        Sleep(133)
        MoveMouseRelative(-86,75)
        Sleep(133)
        MoveMouseRelative(1,64)
        Sleep(133)
        MoveMouseRelative(33,56)
        Sleep(133)
        MoveMouseRelative(58,47)
        Sleep(133)
        MoveMouseRelative(75,39)
        Sleep(133)
        MoveMouseRelative(84,29)
        Sleep(133)
        MoveMouseRelative(83,18)
        Sleep(133)
        MoveMouseRelative(75,20)
        Sleep(133)
        MoveMouseRelative(60,36)
        Sleep(133)
        MoveMouseRelative(33,49)
        Sleep(133)
        MoveMouseRelative(3,55)
        Sleep(133)
        MoveMouseRelative(-32,69)
        Sleep(133)
        MoveMouseRelative(-55,64)
        Sleep(133)
      until not IsMouseButtonpressed(1)
    end 
  end
end

所以即使我单击了鼠标左键,我也要等到循环结束,所以我想知道是否有办法让程序在我放开鼠标左键的那一刻就停止

解决方法

要获得更具响应性的脚本,您需要收紧循环。

您将在循环体内重复很多相同的功能(MoveMouseRelativeSleep)。如果您简化了循环主体,则可以让它在每个Sleep

之后检查释放条件
EnablePrimaryMouseButtonEvents(true);
function OnEvent(event,arg)
  if IsKeyLockOn("capslock")then
    if IsMouseButtonPressed(1) then
      local positions = {             -- Move `MoveMouseRelative` args to an 2d array.
          {-69,99},{11,91},{-109,84},{-86,75},{1,64},{33,56},{58,47},{75,39},{84,29},{83,18},20},{60,36},49},{3,55},{-32,69},{-55,}
      local index = 1
      repeat
        MoveMouseRelative(positions[index][1],positions[index][2])        
        Sleep(133)
        index = (index % #positions) + 1 -- loop index when it reaches the array length.
      until not IsMouseButtonPressed(1)
    end 
  end
end