lghub驱动级别鼠标控制的动态链接库dll

本文介绍了一个Python库,通过ctypes调用dll文件,提供高级别的鼠标控制功能,如滑轮模拟、平滑移动和精确点击,特别适合于脚本编写和FPS游戏中的鼠标操作。附有下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个是经过修改后完善后的版本,纯软件任何鼠标均可使用,驱动级别不会被屏蔽,过ACE检测,可用于脚本以及鼠标模拟,包括各类fps游戏。

功能即函数

可以在低于10ms内完成快速截图(基于截图的分辨率所定),且截图会始终自动保持居中区域截图的效果,其函数与对应的参数如下:
InitializeScreenshot(char* email, char* password)
CaptureScreen(int x, int y, int width, int height, int screenMode) # 截图分x:表示区域的左侧left, y:表示截图区域的顶部top, width:表示截图区域的宽度, height表示截图区域的高度,screenMode截图区域的模式,分别为0:低于或等于1920*1080分辨率选择, 1:高于或等于2560*1440分辨率选择,

python使用示例

import ctypes
import numpy as np
import cv2
class MouseState(ctypes.Structure): # 鼠标监听状态
    _fields_ = [
        ("leftButtonDown", ctypes.c_int),
        ("rightButtonDown", ctypes.c_int),
        ("middleButtonDown", ctypes.c_int),
        ("x1ButtonDown", ctypes.c_int),
        ("x2ButtonDown", ctypes.c_int)
    ]

class MouseOpt():
    def __init__(self, email, password):
        super().__init__()
        ctypes.windll.user32.SetProcessDPIAware()
        #print("鼠标加载成功!!!")
        try:  # 加载驱动
            # 获取当前绝对路径
            self.driver = ctypes.WinDLL(r'./mouse_dll.dll')
            # 声明函数原型
            self.listen_mouse = self.driver.listen_mouse
            self.listen_mouse.restype = MouseState
            email = email.encode('utf-8')  # example@qq.com为注册的email此处必须将后面必须指定email的编码为('utf-8')
            password = password.encode('utf-8')  # password是你注册时候的密码此处必须将后面必须指定email的编码为('utf-8')
            self.driver.mouse_open(email, password) #控制鼠标

            # 定义截屏参数和返回类型
            self.driver.InitializeScreenshot.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
            self.driver.InitializeScreenshot.restype = None

            self.driver.ReleaseScreenshotResources.argtypes = []
            self.driver.ReleaseScreenshotResources.restype = None

            self.driver.CaptureScreen.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int,ctypes.c_int]
            self.driver.CaptureScreen.restype = ctypes.POINTER(ctypes.c_ubyte)

            self.driver.FreeImageData.argtypes = [ctypes.POINTER(ctypes.c_ubyte)]
            self.driver.FreeImageData.restype = None

            # 初始化像素指针
            self.pixels_ptr = None
            # 初始化截图资源
            self.driver.InitializeScreenshot(email, password)

        except FileNotFoundError:
            print(f'错误, DLL 文件没有找到')

    def click(self, code): # 点击鼠标左键
        self.driver.mouse_click(code)

    def move(self, x, y, oprt): # 移动鼠标
        self.driver.mouse_move(x, y, 0, oprt)

    def press(self, oprt):
        self.driver.mouse_press(oprt)

    def listen_mouse(self): # 获取鼠标的按键情况
        # 调用获取鼠标状态函数
        return self.listen_mouse

    def shotx(self, left, top, width, height, flagScreen):
        """
        截取屏幕区域并返回图像数据。
        :param left: 截取区域的左边界
        :param top: 截取区域的上边界
        :param width: 截取区域的宽度
        :param height: 截取区域的高度
        :param flagScreen: 屏幕模式(0 表示分辨率低于或等于 1920x1080, 1 表示分辨率高于或等于 2560x1440)
        """
        if width % 4 != 0:
            for i in range(1, 4):
                if(width - i)%4 == 0:
                    left = round(left+(i/2))
                    width -= i
                    break
        # 调用 DLL 截屏
        self.pixels_ptr = self.driver.CaptureScreen(left, top, width, height, flagScreen)
        # 转换为 NumPy 数组
        cpp_image = np.ctypeslib.as_array(self.pixels_ptr, shape=(height, width, 3))
        return cpp_image

    def destroy(self):
        # 释放图像数据
        if self.pixels_ptr:
            self.driver.FreeImageData(self.pixels_ptr)
            self.pixels_ptr = None
        # 释放截图资源
        # self.screenshot_dll.ReleaseScreenshotResources()

    def __enter__(self):
        """
        进入上下文时,返回类实例。
        """
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """
        离开上下文时自动销毁资源。
        """
        self.destroy()
if __name__ == '__main__':
    email = "example@qq.com" # example@qq.com为注册的email
    password = "********" #  password是你注册时候的密码
    drive = MouseOpt(email, password)
    drive.move(100, 100, 3)
    v = drive.listen_mouse() # 获取鼠标的按键情况
    # 打印获取用户的的鼠标状态.
    print("Left button down:", v.leftButtonDown) # 如果值为1表示当前鼠标左键正在被按下,0表示松开
    print("Right button down:", v.rightButtonDown) # 如果值为1表示当前鼠标右正在被按下,0表示松开
    print("Middle button down:", v.middleButtonDown) # 如果值为1表示当前鼠标中键正在被按下,0表示松开
    print("X1 button down:", v.x1ButtonDown) # 如果值为1表示当前鼠标侧键1正在被按下,0表示松开
    print("X2 button down:", v.x2ButtonDown) # 如果值为1表示当前鼠标侧键2正在被按下,0表示松开
    image = drive.shotx(0, 0, 1919, 1080, 1) # 截图操作
    cv2.imshow("image", image)
    cv2.waitKey(0)  # 等待按键
    cv2.destroyAllWindows()  # 关闭窗口

mouse_open(char* email, char* password) #打开mouse控制调用传入参数即注册的email和password即可。

mouse_close(void) #关闭控制

mouse_move(int x, int y, char wheel, int oprt); # 前两个参数分别为x轴、y轴的移动的像素值,第三个参数wheel为滑轮控制,第四个参数oprt为每次的平滑移动的程度有三个范围(1:没有平滑,2:小幅度平滑 3:大幅度平滑)(此处采用模拟真人鼠标的移动平滑算法,如果该处调节oprt用于fps游戏可以很好模拟人的鼠标移动的平滑度)。

mouse_click(int button) # button为1表示点击鼠标左键。2为右键

mouse_press(int button) #  button为1表示按下鼠标左键。2为右键

mouse_release(void) # 表示松开按下鼠标。

listen_mouse() # 获取当前用户按下或松开鼠标的状态,此响应为1ms级别,比绝大部分对于鼠标响应代码以及软件都要快。分别有五个返回值: int leftButtonDown; int rightButtonDown; int middleButtonDown; int x1ButtonDown; int x2ButtonDown;对应的1表示当前按键按下,0表示松开,用法例子如上面的代码所示。

我将其写成一个类python直接调用:

import ctypes
import numpy as np
import cv2
class MouseState(ctypes.Structure): # 鼠标监听状态
    _fields_ = [
        ("leftButtonDown", ctypes.c_int),
        ("rightButtonDown", ctypes.c_int),
        ("middleButtonDown", ctypes.c_int),
        ("x1ButtonDown", ctypes.c_int),
        ("x2ButtonDown", ctypes.c_int)
    ]
 
class MouseOpt():
    def __init__(self, email, password):
        super().__init__()
        ctypes.windll.user32.SetProcessDPIAware()
        #print("鼠标加载成功!!!")
        try:  # 加载驱动
            # 获取当前绝对路径
            self.driver = ctypes.WinDLL(r'./mouse_dll.dll')
            # 声明函数原型
            self.listen_mouse = self.driver.listen_mouse
            self.listen_mouse.restype = MouseState
            email = email.encode('utf-8')  # example@qq.com为注册的email此处必须将后面必须指定email的编码为('utf-8')
            password = password.encode('utf-8')  # password是你注册时候的密码此处必须将后面必须指定email的编码为('utf-8')
            self.driver.mouse_open(email, password) #控制鼠标
 
            # 定义截屏参数和返回类型
            self.driver.InitializeScreenshot.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
            self.driver.InitializeScreenshot.restype = None
 
            self.driver.ReleaseScreenshotResources.argtypes = []
            self.driver.ReleaseScreenshotResources.restype = None
 
            self.driver.CaptureScreen.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int,ctypes.c_int]
            self.driver.CaptureScreen.restype = ctypes.POINTER(ctypes.c_ubyte)
 
            self.driver.FreeImageData.argtypes = [ctypes.POINTER(ctypes.c_ubyte)]
            self.driver.FreeImageData.restype = None
 
            # 初始化像素指针
            self.pixels_ptr = None
            # 初始化截图资源
            self.driver.InitializeScreenshot(email, password)
 
        except FileNotFoundError:
            print(f'错误, DLL 文件没有找到')
 
    def click(self, code): # 点击鼠标左键
        self.driver.mouse_click(code)
 
    def move(self, x, y, oprt): # 移动鼠标
        self.driver.mouse_move(x, y, 0, oprt)
 
    def press(self, oprt):
        self.driver.mouse_press(oprt)
 
    def listen_mouse(self): # 获取鼠标的按键情况
        # 调用获取鼠标状态函数
        return self.listen_mouse
 
    def shotx(self, left, top, width, height, flagScreen):
        """
        截取屏幕区域并返回图像数据。
        :param left: 截取区域的左边界
        :param top: 截取区域的上边界
        :param width: 截取区域的宽度
        :param height: 截取区域的高度
        :param flagScreen: 屏幕模式(0 表示分辨率低于或等于 1920x1080, 1 表示分辨率高于或等于 2560x1440)
        """
        if width % 4 != 0:
            for i in range(1, 4):
                if(width - i)%4 == 0:
                    left = round(left+(i/2))
                    width -= i
                    break
        # 调用 DLL 截屏
        self.pixels_ptr = self.driver.CaptureScreen(left, top, width, height, flagScreen)
        # 转换为 NumPy 数组
        cpp_image = np.ctypeslib.as_array(self.pixels_ptr, shape=(height, width, 3))
        return cpp_image
 
    def destroy(self):
        # 释放图像数据
        if self.pixels_ptr:
            self.driver.FreeImageData(self.pixels_ptr)
            self.pixels_ptr = None
        # 释放截图资源
        # self.screenshot_dll.ReleaseScreenshotResources()
 
    def __enter__(self):
        """
        进入上下文时,返回类实例。
        """
        return self
 
    def __exit__(self, exc_type, exc_val, exc_tb):
        """
        离开上下文时自动销毁资源。
        """
        self.destroy()
if __name__ == '__main__':
    email = "example@qq.com" # example@qq.com为注册的email
    password = "********" #  password是你注册时候的密码
    drive = MouseOpt(email, password)
    drive.move(100, 100, 3)
    v = drive.listen_mouse() # 获取鼠标的按键情况
    # 打印获取用户的的鼠标状态.
    print("Left button down:", v.leftButtonDown) # 如果值为1表示当前鼠标左键正在被按下,0表示松开
    print("Right button down:", v.rightButtonDown) # 如果值为1表示当前鼠标右正在被按下,0表示松开
    print("Middle button down:", v.middleButtonDown) # 如果值为1表示当前鼠标中键正在被按下,0表示松开
    print("X1 button down:", v.x1ButtonDown) # 如果值为1表示当前鼠标侧键1正在被按下,0表示松开
    print("X2 button down:", v.x2ButtonDown) # 如果值为1表示当前鼠标侧键2正在被按下,0表示松开
    image = drive.shotx(0, 0, 1919, 1080, 1) # 截图操作
    cv2.imshow("image", image)
    cv2.waitKey(0)  # 等待按键
    cv2.destroyAllWindows()  # 关闭窗口

下载链接:

驱动级别鼠标控制

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值