Logitech Lua 按住 G 键时重复

Logitech Lua Repeat while G-Key is being held

大家好,我想写一个 Lua 脚本,只要我按住一个特定的键(最好是 G 键,但并不重要)并立即停止,它就会重复下面的代码块当我释放钥匙时。作为一种解决方法,我只是让它重复 6 次并停止,但这实际上不是我想要的。

非常感谢!

if event == "G_PRESSED" and arg == 5  then


 for i=1,6 do
if i == 1 then
    end
         PressKey("r")
    Sleep(math.random(50, 100)) 
    ReleaseKey("r")
    Sleep(math.random(2300, 2450)) 
    PressKey("r")
    Sleep(math.random(50, 100)) 
    ReleaseKey("r")
    Sleep(math.random(100, 175)) 
if i == 6 then
    OutputLogMessage("Finished!... ")
end
end
end

我假设您使用的是 GHUB 并且同时拥有罗技键盘和罗技鼠标(这两种设备都可以通过 GHUB 进行编程)。

步骤 1.
确保您没有在游戏中使用 MouseButton#4(“后退”)。
如果您在游戏中不使用 MB#4,请跳过“步骤 1”。
如果在游戏中为 MB#4 分配了一些动作,请执行以下操作:

  • 选择您当前未在游戏中使用的键盘按钮
    (假设当前未使用 F12 键)
  • 转到 GHUB(鼠标设备、分配部分、KEYS 选项卡);
    F12 绑定到您的物理 MB#4
  • 转到游戏选项;
    将旧操作绑定到 F12 而不是 MB#4

现在,当您按下物理 MB#4 时,游戏会看到 F12 并激活您的旧操作。


步骤 2.
转到 GHUB(鼠标设备,分配部分)
从 MB#4 解除绑定“返回”(如果您在第 1 步中尚未完成)


步骤 3.
转到 GHUB(键盘设备、分配部分、系统选项卡)
将“返回”绑定到键 G5


步骤 4.
剧本.

function OnEvent(event, arg)
   if event == "G_PRESSED" and arg == 5 then
      Sleep(10)
      while IsMouseButtonPressed(4) do
         PressKey("r")
         Sleep(math.random(50, 100)) 
         ReleaseKey("r")
         Sleep(math.random(2300, 2450)) 
         PressKey("r")
         Sleep(math.random(50, 100)) 
         ReleaseKey("r")
         Sleep(math.random(100, 175)) 
      end
      OutputLogMessage("Finished!...\n")
   end
end

更新:
切换 G5 的脚本:

local state, prev_btn, prev_rel

local function Sleep2(delay)
   local tm = GetRunningTime() + delay
   while true do
      local time_left = tm - GetRunningTime()
      if time_left <= 0 then
         return
      end
      Sleep(math.min(time_left, 20))
      local curr_btn = IsMouseButtonPressed(4)
      local curr_rel = prev_btn and not curr_btn
      state, prev_btn, prev_rel = state or prev_rel and curr_rel, curr_btn, prev_rel or curr_rel
   end
end

function OnEvent(event, arg)
   if event == "G_PRESSED" and arg == 5 then
      state = not state
      if state then
         Sleep(10)
         state = IsMouseButtonPressed(4)
         if state then
            prev_btn, prev_rel, state = state
            repeat
               PressKey("r")
               Sleep2(math.random(50, 100))
               ReleaseKey("r")
               Sleep2(math.random(2300, 2450))
               PressKey("r")
               Sleep2(math.random(50, 100))
               ReleaseKey("r")
               Sleep2(math.random(100, 175))
            until state
            OutputLogMessage("Finished!...\n")
         end
      end
   end
end