import sys
import os
import ctypes
from ctypes import *
import win32com.client
import time

# 路径配置
dll_path = os.path.abspath("ddl/wyhkm.dll")
print(f"测试 DLL: {dll_path}")

# 1. 注册并创建对象
try:
    hkmdll = windll.LoadLibrary(dll_path)
    hkmdll.DllInstall.argtypes = (c_long, c_longlong)
    if hkmdll.DllInstall(1, 2) < 0:
        print("注册失败!")
        sys.exit(0)
    
    wyhkm = win32com.client.Dispatch("wyp.hkm")
    print(f"硬件对象已创建，版本: {hex(wyhkm.GetVersion())}")
except Exception as e:
    print(f"初始化异常: {e}")
    sys.exit(0)

# 2. 打开设备并配置模式 (使用已验证的 Index 0)
dev_id = wyhkm.SearchDevice(0x2612, 0x1701, 0)
if dev_id == -1 or not wyhkm.Open(dev_id, 0):
    print("无法打开硬件设备")
    sys.exit(0)

# 关键模式设置
wyhkm.SetMode(1, 1)  # 键盘增强模式
wyhkm.SetMode(2, 1)  # 鼠标仿真模式
wyhkm.SetKeyInterval(30, 50) # 设置合理的按键间隔

print(f"\n--- 硬件就绪 (ID: {dev_id}) ---")
print("请在 5 秒内切换到记事本进行按键测试...")
time.sleep(5)

# 3. 常用按键序列测试
test_keys = [
    ("方向键", ["W", "A", "S", "D"]),
    ("功能键", ["SPACE", "X", "B", "M", "TAB", "ESCAPE"]),
    ("数字键", ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]),
    ("组合键测试", ["CTRL+C", "ALT+Z"])
]

for category, keys in test_keys:
    print(f"\n正在测试 {category}: {keys}")
    for k in keys:
        print(f"  发送按键: {k}")
        wyhkm.KeyPress(k)
        time.sleep(0.5)

# 4. 鼠标同步测试
print("\n正在测试鼠标移动 (MoveR 100, 100)...")
wyhkm.MoveR(100, 100)

time.sleep(1)
wyhkm.Close()
print("\n所有常用按键测试完成！")
