derkamo
derkamo

Reputation: 1

Logitech Script Help - Left Mouse Toggle

I'm trying to fix my script so when I press Mouse Button 4 it toggles holding down the Left Mouse button and if I press the Left Mouse button during the toggle it will click then continuing holding down the Left Mouse button, unless I toggle it off with Mouse Button 4.

toggle_button = 4 -- What mouse button should control the toggle
button_to_toggle = 1 -- What mouse button to toggle
toggle = false
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
    if (event == "MOUSE_BUTTON_PRESSED" and arg == toggle_button) then
        toggle = not toggle
            OutputLogMessage("Mouse %d toggled %s \n", button_to_toggle, tostring(toggle))
    elseif toggle and (event == "MOUSE_BUTTON_PRESSED" and arg == 1) then
            OutputLogMessage("Mouse 1 pressed \n")
        PressAndReleaseMouseButton(1)
        elseif toggle then
            PressMouseButton(button_to_toggle)
        else
            ReleaseMouseButton(button_to_toggle)
        end
end

For some reason my script kind of works. Left Mouse button during toggle only works sometimes, but most the time will not reengage the Mouse 1 toggle. How do I clean up and fix my script?

Upvotes: 0

Views: 1230

Answers (1)

Egor Skriptunoff
Egor Skriptunoff

Reputation: 23767

Step 1.
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 on LMB.
Go to the big mouse picture in LGS and assign command "Left 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 2.
Go to the big mouse picture in LGS.
Unassign standard command "Left Click" from physical MB#1 (select "Unassign" from the drop-down menu).
You will see a warning about a potentially dangerous operation.
Allow this operation because you have the "spare LMB" if something goes wrong.


Step 3.
Your MB#1 is not working now (until you saved the script), so use MB#8 instead of MB#1 in LGS GUI.
Set the script:

local pressed

function OnEvent(event, arg)
   if event == "PROFILE_ACTIVATED" then
      EnablePrimaryMouseButtonEvents(true)
   elseif event == "PROFILE_DEACTIVATED" then
      ReleaseMouseButton(1)
   elseif event == "MOUSE_BUTTON_PRESSED" and arg == 4 
   or (event == "MOUSE_BUTTON_PRESSED" or event == "MOUSE_BUTTON_RELEASED") and arg == 1 then
      pressed = not pressed
      ;(pressed and PressMouseButton or ReleaseMouseButton)(1)
   end
end

Upvotes: 0

Related Questions