如何在 lua 中添加一个结束函数一段时间后
2021-12-22 4:29:43
收藏:0
阅读:238
评论:1
我正在尝试让鼠标平稳地运动,而不是使用 moverelative。我希望鼠标能够以不同的速度在任意方向上移动,并在运动 150 毫秒后停止,然后立即开始在另一个方向上移动另外 150 毫秒,以此类推。所以我认为 sleep 不起作用。这是我目前为止的 lua 代码。我从旧帖子中得到了这段代码,不确定如何修改它以满足我的需求
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
原文链接 https://stackoverflow.com/questions/70444168
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
我希望鼠标能以不同的速度向任意方向移动,并在例如150ms后停止,然后立即按另一个方向移动另外150ms,依此类推。
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, -- 毫秒 speed = 1.5, -- 每毫秒的像素数 direction = 30, -- 角度(0 = 右,90 = 上,180 = 左,270 = 下) }, { 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
打开滚动锁定,按住RMB,同时按下LMB以测试脚本。
松开LMB以停止脚本。