Skip to content

调用HID操作功能

lua
-- 创建 MouseReceiver 对象
local receiver = newMouseReceiver()

-- 鼠标测试函数
local function testMouse()
    print("开始鼠标测试 (按ESC键退出)...")
    print("将检测以下鼠标按钮状态:")
    print("  Get_Mouse_Left: 左键")
    print("  Get_Mouse_Right: 右键")
    print("  Get_Mouse_Middle: 中键")
    print("  Get_Mouse_Side1: 侧键1")
    print("  Get_Mouse_Side2: 侧键2")

    -- ESC键的键码
    local ESC_KEY = 0x29

    while true do
        -- 检测中键是否按下
        if receiver:Get_Mouse_Middle() then
            print("检测到中键按下,退出鼠标测试")
            break
        end

        -- 打印鼠标按钮状态
        print(string.format(
            "左键:%s 右键:%s 侧键1:%s 侧键2:%s",
            receiver:Get_Mouse_Left() and "按下" or "释放",
            receiver:Get_Mouse_Right() and "按下" or "释放",
            receiver:Get_Mouse_Side1() and "按下" or "释放",
            receiver:Get_Mouse_Side2() and "按下" or "释放"
        ))

        sleep(0.1) -- 100毫秒检测间隔
    end

    -- 测试鼠标移动和滚轮
    print("测试鼠标移动和滚轮...")
    receiver:Mouse_MoreR(50, -50) -- 移动鼠标
    print("移动调用完成")

    receiver:Mouse_Scroll(3) -- 滚动鼠标
    print("滚动调用完成")
end

-- 键盘测试函数
local function testKeyboard()
    print("开始键盘测试A键 (按ESC键退出)...")

    -- ESC键的键码
    local ESC_KEY = 0x29
    local key_code = 0x04

    while true do
        -- 检测ESC键是否按下
        if receiver:Get_Key_State(ESC_KEY) then
            print("检测到ESC键按下,退出键盘测试")
            break
        end

        -- 检测指定键状态
        local state = receiver:Get_Key_State(key_code)
        print(string.format("键码 0x%02X 状态: %s", key_code, state and "按下" or "释放"))

        sleep(0.1) -- 100毫秒检测间隔
    end

    -- 测试按键按下和释放
    print("测试按键按下和释放...")
    receiver:Keyboard_PressKey(key_code)
    print(string.format("Keyboard_PressKey(0x%02X) 调用完成", key_code))

    print(string.format("Get_Key_State(0x%02X): %s", key_code, receiver:Get_Key_State(key_code) and "按下" or "释放"))

    receiver:Keyboard_ReleaseKey(key_code)
    print(string.format("Keyboard_ReleaseKey(0x%02X) 调用完成", key_code))

    print(string.format("Get_Key_State(0x%02X): %s", key_code, receiver:Get_Key_State(key_code) and "按下" or "释放"))
end

-- 请选择测试模式 1. 鼠标测试 2. 键盘测试 输入数字选择(1或2)
local choice = "1"

if choice == "1" then
    testMouse()
elseif choice == "2" then
    testKeyboard()
else
    print("无效选择")
end

-- 结束时停止
receiver:stop()
print("测试完成")