[Logitec][Lua] I am wondering how to forcefully escape the loop - lua

I am curious about how to escape with a specific key in the middle of the loop
instead of escaping after the loop is over
What i want to do is that
Start Macro i = 1 i = 2 i = 3 i = 4 Aborted (when i press arg 7)
.
Full Script:
local isMacroRunning = false
co = coroutine.create(function()
while true do
if not isMacroRunning then break end
MoveMouseRelative (5, 0)
Sleep(150)
MoveMouseRelative (-5, 0)
Sleep(150)
OutputLogMessage("Break\n")
coroutine.yield()
end
end)
function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 8) then
isMacroRunning = true
RunMacro()
elseif(event == "MOUSE_BUTTON_PRESSED" and arg == 7 and isMacroRunning) then
isMacroRunning = false
RunMacro()
end
end
function RunMacro()
if isMacroRunning then
coroutine.resume(co)
OutputLogMessage("Start Macro\n")
for i = 1, 10 do
OutputLogMessage("i = %d\n",i)
Sleep(200)
end
else
OutputLogMessage("Aborted\n")
end
end
coroutine.resume(co)

Why not check if the button is pressed inside the loop?
if IsMouseButtonPressed(7) then break end

Related

Lua script for g502

i'm trying to make a script for my mouse for a game . Basically this is what it has to do:
when num lock is OFF he must press the q key every time right mouse and left mouse clicked are simultaneously
When num lock is ON he must do the passage 1 but with the addition that when I hold down the right mouse button (ads) press the f key and press the f key again when the right mouse button is
released
Here's the code i've been trying to write but can't figure out the 2 part of my request:
Thanks for the help :)
local zoomed = false
local weapons = true
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 2 and weapons) then
zoomed = true
end
if (event == "MOUSE_BUTTON_RELEASED" and arg == 2 and weapons) then
zoomed = false
end
if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and zoomed) then
PressKey("q")
ReleaseKey("q")
end
if IsKeyLockOn("numlock")then
if (event == "MOUSE_BUTTON_PRESSED" and arg == 2) then
PressKey("f")
Sleep(5)
ReleaseKey("f")
end
end
end
Set flag press_F_on_RMB_release to know that F should be pressed on the next right mouse button release event.
local RMB_pressed = false
local press_F_on_RMB_release = false
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 2 then
RMB_pressed = true
elseif event == "MOUSE_BUTTON_RELEASED" and arg == 2 then
RMB_pressed = false
if press_F_on_RMB_release then
PressKey("f")
Sleep(30)
ReleaseKey("f")
press_F_on_RMB_release = false
end
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and RMB_pressed then
PressKey("q")
Sleep(30)
ReleaseKey("q")
if IsKeyLockOn("numlock") then
PressKey("f")
Sleep(30)
ReleaseKey("f")
press_F_on_RMB_release = true
end
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 gaming software lua script repeat toggle

I have a script which does a left click, then moves to the right about 1 cm, then clicks again and moves back to the left.
I would like this script to repeat itself continuously until i press a button, it doesn't really matter much which button it uses(except for MB 1, 2 and 3.
I have been trying for a while, with repeats and loops and the only thing i have achieved is a very complex script that makes the software crash after each run, which is slightly annoying.
I think there is something about the repeat function that i do not understand correctly.
Can anyone show me how to get this to work?
greetings
Edit: I have updated the code to what it is now, the original code is below it.
local mb4_status, exit_flag
local function Move(dx, dy, time, is_interruptable)
local t0 = GetRunningTime()
local prev_dx, prev_dy = 0, 0
repeat
Sleep(15)
local part = math.min(time, GetRunningTime() - t0) / time
local current_dx = math.floor(part * dx)
local current_dy = math.floor(part * dy)
local x, y = current_dx - prev_dx, current_dy - prev_dy
if x ~= 0 or y ~= 0 then
MoveMouseRelative(x, y)
end
prev_dx, prev_dy = current_dx, current_dy
local prev_mb4_status = mb4_status
mb4_status = IsMouseButtonPressed(4)
exit_flag = exit_flag or mb4_status and not prev_mb4_status
until part == 1 or is_interruptable and exit_flag
end
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
if exit_flag then
exit_flag = false
else
mb4_status = true
local x = 44
repeat
PressMouseButton(1)
Move(0, 0, 200, false) -- equivalent to Sleep(200)
ReleaseMouseButton(1)
Move(x, 0, 1000, true) -- mixture of MoveMouseRelative(44,0) + Sleep(1000)
x = -x
until exit_flag
end
end
end
function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 8) then
for i = 1, 1 do
PressMouseButton(1)
Sleep(200)
ReleaseMouseButton(1)
end
Sleep(500)
for i = 5, 15 do
MoveMouseRelative(4,0)
Sleep(1)
end
Sleep(500)
for i = 1, 1 do
PressMouseButton(1)
Sleep(200)
ReleaseMouseButton(1)
end
Sleep(500)
for i = 5, 15 do
MoveMouseRelative(-4,0)
Sleep(1)
end
Sleep(500)
end
end
I haven't tested this code but it should at least give you some idea how to approach this. Instead of long blocking sleeps my code checks how much time has passed since start and checks the abort condtion of any button being pressed while waiting.
function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 8) then
local function abortCondition()
return IsMouseButtonPressed(1) or IsMouseButtonPressed(2) or IsMouseButtonPressed(3)
end
local function abortableSleep(delay)
local startTime = GetRunningTime()
while GetRunningTime() <= startTime + delay do
if abortCondition() then return end
Sleep(5)
end
return true
end
local function delayedClick(button, delay)
PressMouseButton(button)
Sleep(10)
if not abortableSleep(delay-10) then return end
ReleaseMouseButton(button)
return true
end
repeat
if not delayedClick(1, 200) then return end
if not abortableSleep(500) then return end
for i = 0, 10 do
MoveMouseRelative(4,0)
Sleep(1)
end
if not abortableSleep(500) then return end
if not delayedClick(1, 200) then return end
for i = 0, 10 do
MoveMouseRelative(-4,0)
Sleep(1)
end
if not abortableSleep(500) then return end
until releaseCondition()
end
end
Please note Btn#4 is used instead of Btn#8
local mb4_status, exit_flag
local function Move(dx, dy, time, is_interruptable)
local t0 = GetRunningTime()
local prev_dx, prev_dy = 0, 0
repeat
Sleep(15)
local part = math.min(time, GetRunningTime() - t0) / time
local current_dx = math.floor(part * dx)
local current_dy = math.floor(part * dy)
local x, y = current_dx - prev_dx, current_dy - prev_dy
if x ~= 0 or y ~= 0 then
MoveMouseRelative(x, y)
end
prev_dx, prev_dy = current_dx, current_dy
local prev_mb4_status = mb4_status
mb4_status = IsMouseButtonPressed(4)
exit_flag = exit_flag or mb4_status and not prev_mb4_status
until part == 1 or is_interruptable and exit_flag
end
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
if exit_flag then
exit_flag = false
else
mb4_status = true
local x = 44
repeat
PressMouseButton(1)
Move(0, 0, 200, false) -- equivalent to Sleep(200)
ReleaseMouseButton(1)
Move(0, 0, 300, true) -- equivalent to Sleep(300)
Move(x, 0, 1000, true) -- mixture of MoveMouseRelative(44,0) + Sleep(1000)
Move(0, 0, 300, true)
x = -x
until exit_flag
end
end
end
UPDATE:
I've inserted sleep 300ms between move and click.
To change distance modify 44
Timings 200, 1000, 300 can also be modified
To change "start" button modify arg == 4
To change "stop" button modify IsMouseButtonPressed(4) (only 2-5)
Please note that script deliberately ignores every second press on "start" button because it assumes that "start" button is the same as "stop" button

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

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