Logitech lua macro - lua

i'm not so sure how to proceed to this to work, tried some stuff but didn't manage to make it work.
I want it to work like mouse 4 do that one macro and mouse 5 do the other. I'm using that fast sleep cause windows update made logitech macros really slow.
just a little example
do
local function busyloop(final_ctr)
final_ctr = final_ctr - final_ctr%1
local ctr, prev_ms, ms0, ctr0 = 0
while ctr ~= final_ctr do
local ms = GetRunningTime()
if prev_ms and ms ~= prev_ms then
if not ms0 then
ms0, ctr0 = ms, ctr
elseif final_ctr < 0 and ms - ms0 > 500 then
return (ctr - ctr0) / (ms - ms0)
end
end
prev_ms = ms
ctr = ctr + 1
end
end
local coefficient = busyloop(-1)
function FastSleep(ms)
return busyloop(ms * coefficient)
end
end
function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 4)then
repeat
PressMouseButton(1)
MoveMouseRelative(-1,1)
FastSleep(1)
ReleaseMouseButton(1)
until not IsMouseButtonPressed(4)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 5)then
repeat
PressMouseButton(2)
MoveMouseRelative(-2,2)
FastSleep(2)
ReleaseMouseButton(2)
until not IsMouseButtonPressed(5)
end
end
end
didnt manage to find a way around it, tried many ways.

You should write the code with correct indentation to make correct statement nesting
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
repeat
PressMouseButton(1)
MoveMouseRelative(-1,1)
FastSleep(2.5)
ReleaseMouseButton(1)
until not IsMouseButtonPressed(4)
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
repeat
PressMouseButton(1)
MoveMouseRelative(-2,2)
FastSleep(2.5)
ReleaseMouseButton(1)
until not IsMouseButtonPressed(5)
end
end

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

Reverse MoveMouseRelative Lua Codding

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
elseif event == "MOUSE_BUTTON_PRESSED"
and (arg == 5 or arg == 4) then
recoil = recoil ~= arg and arg
elseif event == "MOUSE_BUTTON_PRESSED"
and arg == 1 and recoil == 5 then
MoveMouseRelative(0, -3)
for i = 1, 17 do
MoveMouseRelative(0, 2)
Sleep(15)
if not IsMouseButtonPressed(1) then return end
end
elseif event == "MOUSE_BUTTON_PRESSED"
and arg == 1 and recoil == 4 then
MoveMouseRelative(0, -3)
for i = 1, 35 do
Sleep(15)
MoveMouseRelative(0, 2)
if not IsMouseButtonPressed(1) then return end
end
if not IsMouseButtonPressed(1) then return end
end
end
This is the Lua Script , I wonder how i can get mouse initial position and after it to return to the initial position.
I tried to add MoveMousePosition(x,y)- (32767, 32767 ) at bottom of script but not worked in game . Only on desktop ..
I just want after MoveMouseRelative when i release mouse click to return center or first position .
As Luke100000 said, you need an "undo" movement (the same distance with the opposite sign).
function OnEvent(event, arg)
OutputLogMessage("event = %s, arg = %d\n", event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "MOUSE_BUTTON_PRESSED" and (arg == 5 or arg == 4) then
recoil = recoil ~= arg and arg
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and recoil == 5 then
MoveMouseRelative(0, -3)
local n = 0
for i = 1, 17 do
n = n + 1
MoveMouseRelative(0, 2)
Sleep(15)
if not IsMouseButtonPressed(1) then break end
end
repeat -- wait for LMB release
until not IsMouseButtonPressed(1)
for i = 1, n do
MoveMouseRelative(0, -2)
end
MoveMouseRelative(0, 3)
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and recoil == 4 then
MoveMouseRelative(0, -3)
local n = 0
for i = 1, 35 do
Sleep(15)
n = n + 1
MoveMouseRelative(0, 2)
if not IsMouseButtonPressed(1) then break end
end
repeat -- wait for LMB release
until not IsMouseButtonPressed(1)
for i = 1, n do
MoveMouseRelative(0, -2)
end
MoveMouseRelative(0, 3)
end
end
To undo any operations we need a stack, where you remember everything you did. But since we only have a single position and therefore the order does not matter, we use a simple number to store the total moved x and y instead.
local movedX = 0
local movedY = 0
function move(x, y)
MoveMouseRelative(x, y)
movedX = movedX + x
movedY = movedY + y
end
Now you use e.g. move(0, 2) only.
To undo we do the opposite; since we only have a number by subtracting it.
function undo()
MoveMouseRelative(-movedX, -movedY)
movedX = 0
movedY = 0
end
Unrelated, but in your loop, do not use return but break. That way you can reach the end of the event and add an undo() there.

Logitech LUA script adding sleep timer

Was wondering how to add the sleep timer to my LUA script so it doesn't continually loop as fast as possible and press 0x29, I'd like to make it so when buttons 1 and 3 are pressed on my mouse it hits key 0x29 once every 3-4 seconds instead of as fast as possible.
EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" then
if (arg == 1 or arg == 2) then
mb1 = IsMouseButtonPressed(1);
mb2 = IsMouseButtonPressed(3);
--OutputLogMessage(tostring(mb1) .. " - " .. tostring(mb2));
if (mb1 == true and mb2 == true) then
PressAndReleaseKey(0x29);
end
end
end
end
You can get the current time in milliseconds by GetRunningTime()
local last_time = -math.huge
local is_pressed = {}
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "MOUSE_BUTTON_RELEASED" and (arg == 1 or arg == 2) then
is_pressed[arg] = false
elseif event == "MOUSE_BUTTON_PRESSED" and (arg == 1 or arg == 2) then
is_pressed[arg] = true
local mb1 = is_pressed[1]
local mb2 = is_pressed[2]
--OutputLogMessage(tostring(mb1) .. " - " .. tostring(mb2))
if mb1 and mb2 and GetRunningTime() - last_time > 5000 then
PressAndReleaseKey(0x29)
last_time = GetRunningTime()
end
end
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 Script add Delay

so i have a problem with a LUA Script that im currently building. I have variable for a Gun Spray Pattern, but i want to add a 9ms delay between the Coordinates, unfortunately i cant find a vay to add delays to between the coordinates. Its should look like this
{x=2,y=2}DELAY{x=1,y=1}DELAY......
I know that its impossible to put delays in Variables but i would hope to find a way to put the delays in a loop maybe??.
Heres the code:
local Macro_Activation_Key = 4
local Selection_Key = 3
local Spray_Randomize1 = math.random(24,24)
local Spray_Randomize2 = math.random(20,20.5)
local Spray_Randomize3 = math.random(24,24)
local Recoil_Activator
R_Weapon_Selector = false,0
EnablePrimaryMouseButtonEvents(true);
local AK47_Pattern = {{x=0,y=2},{x=0,y=2},{x=0,y=2},{x=0,y=3},{x=0,y=4},{x=0,y=4},{x=0,y=5},{x=0,y=8},{x=0,y=8},{x=0,y=8}}
local M4A1_Pattern = {{x=0,y=1},{x=0,y=1},{x=0,y=2},{x=0,y=2},{x=0,y=1},{x=0,y=1},{x=0,y=2},{x=0,y=2},{x=0,y=3},{x=0,y=3},{x=0,y=3},{x=0,y=3},{x=0,y=3},{x=0,y=3},{x=0,y=3},{x=-1,y=4},{x=-1,y=4},{x=0,y=5},{x=-1,y=5},{x=-1,y=5},{x=0,y=5},{x=0,y=5},{x=0,y=5}}
local function RetrieveWeaponName(weapon,act) if weapon == 1 then
return"AK47" elseif weapon == 3 then
return"M4A1" end if act then
return"ON_Macro" else return"OFF_Macro" end end
local function OutputLogs(weapon, act)
OutputLogMessage(RetrieveWeaponName(weapon,act).."\n");
OutputDebugMessage(RetrieveWeaponName(weapon,act).."\n");
ClearLCD();
OutputLCDMessage(RetrieveWeaponName(weapon,act));
end function
OnEvent(event, arg) if (event == "MOUSE_BUTTON_PRESSED" and arg == Macro_Activation_Key) then
Recoil_Activator = not Recoil_Activator OutputLogs(nil,Recoil_Activator) end
if Recoil_Activator then
if (event == "MOUSE_BUTTON_PRESSED" and arg == Selection_Key) then
if R_Weapon_Selector >= 3 then R_Weapon_Selector = 0 end R_Weapon_Selector = R_Weapon_Selector + 1 OutputLogs(R_Weapon_Selector,nil) end if (R_Weapon_Selector == 1) and IsMouseButtonPressed(1) then
for i = 1, #AK47_Pattern do if IsMouseButtonPressed(1) then Sleep(Spray_Randomize1) MoveMouseRelative( AK47_Pattern[i].x, AK47_Pattern[i].y ) end end end
if (R_Weapon_Selector == 3) and IsMouseButtonPressed(1) then for i = 1, #M4A1_Pattern do if IsMouseButtonPressed(1) then Sleep(Spray_Randomize3) MoveMouseRelative( M4A1_Pattern[i].x, M4A1_Pattern[i].y )
I want to add Delay to the local AK47_Pattern and M4a1_Pattern Variables.
Appreciate all kinds of help
end end end end end
local Macro_Activation_Key = 4
local Selection_Key = 3
local Recoil_Activator, R_Weapon_Selector = false,0
EnablePrimaryMouseButtonEvents(true)
-- d = delay to wait before moving the mouse (d=9 by default)
local AK47_Pattern = {{x=0,y=2},{d=11,x=0,y=2},{d=12,x=0,y=2},{x=0,y=3},{x=0,y=4},{x=0,y=4},{x=0,y=5},{x=0,y=8},{x=0,y=8},{x=0,y=8}}
local M4A1_Pattern = {{x=0,y=1},{d=5,x=0,y=1},{d=7,x=0,y=2},{x=0,y=2},{x=0,y=1},{x=0,y=1},{x=0,y=2},{x=0,y=2},{x=0,y=3},{x=0,y=3},{x=0,y=3},{x=0,y=3},{x=0,y=3},{x=0,y=3},{x=0,y=3},{x=-1,y=4},{x=-1,y=4},{x=0,y=5},{x=-1,y=5},{x=-1,y=5},{x=0,y=5},{x=0,y=5},{x=0,y=5}}
local function RetrieveWeaponName(weapon,act)
if weapon == 1 then
return"AK47"
elseif weapon == 3 then
return"M4A1"
end
if act then
return"ON_Macro"
else
return"OFF_Macro"
end
end
local function OutputLogs(weapon, act)
OutputLogMessage(RetrieveWeaponName(weapon,act).."\n")
OutputDebugMessage(RetrieveWeaponName(weapon,act).."\n")
ClearLCD()
OutputLCDMessage(RetrieveWeaponName(weapon,act))
end
function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == Macro_Activation_Key) then
Recoil_Activator = not Recoil_Activator
OutputLogs(nil,Recoil_Activator)
end
if Recoil_Activator then
if (event == "MOUSE_BUTTON_PRESSED" and arg == Selection_Key) then
if R_Weapon_Selector >= 3 then
R_Weapon_Selector = 0
end
R_Weapon_Selector = R_Weapon_Selector + 1
OutputLogs(R_Weapon_Selector,nil)
end
if (event == "MOUSE_BUTTON_PRESSED" and arg == 1) then
if R_Weapon_Selector == 1 then
for i = 1, #AK47_Pattern do
Sleep(AK47_Pattern[i].d or 9)
MoveMouseRelative( AK47_Pattern[i].x, AK47_Pattern[i].y )
if not IsMouseButtonPressed(1) then
break
end
end
end
if R_Weapon_Selector == 3 then
for i = 1, #M4A1_Pattern do
Sleep(M4A1_Pattern[i].d or 9)
MoveMouseRelative( M4A1_Pattern[i].x, M4A1_Pattern[i].y )
if not IsMouseButtonPressed(1) then
break
end
end
end
end
end
end

Resources