Logitech LUA 脚本添加睡眠定时器

想知道如何将睡眠定时器添加到我的 LUA 脚本中,以便它不会像可能一样快速循环并按下 0x29,我想使它在按下我的鼠标上的按钮 1 和 3 时以每 3-4 秒钟的速度按下键 0x29。

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
点赞
用户1847592
用户1847592

你可以通过 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)     -- 模拟按下并释放按键(VK_ESCAPE)
            last_time = GetRunningTime() -- 记录上一次按下时间
        end
    end
end
2021-05-25 04:56:05