How can I write simple Lua code for Logitech mouse? - lua

I am trying to make a script for logitech mouse that:
When left mouse button is pressed, it will activate case 1
When holding the right mouse button and pressing the left mouse button, it will activate the case 2
However, no matter how I try it will only work on case 1.
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
-- Case 1: Press only Button 1
if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsKeyLockOn("scrolllock") == false) then
Sleep(77)
if (IsMouseButtonPressed(1)) then
MoveMouseRelative(0, 4)
Sleep(76)
end
if (IsMouseButtonPressed(1)) then
MoveMouseRelative(0, 6)
Sleep(62)
end
if (IsMouseButtonPressed(1)) then
MoveMouseRelative(0, 5)
Sleep(84)
end
--Case 2: Press button 1+2
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == 2 and IsKeyLockOn("scrolllock") == false) then
Sleep(77)
if (IsMouseButtonPressed(1)) then
MoveMouseRelative(0, 8)
Sleep(76)
end
if (IsMouseButtonPressed(1)) then
MoveMouseRelative(0, 9)
Sleep(62)
end
if (IsMouseButtonPressed(1)) then
MoveMouseRelative(0, 0)
Sleep(84)
end
end
end
I want to add one more case when I press RMB of this script:
When pressed RMB -> press Lshift button
When release RMB -> press Lshift button again
I added the end of the script as below, it does not work.
elseif (event == "MOUSE_BUTTON_PRESSED" and arg==2 and IsKeyLockOn("scrolllock")==false) then
PressAndReleaseKey("lshift")
elseif (event == "MOUSE_BUTTON_RELEASED" and arg==2 and IsKeyLockOn("scrolllock")==false) then
PressAndReleaseKey("lshift")
if I want to add case 3: Press LAlt + LMB, so where do I put IsModifierPressed("lalt") ? I tried as below but failed
function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and not IsKeyLockOn("scrolllock")) then
if not IsMouseButtonPressed(3) then -- 3 = Right Mouse Button (it's the same button as arg==2)
-- Case 1: Press only LMB
if IsModifierPressed("lalt") then
-- Case 3: Press LAlt+LMB
else
-- Case 2: Press RMB+LMB
end
elseif ((event == "MOUSE_BUTTON_PRESSED" or event == "MOUSE_BUTTON_RELEASED") and arg==2 and not IsKeyLockOn("scrolllock")) then
PressKey("lshift")
Sleep(50)
ReleaseKey("lshift")
end
end

function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and not IsKeyLockOn("scrolllock")) then
if IsModifierPressed("lalt") then
-- Case 3: Press LAlt+LMB
elseif not IsMouseButtonPressed(3) then -- 3 = Right Mouse Button (it's the same button as arg==2)
-- Case 1: Press only LMB
else
-- Case 2: Press RMB+LMB
end
elseif ((event == "MOUSE_BUTTON_PRESSED" or event == "MOUSE_BUTTON_RELEASED") and arg==2 and not IsKeyLockOn("scrolllock")) then
PressKey("lshift")
Sleep(50)
ReleaseKey("lshift")
end
end

You must not have an end before an elseif. The elseif serves as the end of the last if automatically.
I'm surprised this code even compiles and runs at all, as you mentioned, that the first case does work.
Edit: You also do not need to use parentheses around the condition of an if. Since it is already encapsulated by if and then it was not necessary for the langugae design to make those mandatory as in other languages.

Related

LUA scripting no recoil

I have this code and I want the repeat loop to get activated after the Right click is pressed for more than .1 seconds otherwise not to get activated OR the loop to stop entirely once I release the Right click after .1 seconds both will work I suppose. Any ideas?
function OnEvent(event, arg)
if (event == "PROFILE_ACTIVATED") then
EnablePrimaryMouseButtonEvents(true)
end
if IsKeyLockOn("capslock")then
if (event == "MOUSE_BUTTON_PRESSED" and arg == 1) then
if IsMouseButtonPressed(3) then
Sleep(30)
repeat
MoveMouseRelative(-9, 12)
Sleep(5)
MoveMouseRelative(9, -9)
Sleep(5)
until not IsMouseButtonPressed(1)
end
end
end
end
I want the repeat loop to get activated after the Right click is pressed for more than .1 seconds otherwise not to get activated
You can use the following code to accomplish this goal:
local RMB_tm = math.huge
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 2 then
RMB_tm = GetRunningTime()
elseif event == "MOUSE_BUTTON_RELEASED" and arg == 2 then
RMB_tm = math.huge
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1
and GetRunningTime() - RMB_tm > 100 and IsKeyLockOn("capslock")
then
Sleep(30)
repeat
MoveMouseRelative(-9, 12)
Sleep(5)
MoveMouseRelative(9, -9)
Sleep(5)
until not IsMouseButtonPressed(1)
end
end

Issue with Logitech Gaming Software macro scripting

I tried to write a script that's just drag your mouse down when right and left mouse button is clicked. Basically it's like norecoil script for shooting game.
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true);
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 2 and IsKeyLockOn"capslock" then
OutputLogMessage("RMB Pressed")
repeat
if IsMouseButtonPressed(1) then
OutputLogMessage("LMB Pressed")
repeat
Sleep(16)
MoveMouseRelative(0, 1)
Sleep(16)
MoveMouseRelative(0, 1)
Sleep(16)
MoveMouseRelative(0, 2)
Sleep(16)
MoveMouseRelative(1, 1)
Sleep(16)
MoveMouseRelative(0, -1)
Sleep(16)
MoveMouseRelative(0, 1)
Sleep(16)
MoveMouseRelative(-2, 2)
until not IsMouseButtonPressed(1)
OutputLogMessage("LMB Released")
end
until not IsMouseButtonPressed(3)
OutputLogMessage("RMB Released")
end
With this code i'm getting OutputLogMessage in script editor but there is no mouse movement at all. I'm kind confused where i could make a mistake?
Here, use this one. Feel free to customize it based on your needs:
local Activate = 3 -- Wheel button to activate/deactivate local
recoil = false -- Disable/Enable script
EnablePrimaryMouseButtonEvents(true); function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == Activate) then
recoil = not recoil if (recoil == false) then
OutputLogMessage("OFF-NoRecoil\n")
OutputLCDMessage("OFF-NoRecoil") else
OutputLogMessage("ON-NoRecoil\n")
OutputLCDMessage("ON-NoRecoil") end end if recoil then antiRecoil(); end end function antiRecoil() repeat -- if mouse1
pressed then MoveMouseRelative(0,5) Sleep(17) until not
IsMouseButtonPressed(1) end

Logitech Lua combining Rapid Fire with spray

So basically, this is what my script looks like at the moment, it’s a rapid fire macro and reduces the recoil of the guns, however I can never spray with this script for some reason as it’s very slow because I guess it’s reducing the recoil. I was wondering if I could shoot like 4, 5 bullets without any recoil (only auto shoot while holding mouse 3 not while tapping.) and continue with spray like normal spray without any delays while already holding mouse3. So 4 bullets no recoil and rest the regular spray on the same cycle. If that makes any sense. Any help would be greatly appreciated.
EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
if IsKeyLockOn("scrolllock")then
if IsMouseButtonPressed(3) then
repeat
if IsMouseButtonPressed(3) then
repeat
PressMouseButton(1)
Sleep(15)
ReleaseMouseButton(1)
until not IsMouseButtonPressed(3)
end
until not IsMouseButtonPressed(3)
end
end
end
local rapid_fire_delay = 15 -- delay between simulation of LMB press/release
local LMB_Pressed
do -- initializing PRNG
local dt = 0
for c in GetDate():gmatch"." do
dt = (dt % 65537 * 23456 + c:byte())
end
math.randomseed(dt)
end
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 2 and IsKeyLockOn("scrolllock") then -- RMB press
for j = 1, math.random(4, 5) do -- first 4-5 bullets as rapid-fire
PressMouseButton(1)
Sleep(math.random(rapid_fire_delay, 2*rapid_fire_delay))
ReleaseMouseButton(1)
Sleep(math.random(rapid_fire_delay, 2*rapid_fire_delay))
if not IsMouseButtonPressed(3) then return end -- is RMB pressed?
end
PressMouseButton(1)
LMB_Pressed = true
elseif event == "MOUSE_BUTTON_RELEASED" and arg == 2 and LMB_Pressed then -- RMB release
ReleaseMouseButton(1)
LMB_Pressed = false
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
repeat
Sleep(15)
PressKey("SPACEBAR")
Sleep(15)
ReleaseKey("SPACEBAR")
until not IsMouseButtonPressed(5)
end
end
The line for j = 1, math.random(4, 5) do means "a random quantity from 4 to 5 bullets".
If you want exactly 3 bullets change this line to for j = 1, 3 do
EDIT:
This is instruction on how to make the rapidfire turn on only after LMB double-click.
Usual slow LMB click will not trigger rapidfire.
local Prev_LMB_Time, LMB_Pressed = 0
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
if IsKeyLockOn("scrolllock") then
local tm = GetRunningTime()
tm, Prev_LMB_Time = tm - Prev_LMB_Time, tm
if tm < 200 then -- LMB double-click
for j = 1, 100 do
PressMouseButton(1)
Sleep(1)
ReleaseMouseButton(1)
Sleep(1)
if not IsMouseButtonPressed(4) then return end
end
end
end
PressMouseButton(1)
LMB_Pressed = true
elseif event == "MOUSE_BUTTON_RELEASED" and arg == 1 and LMB_Pressed then
ReleaseMouseButton(1)
LMB_Pressed = false
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
repeat
Sleep(15)
PressKey("SPACEBAR")
Sleep(15)
ReleaseKey("SPACEBAR")
until not IsMouseButtonPressed(5)
end
end
Currently you have in GHUB:
Primary Click = G1
Back = G4 G8
What you should do in GHUB (in this order):
Bind "Primary Click" to G8 (from now on, use button#8 instead of LMB)
Bind "Back" to G1
Set the script
Now you should have the following:
Primary Click = G8
Back = G1 G4
Mouse button#8 is now "spare LMB" just in case LMB works incorrectly.

Lua Logitech How to have script NOT repeat when button is pressed?

I'm trying to have the below code execute the MoveMouseRelative function only once when mouse button 1 is pressed or held. I've tried removing the "repeat" line but it breaks the code. Currently when activated and mouse 1 is held the cursor is dragged down constantly.
function OnEvent(event, arg)
OutputLogMessage("event = %s, arg = %d\n", event, arg)
if (event == "PROFILE_ACTIVATED") then
EnablePrimaryMouseButtonEvents(true)
elseif event == "PROFILE_DEACTIVATED" then
ReleaseMouseButton(2) -- to prevent it from being stuck on
end
if (event == "MOUSE_BUTTON_PRESSED" and arg == 5) then
recoilx2 = not recoilx2
spot = not spot
end
if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and recoilx2) then
if recoilx2 then
repeat
--Sleep(35)
Sleep(5)
MoveMouseRelative(0, 3)
until not IsMouseButtonPressed(1)
end
end
Goal: Lua script for Logitech mouse that performs the following tasks:
When mouse button 5 has been "activated":
- Left clicks once every 1000 milliseconds (time in between shots),
- and also pulls the mouse down once every 1000 milliseconds.
So if I hold left mouse button it continuously shoots but only pulls down when it does shoot
Select a keyboard button you never use in the game and set it as the only way to fire the pistol, this key will be used to fire programmatically.
I assume the key is P, but you can choose any other button: "f12", "backspace", "num9", ...
The game must do nothing on left mouse button press.
local fire_button = "P"
function OnEvent(event, arg)
OutputLogMessage("event = %s, arg = %d\n", event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "PROFILE_DEACTIVATED" then
-- to prevent mouse buttons from being stuck on
for j = 1, 5 do ReleaseMouseButton(j) end
end
if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
recoilx2 = not recoilx2
end
if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
PressKey(fire_button)
Sleep(50)
ReleaseKey(fire_button)
if recoilx2 then
while IsMouseButtonPressed(1) do
MoveMouseRelative(0, 25)
local next_shot_time = GetRunningTime() + 1000
local LMB
repeat
Sleep(50)
LMB = IsMouseButtonPressed(1)
until not LMB or GetRunningTime() >= next_shot_time
if LMB then
PressKey(fire_button)
Sleep(50)
ReleaseKey(fire_button)
end
end
end
end
end

Combining two Lua scripts?

I'm trying to find a way to combine these two scripts:
function OnEvent(event, arg)
OutputLogMessage("event = %s, arg = %d\n", event, arg)
if (event == "PROFILE_ACTIVATED") then
EnablePrimaryMouseButtonEvents(true)
elseif event == "PROFILE_DEACTIVATED" then
ReleaseMouseButton(2) -- to prevent it from being stuck on
end
if (event == "MOUSE_BUTTON_PRESSED" and arg == 5) then
recoilx2 = not recoilx2
spot = not spot
end
if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and recoilx2) then
if recoilx2 then
repeat
MoveMouseRelative(0, 25)
Sleep(1000)
until not IsMouseButtonPressed(1)
end
end
if IsMouseButtonPressed(1) then
repeat
PressMouseButton(1)
Sleep(15)
ReleaseMouseButton(1)
until not IsMouseButtonPressed(1)
end
end
When I press mouse 5 to activate the first section, the repeating click section at the bottom doesn't work.
When I press mouse 5 to activate the first section, the repeating
click section at the bottom doesn't work.
When you press button 5 the first time you end up in this conditional statement
if (event == "MOUSE_BUTTON_PRESSED" and arg == 5) then
recoilx2 = not recoilx2
spot = not spot
end
This will assign true to recoilx2.
When you press button 1 after that you'll end up in this conditional statement:
if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and recoilx2) then
if recoilx2 then
repeat
MoveMouseRelative(0, 25)
Sleep(1000)
until not IsMouseButtonPressed(1)
end
end
Here you wait until button 1 is not pressed anymore.
Unless you press button 5 again to assign false to recoilx2 you'll always end up there when you press button 1 and stay there until you release button 1.
Hence you can never enter this conditional statement.
if IsMouseButtonPressed(1) then
repeat
PressMouseButton(1)
Sleep(15)
ReleaseMouseButton(1)
until not IsMouseButtonPressed(1)
end

Resources