Lua Script for Controlling WASD Keys via Mouse Movement - lua

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

Related

Sideway Movement When Moving Cursor Horizontally Only

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

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 use lerp correctly, making sure it reaches the target value

I am trying to create a player in love2D, and I have a lerp function that brings the players speed to 0 , but I know I am not using the lerp function correctly because it never reaches the end value and starts acting funky and crazy, how do I use the lerp function correctly.
function love.load()
moon = require "Library/moon_light"
position = {x = 0, y = 0}
H = 0
V = 0
new_H = 0
new_V = 0
position["x"] = moon.Mid_point(0,0,1280,720)[1]
position["y"] = moon.Mid_point(0,0,1280,720)[2]
accel = 8
max_speed = 80
fps = 60
friction = 6
end
function love.update(dt)
if love.keyboard.isDown('escape') then
love.event.push('quit')
end
if love.keyboard.isDown("d") then
H = 1
elseif love.keyboard.isDown("a") then
H = -1
else
H = 0
end
if love.keyboard.isDown("w") then
V = -1
elseif love.keyboard.isDown("s") then
V = 1
else
V = 0
end
if H ~= 0 then -- use a new value and give it acceleration
new_H = H * accel * dt * fps
end
if H == 0 then -- Check if the player isnt moving lerp it to 0 so stopping the movement
new_H = moon.Lerp(new_H,0,friction * dt)
end
if V ~= 0 then
new_V = V * accel * dt * fps
end
if V == 0 then
new_V = moon.Lerp(new_V,0,friction * dt)
end
position["x"] = position["x"] + new_H -- Set the motion to the position
position["y"] = position["y"] + new_V
end
function love.draw()
love.graphics.rectangle("fill",position["x"] - 25,position["y"] - 25,50,50)
love.graphics.print("Horiztonal value : " .. H .. "\nVertical value : " .. V,100,400)
love.graphics.print("new_H value : " .. new_H .. "\nnew_V value : " .. new_V,100,440)
end
The lerp function, I think nothing is wrong with it, but just in case.
function moon_light.Lerp(start_v,end_v,percent)
return start_v + (end_v - start_v) * percent
end
EDIT I changed the lerp to be time based, from the suggestion and I also made a new variable called motion it seems setting the lerp to its self caused a weird issue, making it never reach 0:
if H ~= 0 then -- use a new value and give it acceleration
H_counter = 0
new_H = H * accel * dt * fps
motion_H = new_H
end
if H == 0 then -- Check if the player isnt moving lerp it to 0 so stopping the movement
if H_counter < slow_down_duration then
motion_H = moon.Lerp(new_H,0,H_counter/slow_down_duration)
H_counter = H_counter + dt
else
new_H = 0
motion_H = 0
end
end
if V ~= 0 then
V_counter = 0
new_V = V * accel * dt * fps
motion_V = new_V
end
if V == 0 then
if V_counter < slow_down_duration then
motion_V = moon.Lerp(new_V,0,V_counter/slow_down_duration)
V_counter = V_counter + dt
else
new_V = 0
motion_V = 0
end
end
position["x"] = position["x"] + motion_H -- Set the motion to the position
position["y"] = position["y"] + motion_V

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

Resources