Skip to content

Instantly share code, notes, and snippets.

@MartinHarding
Last active October 28, 2023 10:35
Show Gist options
  • Save MartinHarding/3155be5e286e3f483223a1e21024c554 to your computer and use it in GitHub Desktop.
Save MartinHarding/3155be5e286e3f483223a1e21024c554 to your computer and use it in GitHub Desktop.
Logitech Gaming Software Mouse Button Toggle script
-- Toggles a mouse button when you press another mouse button.
-- 1. Change your mouse to "Automatic Game Detection" mode
-- 2. Right click on a profile's icon and choose scripting
-- 3. Paste this into the default profile script box, save, enjoy!
toggle_button = 7 -- What mouse button should control the toggle
button_to_toggle = 1 -- What button to toggle
button_toggled = false
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == toggle_button then
button_toggled = not button_toggled
OutputLogMessage("Mouse %d toggled %s", button_to_toggle, tostring(button_toggled))
if button_toggled then
PressMouseButton(button_to_toggle)
else
ReleaseMouseButton(button_to_toggle)
end
end
end
@johnwang16
Copy link

Thanks for this! works great!

@Kanjejou
Copy link

Kanjejou commented Jul 24, 2020

Hi martin I was trying to do some stuff with my G502, actualy make it press another button when i press left mouse button here is what I tried to make

EnablePrimaryMouseButtonEvents (true)
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
p = not button_toggled
OutputLogMessage("Mouse %d toggled %s", p, tostring(button_toggled))
if button_toggled then
PressMouseButton(p)
else
ReleaseMouseButton(p)
end
end
end

but i get this error

[string "LuaVM"]:4:bad argument #2 to 'OutputLogMessage' (number expected, got boolean)

can you please tell me what i did wrong?

@johnwang16
Copy link

It's because you're replacing both button_to_toggle and button_toggled with one variable (p). That completely breaks the logic of this script. What is it you want to do with the left mouse? Press it once and have it hold down another button until you press it again, or press it and have it be remapped to another button?

@Kanjejou
Copy link

What i was trying to achieve was to push left mouse button and its counted as having both left mouse button and press "p" once from my keyboard, and when i stop pushing left click it push "p"again

@johnwang16
Copy link

johnwang16 commented Jul 25, 2020

function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
PressMouseButton(1)
PressAndReleaseKey("p")
end
if event == "MOUSE_BUTTON_RELEASED" and arg == 1 then
ReleaseMouseButton(1)
PressAndReleaseKey("p")
end
end

@Kanjejou
Copy link

Thank you it worked perfectly

@superarts
Copy link

Hi, I would like to simulate key toggle instead of button toggle, but PressKey("lshift") doesn't work - it actually does PressAndRelease, which is weird. I'm on macOS with G502. Any idea please?

@johnwang16
Copy link

toggle_button = 10
button_toggled = false
function OnEvent(event, arg)
	if event == "MOUSE_BUTTON_PRESSED" and arg == toggle_button then
		button_toggled = not button_toggled
		if button_toggled then
			PressKey("lshift")
		else
			ReleaseKey("lshift")
		end
	end
end

@superarts
Copy link

Thanks @johnwang16; unfortunately it doesn't work on macOS, and I have granted accessibility permission to GHub. Could you confirm if you are using Windows or macOS please?

@johnwang16
Copy link

Ah.. I'm on windows and it works here. I don't have any mac machines so I'm unable to test macos

@superarts
Copy link

Ah.. I'm on windows and it works here. I don't have any mac machines so I'm unable to test macos

Thank you! Giving how simple the API is, I highly suspect they are calling the wrong API on macOS, which results PressKey() and PressAndReleaseKey() does the same thing. I'm not an expert of macOS programming, but based on https://github.com/BlueM/cliclick, differentiating key up and key down events shouldn't be that hard.

Hope someone could prove me wrong and I'm just making some mistakes here. This problem is really irritating.

@thrashette
Copy link

I just wanted to thank you so much for this. I have wrist problems and struggle playing certain builds in Diablo III. This saved me so much pain.

@superarts
Copy link

As an update, macOS app works with a recent update. I'm using this for the same reason as @thrashette as there's no way to keep tapping 1 and 2 for a good 15 minutes while positioning myself (I'm playing strife build this season)

Happy grinding!

@sheepisready
Copy link

Hi guys, was looking for a solution to a similar problem, have no idea how to start a new topic so hope this necro is ok.
what I'm trying to get my mouse to do is:

when pressing mouse button 5:
-press crtl button
-release ctrl button
-press mouse1 button
-press f button
-release f button

when releasing button 5:
-release mouse1 button
-press crtl button
-release crtl button

not sure if this is possible in logitech script?

@lonesbeats
Copy link

is there a way to create a toggle macro on one button that can cycle through 3 different commands on one button.
for example: have one button set to shift+e, the first time you hit it.
press the same button again and go to the next command Shift+p,
press the same button a thrid time and go to the 3rd command alt+c,
press the button again and start the whole loop all over again?
if so could you write the code, I'm a complete noob to Lula.
I would even pay to have this done.
email me: lonesbeats@gmail.com

@Parzehlli
Copy link

Ah.. I'm on windows and it works here. I don't have any mac machines so I'm unable to test macros
Hey @johnwang16 you available? Or anybody that can do this I'm working through making a macro in the Logitech Software script section like some of these other guys and I'm not sure what my side mouse buttons on my Logitech g502 are numbered or named as in the Lua language. Im trying to make a script that holds the left click or right click of the mouse down for me while pressing other keys, keys i want to press while left or right click is held down for me is both side mouse buttons and the top ones by the left click, and the C button on my keyboard. With an easy on off tap off a single key! Would appreciate any help..

@Parzehlli
Copy link

I may figure this out on my own a few days from now cause im a noob, but man would help be appreciated greatly.

@johnwang16
Copy link

to find what buttons map to what number, use this in the script window and start pressing stuff

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

@Parzehlli
Copy link

to find what buttons map to what number, use this in the script window and start pressing stuff

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

Actually yeah i figured that out BUT! what if i don't even need the buttons mapped in the script cause the goal is only to have my left or right click held in. How bout a Toggle ON-OFF button for the left or right click mouse buttons held on. For example, i can just press J and the script is on and holding the left click in for me. Than when i want it off just press J again, Is that possible?

@johnwang16
Copy link

No, not with logitech scripting. https://douile.github.io/logitech-toggle-keys/APIDocs.pdf
AutoHotkey is what you want to use

@Parzehlli
Copy link

I figured it OUT! AutoHotKey can get you banned tho in some games? Here- for any future people who come across this your welcome! This is working in the LGS Software with a Logitech G502 or G502 Hero. Hmu for more details on what it does, thanks @johnwang16
"function OnEvent(event, arg)
if IsKeyLockOn ("capslock") then
if event == "MOUSE_BUTTON_PRESSED" and arg == 8 then
PressKey("B")
end
if event == "MOUSE_BUTTON_RELEASED" and arg == 2 then
ReleaseKey("B")
end
elseif IsMouseButtonPressed(3)then
repeat
if IsMouseButtonPressed(1) then
repeat
MoveMouseRelative(0,1)
Sleep(8)
until not IsMouseButtonPressed(1)
end
until not IsMouseButtonPressed(3)
end
end"

@evanricard
Copy link

Is there any way to apply this profile to a Logitech Ergo MX?

Copy link

ghost commented Jun 7, 2021

https://github.com/RavenBHK/DBD-Left-Hold-and-Auto-Run

I used to above macro and am trying to combine with this one. Can anyone help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment