Logitech Lua 当按下 G 键时重复执行

大家好,我想编写一个 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
点赞
用户1847592
用户1847592

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

步骤1。

确保你在游戏中没有使用MouseButton#4(“后退”)。

如果你在游戏中没有使用MB#4,可以跳过“步骤1”。

如果在游戏中为MB#4分配了某些操作,请执行以下操作:

  • 选择一个在游戏中尚未使用的键盘按钮

    (假设当前未使用F12键)

  • 前往GHUB(鼠标设备,分配部分,键盘选项卡);

    绑定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
2020-12-13 14:44:47