You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何合并两个可通过鼠标键切换功能的Lua脚本?

合并Lua脚本实现鼠标键切换两种输入功能

需求说明

需要合并两个Lua脚本,实现通过鼠标键切换两种自动输入功能:

  • 模式1:每隔2000ms输入一次a
  • 模式2:每隔2000ms输入一次H

触发逻辑为:NumLock开启状态下,按住鼠标中键(按键3)时按下左键(按键1),执行对应模式的输入操作。此前尝试通过其他按键的IsKeyLockOn来区分两种模式,但无法正常工作。

原脚本示例

输入a的脚本

function OnEvent(event, arg)
    if IsKeyLockOn("numlock") then
        if IsMouseButtonPressed(3) then
            repeat  
                if IsMouseButtonPressed(1) then
                    repeat
                        PressKey("a")
                        Sleep(100)
                        ReleaseKey("a")
                        Sleep(2000)
                    until not IsMouseButtonPressed(1)
                end         
            until not IsMouseButtonPressed(3)
        end     
    end
end

输入H的脚本

function OnEvent(event, arg)
    if IsKeyLockOn("numlock") then
        if IsMouseButtonPressed(3) then
            repeat  
                if IsMouseButtonPressed(1) then
                    repeat
                        PressKey("h")
                        Sleep(100)
                        ReleaseKey("h")
                        Sleep(2000)
                    until not IsMouseButtonPressed(1)
                end         
            until not IsMouseButtonPressed(3)
        end     
    end
end

解决方案

改用全局变量记录当前模式,通过鼠标侧键(比如按键4)切换模式,避免依赖锁定键检测的问题。以下是合并后的完整脚本:

-- 全局变量,记录当前要输入的字符,初始为"a"
local current_char = "a"

function OnEvent(event, arg)
    -- 监听鼠标侧键4按下,切换输入字符
    if event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
        current_char = current_char == "a" and "H" or "a"
        return
    end

    -- 原有的触发逻辑,替换为全局变量对应的字符
    if IsKeyLockOn("numlock") then
        if IsMouseButtonPressed(3) then
            repeat  
                if IsMouseButtonPressed(1) then
                    repeat
                        PressKey(current_char)
                        Sleep(100)
                        ReleaseKey(current_char)
                        Sleep(2000)
                    until not IsMouseButtonPressed(1)
                end         
            until not IsMouseButtonPressed(3)
        end     
    end
end

逻辑说明

  1. 定义全局变量current_char,初始设置为a,表示默认模式是输入a
  2. 当按下鼠标侧键4(可根据需求改为按键5)时,切换current_char的值,在a和H之间互换
  3. 保留原有的NumLock检测、鼠标中键+左键触发逻辑,将输入的固定字符替换为current_char变量,实现模式切换

为何原方法失效?

IsKeyLockOn仅适用于带有锁定状态的按键(如NumLock、CapsLock、ScrollLock),普通按键没有锁定状态,因此用它来区分模式会失效。改用鼠标侧键切换全局变量的方式,逻辑更直接可靠。

附API文档说明:该文档为Logitech鼠标宏API参考手册,包含事件监听、按键状态检测、模拟按键输入等常用函数的详细用法。

内容的提问来源于stack exchange,提问作者user18953534

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 04:05:40