Sideway Movement When Moving Cursor Horizontally Only - lua

Trying to make a LUA script that gets a mouse position and then inputs key movement accordingly
If the mouse is moving towards the right, I want the character to move left and vice versa.
Found bunch of codes here online and tried stitching them but no consistent result
EnablePrimaryMouseButtonEvents(true);
local distance_horiz = 2 -- x, pixels
local distance_vert = 2 -- x, pixels
local key_press_delay = 0 -- y, ms
local screen_width = 2560 -- set your game screen resolution here
local screen_height = 1440
--------------------------------------------------------------------
local active
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
repeat
if IsMouseButtonPressed(1) then
local horiz, vert, horiz_key, vert_key, horiz_key_tm, vert_key_tm = 0, 0
local prev_MMB, prev_x, prev_y = true
repeat
Sleep(10)
local MMB = IsMouseButtonPressed(2)
local exit = MMB and not prev_MMB
prev_MMB = MMB
local x, y = GetMousePosition()
x = math.floor((x + (0.5 + 2^-16)) * (screen_width-1) / (2^16-1))
y = math.floor((y + (0.5 + 2^-16)) * (screen_height-1) / (2^16-1))
x, y, prev_x, prev_y = x - (prev_x or x), y - (prev_y or y), x, y
horiz, vert = horiz + x, vert + y
local tm = GetRunningTime()
-- horiz
if tm >= (horiz_key_tm or 0) then
if math.abs(horiz) >= distance_horiz then
local new_key
if horiz > 0 then
horiz, new_key = horiz - distance_horiz, "D"
else
horiz, new_key = horiz + distance_horiz, "A"
end
if new_key ~= horiz_key then
if horiz_key then
ReleaseKey(horiz_key)
end
PressKey(new_key)
end
horiz_key, horiz_key_tm = new_key, (horiz_key_tm or tm) + key_press_delay
elseif horiz_key then
ReleaseKey(horiz_key)
horiz_key, horiz_key_tm = nil
end
end
until not IsMouseButtonPressed(1)
end
until not IsMouseButtonPressed(3)
end
end

To turn the script on press Middle Mouse Button.
To turn the script off press MMB again.
While the script is on, horizontal mouse movement is echoed by "A"/"D" keys
local distance_horiz = 20 -- each 20 pixels makes one key-press A/D
local screen_width = 2560 -- set your game screen resolution here
local active
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 3 then -- MMB
active = not active
if active then
local horiz, prev_x, exit, key = 0
repeat
Sleep(10)
local MMB = IsMouseButtonPressed(2)
exit, active = MMB and not active, MMB
local x = GetMousePosition()
x = math.floor((x + (0.5 + 2^-16)) * (screen_width-1) / (2^16-1))
horiz, prev_x = horiz + x - (prev_x or x), x
if math.abs(horiz) >= distance_horiz then
if horiz > 0 then
horiz, key = horiz - distance_horiz, "A"
else
horiz, key = horiz + distance_horiz, "D"
end
PressAndReleaseKey(key)
end
until exit
end
end
end

Related

Lua Script for Controlling WASD Keys via Mouse Movement

I need a lua script that allows me to press keys ("W", "A", "S", "D") through moving my mouse in certain direction.
For example:
I am moving mouse up by x pixels ---> Key "W" is being used for for y ms.
And so on analogically for the other mouse movements.
Try this
-- turn script on - click middle mouse button while NumLock LED is ON
-- turn script off - click middle mouse button
--------------------------------------------------------------------
-- PARAMETERS
--------------------------------------------------------------------
local distance_horiz = 20 -- x, pixels
local distance_vert = 25 -- x, pixels
local key_press_delay = 30 -- y, ms
local screen_width = 1920 -- set your game screen resolution here
local screen_height = 1080
--------------------------------------------------------------------
local active
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 3 then -- Middle mouse button
active = not active and IsKeyLockOn"NumLock"
if active then
local horiz, vert, horiz_key, vert_key, horiz_key_tm, vert_key_tm = 0, 0
local prev_MMB = true
repeat
local prev_x, prev_y = math.floor(screen_width/2), math.floor(screen_height/2)
local x = math.floor(prev_x * (2^16-1) / (screen_width-1) + 0.5)
local y = math.floor(prev_y * (2^16-1) / (screen_height-1) + 0.5)
MoveMouseTo(x, y)
Sleep(10)
local MMB = IsMouseButtonPressed(2)
local exit = MMB and not prev_MMB
prev_MMB = MMB
x, y = GetMousePosition()
x = math.floor((x + (0.5 + 2^-16)) * (screen_width-1) / (2^16-1))
y = math.floor((y + (0.5 + 2^-16)) * (screen_height-1) / (2^16-1))
horiz, vert = horiz + x - prev_x, vert + y - prev_y
local tm = GetRunningTime()
-- horiz
if tm >= (horiz_key_tm or 0) then
if math.abs(horiz) >= distance_horiz then
local new_key
if horiz > 0 then
horiz, new_key = horiz - distance_horiz, "D"
else
horiz, new_key = horiz + distance_horiz, "A"
end
if new_key ~= horiz_key then
if horiz_key then
ReleaseKey(horiz_key)
end
PressKey(new_key)
end
horiz_key, horiz_key_tm = new_key, (horiz_key_tm or tm) + key_press_delay
elseif horiz_key then
ReleaseKey(horiz_key)
horiz_key, horiz_key_tm = nil
end
end
-- vert
if tm >= (vert_key_tm or 0) then
if math.abs(vert) >= distance_vert then
local new_key
if vert > 0 then
vert, new_key = vert - distance_vert, "S"
else
vert, new_key = vert + distance_vert, "W"
end
if new_key ~= vert_key then
if vert_key then
ReleaseKey(vert_key)
end
PressKey(new_key)
end
vert_key, vert_key_tm = new_key, (vert_key_tm or tm) + key_press_delay
elseif vert_key then
ReleaseKey(vert_key)
vert_key, vert_key_tm = nil
end
end
until exit
if vert_key then
ReleaseKey(vert_key)
end
if horiz_key then
ReleaseKey(horiz_key)
end
end
end
end

Love2D trying to generate a chunk that has blocks inside it, but after moving a few chunks away it renders more chunks then intended

I have created a basic chunk generator, a chunk is area filled with squares, when the player moves a few blocks away from 0,0 it works correctly but after moving 4 chunks away it renders more then one chunk instead of one, I am not sure what I am doing wrong, I have given it a go changing some values, but I am left head scratching.
here is the full code, you can copy and paste into VSCODE with love2D to see what happens.
I think the main issue is somewhere around check_boarders function since that is what checks if the player is inside a chunk.
function Key_input(key)
if love.keyboard.isDown(key) then
return 1
else
return 0
end
end
function love.load()
Camera = require "camera"
Cam = Camera()
Basic_Player = {}
Basic_Player.X = 100
Basic_Player.Y = 100
Basic_Player.Speed = 15
Movement = {}
end
function love.update(dt)
Movement.X = Key_input("d") - Key_input("a")
Movement.Y = Key_input("s") - Key_input("w")
Basic_Player.X = Basic_Player.X + Movement.X * Basic_Player.Speed
Basic_Player.Y = Basic_Player.Y + Movement.Y * Basic_Player.Speed
Cam:lookAt(Basic_Player.X,Basic_Player.Y)
X, Y = Cam:position() -- Set cam position to global values
end
function love.draw()
love.graphics.setBackgroundColor(0.5,0.5,0.9)
Cam:attach() -- Renders the player and world inside its own scene
generate_world(10,0)
love.graphics.setColor( 0,0,1, 1 )
love.graphics.rectangle("fill",Basic_Player.X,Basic_Player.Y,30,30)
love.graphics.setColor( 1,1,1, 1 )
Cam:detach()
love.graphics.setColor( 1,0,0, 1 ) --Stays on the screen
love.graphics.print(X .. " / " .. Y ,300,400)
love.graphics.print(love.timer.getFPS( ) ,300,450)
love.graphics.setColor( 1,1,1, 1 )
end
function old_generate_world(_world_size, _seed) -- Before optimization
local _chunk_size = 30
local _block_size = 30
for i = 0, _world_size - 1 do
for f = 0, _world_size - 1 do
local x_val = (_chunk_size * _block_size) * i -- Position value for actually building the chunks
local y_val = (_chunk_size * _block_size) * f
gen_chunk(_chunk_size,_block_size,_seed,{X = x_val ,Y = y_val })
end
end
end
function generate_world(_world_size, _seed)
local _chunk_size = 10 -- Chunk size width and height
local _block_size = 30 -- block size inside the chunk
for i = 0, _world_size - 1 do -- loop through world size
for f = 0, _world_size - 1 do
local x_val = (_chunk_size * _block_size) * i -- Position value for actually building the chunks
local y_val = (_chunk_size * _block_size) * f
local chunk_x_local_size = 0 -- To make sure we get a length for when i and f = 0
local chunk_y_local_size = 0
if i == 0 then -- To make sure the size of the chunk isnt zero
chunk_x_local_size = _chunk_size * _block_size -- Get length of chunk when i = 0
else
chunk_x_local_size = x_val
end
if f == 0 then -- ditto
chunk_y_local_size = _chunk_size * _block_size
else
chunk_y_local_size = y_val
end
-- Checks if the player is inside a chunk if true draw it.
if Check_boarders({X = X,Y = Y},{X = x_val,Y = y_val}, {X = chunk_x_local_size, Y = chunk_y_local_size}) then
gen_chunk(_chunk_size,_block_size,_seed,{X = x_val ,Y = y_val }) -- Actually generate the chunk
end
love.graphics.setColor( 0,1,0, 1 )
love.graphics.rectangle("fill",x_val,y_val,15,15)
love.graphics.setColor( 1,1,1, 1 )
end
end
end
function Check_boarders(player_pos, boarder_pos, chunk_length) -- Checks player position is inside the boarder of the currently generated chunk
if player_pos.X > boarder_pos.X and player_pos.X < boarder_pos.X + chunk_length.X then -- Check if the player is greater then top left and less then top right
if player_pos.Y > boarder_pos.Y and player_pos.Y < boarder_pos.Y + chunk_length.Y then -- check if player is greater then top and less then bottom left
return true
end
end
return false
end
function gen_chunk(chunk_size,block_size,seed,position) -- chunk size is how many blocks inside the chunk, block size is self explain, seed n/a, pos starting chunk position
for i = 0, chunk_size - 1 do
for e = 0, chunk_size - 1 do -- loop until chunk size is met this is the amount of blocks being created
love.graphics.rectangle("fill",position.X + i * block_size,position.Y + e * block_size,block_size - 1,block_size - 1)
end
end
love.graphics.setColor( 1,0,0, 1 )
love.graphics.rectangle("fill",position.X ,position.Y,6,6)
love.graphics.setColor( 1,1,1, 1 )
end
You will need this camera.lua script just create it and paste this into it:
local _PATH = (...):match('^(.*[%./])[^%.%/]+$') or ''
local cos, sin = math.cos, math.sin
local camera = {}
camera.__index = camera
-- Movement interpolators (for camera locking/windowing)
camera.smooth = {}
function camera.smooth.none()
return function(dx,dy) return dx,dy end
end
function camera.smooth.linear(speed)
assert(type(speed) == "number", "Invalid parameter: speed = "..tostring(speed))
return function(dx,dy, s)
-- normalize direction
local d = math.sqrt(dx*dx+dy*dy)
local dts = math.min((s or speed) * love.timer.getDelta(), d) -- prevent overshooting the goal
if d > 0 then
dx,dy = dx/d, dy/d
end
return dx*dts, dy*dts
end
end
function camera.smooth.damped(stiffness)
assert(type(stiffness) == "number", "Invalid parameter: stiffness = "..tostring(stiffness))
return function(dx,dy, s)
local dts = love.timer.getDelta() * (s or stiffness)
return dx*dts, dy*dts
end
end
local function new(x,y, zoom, rot, smoother)
x,y = x or love.graphics.getWidth()/2, y or love.graphics.getHeight()/2
zoom = zoom or 1
rot = rot or 0
smoother = smoother or camera.smooth.none() -- for locking, see below
return setmetatable({x = x, y = y, scale = zoom, rot = rot, smoother = smoother}, camera)
end
function camera:lookAt(x,y)
self.x, self.y = x, y
return self
end
function camera:move(dx,dy)
self.x, self.y = self.x + dx, self.y + dy
return self
end
function camera:position()
return self.x, self.y
end
function camera:rotate(phi)
self.rot = self.rot + phi
return self
end
function camera:rotateTo(phi)
self.rot = phi
return self
end
function camera:zoom(mul)
self.scale = self.scale * mul
return self
end
function camera:zoomTo(zoom)
self.scale = zoom
return self
end
function camera:attach(x,y,w,h, noclip)
x,y = x or 0, y or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
self._sx,self._sy,self._sw,self._sh = love.graphics.getScissor()
if not noclip then
love.graphics.setScissor(x,y,w,h)
end
local cx,cy = x+w/2, y+h/2
love.graphics.push()
love.graphics.translate(cx, cy)
love.graphics.scale(self.scale)
love.graphics.rotate(self.rot)
love.graphics.translate(-self.x, -self.y)
end
function camera:detach()
love.graphics.pop()
love.graphics.setScissor(self._sx,self._sy,self._sw,self._sh)
end
function camera:draw(...)
local x,y,w,h,noclip,func
local nargs = select("#", ...)
if nargs == 1 then
func = ...
elseif nargs == 5 then
x,y,w,h,func = ...
elseif nargs == 6 then
x,y,w,h,noclip,func = ...
else
error("Invalid arguments to camera:draw()")
end
self:attach(x,y,w,h,noclip)
func()
self:detach()
end
-- world coordinates to camera coordinates
function camera:cameraCoords(x,y, ox,oy,w,h)
ox, oy = ox or 0, oy or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
-- x,y = ((x,y) - (self.x, self.y)):rotated(self.rot) * self.scale + center
local c,s = cos(self.rot), sin(self.rot)
x,y = x - self.x, y - self.y
x,y = c*x - s*y, s*x + c*y
return x*self.scale + w/2 + ox, y*self.scale + h/2 + oy
end
-- camera coordinates to world coordinates
function camera:worldCoords(x,y, ox,oy,w,h)
ox, oy = ox or 0, oy or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
-- x,y = (((x,y) - center) / self.scale):rotated(-self.rot) + (self.x,self.y)
local c,s = cos(-self.rot), sin(-self.rot)
x,y = (x - w/2 - ox) / self.scale, (y - h/2 - oy) / self.scale
x,y = c*x - s*y, s*x + c*y
return x+self.x, y+self.y
end
function camera:mousePosition(ox,oy,w,h)
local mx,my = love.mouse.getPosition()
return self:worldCoords(mx,my, ox,oy,w,h)
end
-- camera scrolling utilities
function camera:lockX(x, smoother, ...)
local dx, dy = (smoother or self.smoother)(x - self.x, self.y, ...)
self.x = self.x + dx
return self
end
function camera:lockY(y, smoother, ...)
local dx, dy = (smoother or self.smoother)(self.x, y - self.y, ...)
self.y = self.y + dy
return self
end
function camera:lockPosition(x,y, smoother, ...)
return self:move((smoother or self.smoother)(x - self.x, y - self.y, ...))
end
function camera:lockWindow(x, y, x_min, x_max, y_min, y_max, smoother, ...)
-- figure out displacement in camera coordinates
x,y = self:cameraCoords(x,y)
local dx, dy = 0,0
if x < x_min then
dx = x - x_min
elseif x > x_max then
dx = x - x_max
end
if y < y_min then
dy = y - y_min
elseif y > y_max then
dy = y - y_max
end
-- transform displacement to movement in world coordinates
local c,s = cos(-self.rot), sin(-self.rot)
dx,dy = (c*dx - s*dy) / self.scale, (s*dx + c*dy) / self.scale
-- move
self:move((smoother or self.smoother)(dx,dy,...))
end
-- the module
return setmetatable({new = new, smooth = camera.smooth},
{__call = function(_, ...) return new(...) end})

how do i add an end finction after x amount of time in lua

im trying to have smooth continues mouse movement instead of using moverelative. i would like the mouse to be able to move in any direction at varying speeds and to stop after for example 150ms and then instantly start moving in another direction for another 150ms and so on. so i dont think sleep will work. this is the lua code i have so far. i got this code from someone else from an old post and am not sure how to modify it for my needs
do
local frac_x, frac_y, prev_time = 0, 0
function StartMoving()
prev_time = GetRunningTime()
end
function MoveMouseForAWhile(x, y)
Sleep(1)
local time = GetRunningTime()
time, prev_time = time - prev_time, time
frac_x, frac_y = frac_x + time * x, frac_y + time * y
x, y = math.floor(frac_x), math.floor(frac_y)
frac_x, frac_y = frac_x - x, frac_y - y
while x ~= 0 or y ~= 0 do
local dx = math.min(127, math.max(-127, x))
local dy = math.min(127, math.max(-127, y))
x, y = x - dx, y - dy
MoveMouseRelative(dx, dy)
end
end
end
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event,arg)
if IsKeyLockOn("scrolllock")then
if IsMouseButtonPressed(3)then
repeat
if IsMouseButtonPressed(1) then
local speed = 1.5
StartMoving()
repeat
MoveMouseForAWhile(-0.25 * speed, .35 * speed)
until not IsMouseButtonPressed(1)
end
until not IsMouseButtonPressed(3)
end
end
end
i would like the mouse to be able to move in any direction at varying speeds and to stop after for example 150ms and then instantly start moving in another direction for another 150ms and so on.
do
local frac_x, frac_y, start_time, prev_time = 0, 0
function StartMoving()
prev_time = GetRunningTime()
start_time = prev_time
end
function MoveMouseForAWhile(x, y)
Sleep(1)
local time = GetRunningTime()
time, prev_time = time - prev_time, time
frac_x, frac_y = frac_x + time * x, frac_y + time * y
x, y = math.floor(frac_x), math.floor(frac_y)
frac_x, frac_y = frac_x - x, frac_y - y
while x ~= 0 or y ~= 0 do
local dx = math.min(127, math.max(-127, x))
local dy = math.min(127, math.max(-127, y))
x, y = x - dx, y - dy
MoveMouseRelative(dx, dy)
end
return prev_time - start_time
end
end
local sequence = {
{
duration = 150, -- milliseconds
speed = 1.5, -- pixels per 1 millisecond
direction = 30, -- degrees (0 = right, 90 = up, 180 = left, 270 = down)
},
{
duration = 150,
speed = 3,
direction = -60,
},
}
function OnEvent(event,arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsMouseButtonPressed(3) and IsKeyLockOn("scrolllock") then
for _, seq in ipairs(sequence) do
local angle = math.rad(seq.direction)
local speed_x, speed_y = math.cos(angle) * seq.speed, -math.sin(angle) * seq.speed
StartMoving()
repeat
local time = MoveMouseForAWhile(speed_x, speed_y)
until time >= seq.duration
if not IsMouseButtonPressed(1) then return end
end
end
end
Turn ScrollLock ON and press LMB while RMB is held down to test the script.
Release LMB to stop the script.

How can I make my Lua script do a natural curve down rather then juddering diagonally?

EnablePrimaryMouseButtonEvents (true);
function OnEvent(event,arg)
if IsKeyLockOn("numlock")then
if IsMouseButtonPressed(3)then
repeat
if IsMouseButtonPressed(1) then
repeat
MoveMouseRelative(0,11)
Sleep(15)
MoveMouseRelative(-1,0)
until not IsMouseButtonPressed(1)
end
until not IsMouseButtonPressed(3)
end
end
end
I tried this code in lua however it makes it judder and need it to curve slowly down. What can I try?
To move smoothly you may try to jump with shorter steps and sleep for shorter periods.
This would require special versions for Sleep and MoveMouseRelative:
do
local frac_x, frac_y = 0, 0
local orig_MoveMouseRelative = MoveMouseRelative
function MoveMouseRelative(x, y)
frac_x, frac_y = frac_x + x, frac_y + y
x, y = math.floor(frac_x), math.floor(frac_y)
frac_x, frac_y = frac_x - x, frac_y - y
while x ~= 0 or y ~= 0 do
local dx = math.min(127, math.max(-127, x))
local dy = math.min(127, math.max(-127, y))
x, y = x - dx, y - dy
orig_MoveMouseRelative(dx, dy)
end
end
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)
local orig_Sleep = Sleep
function Sleep(ms)
if ms > 30 then
return orig_Sleep(ms)
else
return busyloop(ms * coefficient)
end
end
end
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event,arg)
if IsKeyLockOn("numlock")then
if IsMouseButtonPressed(3)then
repeat
if IsMouseButtonPressed(1) then
repeat
MoveMouseRelative(-0.1, 1.1)
Sleep(1.5) -- should be about 1ms for really smooth movement
until not IsMouseButtonPressed(1)
end
until not IsMouseButtonPressed(3)
end
end
end
UPDATE
If you don't have unused CPU core there is another version, not so smooth, but with a constant speed
do
local frac_x, frac_y, prev_time = 0, 0
function StartMoving()
prev_time = GetRunningTime()
end
function MoveMouseForAWhile(x, y)
Sleep(1)
local time = GetRunningTime()
time, prev_time = time - prev_time, time
frac_x, frac_y = frac_x + time * x, frac_y + time * y
x, y = math.floor(frac_x), math.floor(frac_y)
frac_x, frac_y = frac_x - x, frac_y - y
while x ~= 0 or y ~= 0 do
local dx = math.min(127, math.max(-127, x))
local dy = math.min(127, math.max(-127, y))
x, y = x - dx, y - dy
MoveMouseRelative(dx, dy)
end
end
end
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event,arg)
if IsKeyLockOn("numlock")then
if IsMouseButtonPressed(3)then
repeat
if IsMouseButtonPressed(1) then
local speed = 1.5
StartMoving()
repeat
MoveMouseForAWhile(-0.1 * speed, 1.1 * speed)
until not IsMouseButtonPressed(1)
end
until not IsMouseButtonPressed(3)
end
end
end

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

Resources