import ctypes import os import time import pynput import winsound try: root = os.path.abspath(os.path.dirname(__file__)) driver = ctypes.CDLL(f'{root}/logitech.driver.dll') ok = driver.device_open() == 1 # 该驱动每个进程可打开一个实例 if not ok: print('Error, GHUB or LGS driver not found') except FileNotFoundError: print(f'Error, DLL file not found') class Logitech: class mouse: """ code: 1:左键, 2:中键, 3:右键 """ @staticmethod def press(code): if not ok: return driver.mouse_down(code) @staticmethod def release(code): if not ok: return driver.mouse_up(code) @staticmethod def click(code): if not ok: return driver.mouse_down(code) driver.mouse_up(code) @staticmethod def scroll(a): """ a:没搞明白 """ if not ok: return driver.scroll(a) @staticmethod def move(x, y): """ 相对移动, 绝对移动需配合 pywin32 的 win32gui 中的 GetCursorPos 计算位置 pip install pywin32 -i https://pypi.tuna.tsinghua.edu.cn/simple x: 水平移动的方向和距离, 正数向右, 负数向左 y: 垂直移动的方向和距离 """ if not ok: return if x == 0 and y == 0: return driver.moveR(x, y, True) class keyboard: """ 键盘按键函数中,传入的参数采用的是键盘按键对应的键码 code: 'a'-'z':A键-Z键, '0'-'9':0-9, 其他的没猜出来 """ @staticmethod def press(code): if not ok: return driver.key_down(code) @staticmethod def release(code): if not ok: return driver.key_up(code) @staticmethod def click(code): if not ok: return driver.key_down(code) driver.key_up(code) if __name__ == '__main__': # 测试 winsound.Beep(800, 200) def release(key): if key == pynput.keyboard.Key.end: # 结束程序 End 键 winsound.Beep(400, 200) return False elif key == pynput.keyboard.Key.home: # 移动鼠标 Home 键 winsound.Beep(600, 200) # Logitech.mouse.move(100, 100) with pynput.keyboard.Listener(on_release=release) as k: k.join()