AINER
AINER

Reputation: 3

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?

Upvotes: 0

Views: 1347

Answers (3)

SOF
SOF

Reputation: 1

Why is it so complicated? All you need is to disable button 4 (in G HUB ui) and check delay after button was released. So code would be:

local btn_pressed_at
function OnEvent(event, arg)
    OutputLogMessage("event = %s, arg = %s\n", event, arg)
    
    local btn_id = 4

    if event == "MOUSE_BUTTON_PRESSED" and arg == btn_id then
        btn_pressed_at = GetRunningTime()
    end
    
    if event == "MOUSE_BUTTON_RELEASED" and arg == btn_id then
      if GetRunningTime() > btn_pressed_at + 200 then
        PressKey("lgui")
        PressAndReleaseKey("V")
        ReleaseKey("lgui")
      else
        PressKey("lctrl")
        PressAndReleaseKey("V")
        ReleaseKey("lctrl")
      end
    end
end

Upvotes: 0

AINER
AINER

Reputation: 3

Final script. Thanks to ESkri.

function OnEvent(event, arg)
    OutputLogMessage("event = %s, arg = %s\n", event, arg)

    -- Ctrl + V / Win + V
    if event == "MOUSE_BUTTON_PRESSED" and arg == 10 then
        local tm = GetRunningTime() + 200
        repeat
            Sleep(10)
            if not IsModifierPressed("rctrl") then   -- 10 = Right Ctrl
                PressKey("lctrl")
                PressAndReleaseKey("V")
                ReleaseKey("lctrl")
                return
            end
        until GetRunningTime() > tm
        ReleaseKey("rctrl")
        PressKey("lgui")
        PressAndReleaseKey("V")
        ReleaseKey("lgui")
    end

    -- Ctrl + C / Ctrl + X
    if event == "MOUSE_BUTTON_PRESSED" and arg == 11 then
        local tm = GetRunningTime() + 200
        repeat
            Sleep(10)
            if not IsModifierPressed("rctrl") then   -- 11 = Right Ctrl
                PressKey("lctrl")
                PressAndReleaseKey("C")
                ReleaseKey("lctrl")
                return
            end
        until GetRunningTime() > tm
        ReleaseKey("rctrl")
        PressKey("lctrl")
        PressAndReleaseKey("X")
        ReleaseKey("lctrl")
    end
end

Upvotes: 0

ESkri
ESkri

Reputation: 1928

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

Upvotes: 0

Related Questions