Search code examples
lualogitechlogitech-gaming-software

Logitech Lua Repeat while G-Key is being held


Hello guys I want to write a Lua Script thats repeating the codeblock below as long as I'm holding a specific key (best would be G-Key, but is not that important) and instantly stopps when I release the key. As a workaround I just made it that it repeats 6 times and stops, but thats not actually what I want.

Thank you very much!

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

Solution

  • I assume you are using GHUB and having both a Logitech keyboard and a Logitech mouse (both devices are programmable via GHUB).

    Step 1.
    Make sure you're not using MouseButton#4 ("back") in the game.
    If you don't use MB#4 in the game, just skip "Step 1".
    If some action is assigned to MB#4 in the game, do the following:

    • choose keyboard button you don't currently use in the game
      (let assume the F12 key is not currently used)
    • goto GHUB (mouse device, assignments section, KEYS tab);
      bind F12 to your physical MB#4
    • goto game options;
      bind the old action to F12 instead of MB#4

    Now when you press physical MB#4, the game sees F12 and activates your old action.


    Step 2.
    Goto GHUB (mouse device, assignments section)
    Unbind "Back" from MB#4 (if you have not done it already in Step 1)


    Step 3.
    Goto GHUB (keyboard device, assignments section, SYSTEM tab)
    Bind "Back" to key G5


    Step 4.
    The script.

    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
    

    UPDATE:
    Script for toggling 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