Search code examples
windowslualogitech

Logitech Lua Script: 2 different functions for one-time press and hold action


I was trying to create a script for two different actions on the same button for single press and hold.

Now I use this script.


function OnEvent(event, arg)
    OutputLogMessage("event = %s, arg = %s\n", event, arg)
    if (event == "MOUSE_BUTTON_PRESSED" and arg == 10) then
        timeStart = GetRunningTime()
    elseif (event == "MOUSE_BUTTON_RELEASED" and arg == 10) then
        --Measure time elapsed since button_pressed event.
        elapsed = GetRunningTime() - timeStart
        OutputLogMessage("held for %dms\n",elapsed)
        if (elapsed >= 200) then
             PressKey("lgui")
             PressAndReleaseKey("V")
             ReleaseKey("lgui")
        elseif (elapsed < 200) then
             PressKey("lctrl")
             PressAndReleaseKey("V")
             ReleaseKey("lctrl")
        end
    end
end

But second action (Win+V) occurs only after #10 button is released.

What should be in the script so that the second combination (Win + V) occurs automatically after 200 milliseconds, without having to release the button?


Solution

  • Open LGS/GHUB application, find a big picture of mouse and assign "Back" action for physical button #10.
    ("Back" is the default action for button #4).

    function OnEvent(event, arg)
        OutputLogMessage("event = %s, arg = %s\n", event, arg)
        if event == "MOUSE_BUTTON_PRESSED" and arg == 10 then
            local tm = GetRunningTime() + 200
            repeat
                Sleep(10)
                if not IsMouseButtonPressed(4) then   -- 4 = Back
                    PressKey("lctrl")
                    PressAndReleaseKey("V")
                    ReleaseKey("lctrl")
                    return
                end
            until GetRunningTime() > tm
            PressKey("lgui")
            PressAndReleaseKey("V")
            ReleaseKey("lgui")
        end
    end