0

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?

AINER
  • 3
  • 3

2 Answers2

0

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
ESkri
  • 1,461
  • 1
  • 1
  • 8
  • This works but cannot be used in the browser and other programs that use "back" action. A short press triggers "back" action and paste (Ctrl+V) at the same moment. – AINER Mar 30 '23 at 12:15
  • Actions to be abused: Back, Forward, MiddleMouseButtonClick, LeftAlt, RightAlt, LeftCtrl, RightCtrl, LeftShift, RightShift. One of these actions has to be assigned to physical button #10. – ESkri Mar 30 '23 at 12:56
  • I'm not entirely sure that I understood correctly. How can this be configured, for example, for RightCtrl? – AINER Mar 30 '23 at 17:55
  • Replace `if not IsMouseButtonPressed(4) then` with `if not IsModifierPressed("rctrl") then` – ESkri Mar 31 '23 at 01:06
  • I replaced the string, added the command RightCtrl for the physical #10 button (GHUB: Assignments -> Keys -> "Right Ctrl") . But hold action is not executed. – AINER Mar 31 '23 at 11:12
  • This is because the hold action is now "RCtrl+Win+V" instead of "Win+V" (you are still keeping Btn#10=RCtrl pressed) – ESkri Mar 31 '23 at 14:10
  • Try to insert `ReleaseKey("rctrl")` or `PressAndReleaseKey("rctrl")` right before `PressKey("lgui")` – ESkri Mar 31 '23 at 14:15
0

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
AINER
  • 3
  • 3