lualogitechlogitech-gaming-software

Ghub Script: Drag mouse down and autocliking by pressing left mouse button (LUA)


So basically I was trying to make the thing explained in the title using Ghub Lua scripting mechanics, posting the code below of what I have rn:

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
if IsKeyLockOn("capslock")then
if  IsMouseButtonPressed(1)then
   repeat
   MoveMouseRelative(0,4)
   PressMouseButton(1)
   Sleep(1,2)
   ReleaseMouseButton(1)
   until not IsMouseButtonPressed(1)
end
end
end

The problem is, sometimes it just stops midway going down, sometimes it just doesnt click at all. Have tried multiple ways of going around this, including changing what button is being pressed on the mouse, for now it's IsMouseButtonPressed(5), which works fine, but my goal is to get it working while pressing MouseButton(1). If it was any possible, I'd appriciate any help from you guys, thanks in advance, hope I gave you all necessary info.


Solution

  • The problem is because it is impossible to simultaneously simulate LMB press/release and monitor its state.
    The workaround is below:

    Step 0.
    You are about to modify the behavior of Left Mouse Button.
    This is a potentially dangerous operation: you can do almost nothing on your computer without LMB.
    So you must create a "spare LMB".
    For example, if you don't use Mouse Button 8, you can make it acting like a clone of LMB.
    Go to GHUB (mouse device, "Assignments" screen, SYSTEM tab).
    Assign "Primary click" to your physical MB#8.
    Now, if something goes wrong and your LMB stops working, you can press MB#8 instead of LMB.


    Step 1.
    Do you use Mouse Button 4 ("back") in the game?


    Step 2.
    You have to remap game action from MB#4 to some other key.
    Do the following:

    Now when you press physical MB#4, the game receives F12 and activates the game action.
    Skip "Step 3" and proceed to "Step 4".


    Step 3.
    Go to GHUB (mouse device, "Assignments" screen).
    Unassign standard command "Back" from physical MB#4 (click and select DISABLE from the drop-down menu).
    Disabled MB#4 will look like a white circle with black inside.


    Step 4.
    Set the script (see below).


    Step 5.
    Go to GHUB (mouse device, "Assignments" screen, SYSTEM tab).
    Assign "Back" to your physical LMB.
    You will see a warning about a potentially dangerous operation.
    Allow this operation because you have the "spare LMB" if something goes wrong.

    function OnEvent(event, arg)
       if event == "PROFILE_ACTIVATED" then
          EnablePrimaryMouseButtonEvents(true)
       elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
          PressMouseButton(1)
          if IsKeyLockOn("capslock") then
             repeat
                MoveMouseRelative(0,4)
                Sleep(10)
                if not IsMouseButtonPressed(4) then break end
                ReleaseMouseButton(1)
                MoveMouseRelative(0,4)
                Sleep(10)
                PressMouseButton(1)
             until not IsMouseButtonPressed(4)  -- 4 = "Back"
          end
       elseif event == "MOUSE_BUTTON_RELEASED" and arg == 1 then
          ReleaseMouseButton(1)
       elseif event == "PROFILE_DEACTIVATED" then
          ReleaseMouseButton(1)
       end
    end