Python模拟真人动态生成鼠标滑动路径
一.简介
鼠标轨迹算法是一种模拟人类鼠标操作的程序,它能够模拟出自然而真实的鼠标移动路径。
鼠标轨迹算法的底层实现采用C/C++语言,原因在于C/C++提供了高性能的执行能力和直接访问操作系统底层资源的能力。
鼠标轨迹算法具有以下优势:
- 模拟人工轨迹:算法能够模拟出非贝塞尔曲线的自然鼠标移动,避免了机械式的直线移动。
- 适当的停顿/加速/减速:算法能够根据需要模拟出鼠标的停顿、加速和减速,使得轨迹更加真实。
- 随机轨迹:在固定两点间,算法能够生成不同的随机轨迹,增加了轨迹的不可预测性。
二.应用场景
- 游戏鼠标轨迹检测(检测能过无畏fps类型、传奇、梦幻等游戏,已经在游戏中验证)
- 滑块拖动验证
- 部分网页鼠标轨迹检测
三.支持多种编程语言
1.C++头文件
- /******************************************************************************************/
-
- @SDK功能描述:C++鼠标轨迹
-
- /******************************************************************************************/
-
- #ifndef _SN_SDK_H__
-
- #define _SN_SDK_H__
-
- #include <windows.h>
-
- //返回参数
-
- typedef struct SN_RESULT {
-
- int code; //错误码,如果为 0 表示成功,否则表示错误号
-
- char message[4096]; //错误信息,如果为 "OK" 表示成功,否则返回错误信息
-
- }SN_RESULT;
-
- //坐标参数
-
- typedef struct SN_POINT
-
- {
-
- int x; //屏幕坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
-
- int y; //屏幕坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
-
- }SN_POINT;
-
- //轨迹参数
-
- typedef struct SN_POINT_PARAMS
-
- {
-
- struct SN_POINT point;//屏幕坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
-
- int delayTime; //延时时间(单位:毫秒),仅供参考
-
- }SN_POINT_PARAMS;
-
- /*创建句柄
- *
- * 参数:
- * [in] szKey: 卡密
- * [out] pResult: 返回错误信息,参数pResult.code(错误码)如果为 0 表示成功,否则表示错误号;
- *
- * 返回值:成功返回句柄,失败返回NULL
- *
- */
-
- HANDLE WINAPI apiSNCreateHandle(char* szKey, SN_RESULT* pResult);
-
- /*获取鼠标移动轨迹
- *
- * 参数:
- * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
- * [in] startPoint: 开始坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
- * [in] endPoint: 结束坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
- * [out] points: 轨迹数组,如果数组中元素 point 出现(-1,-1),表示鼠标轨迹结束
- *
- * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
- *
- */
-
- SN_RESULT WINAPI apiSNMouseMove(HANDLE handle, SN_POINT startPoint, SN_POINT endPoint, SN_POINT_PARAMS* points);
-
- /*获取版本号
- *
- * 参数:
- * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
- * [out] szVersion: 版本号
- *
- * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
- *
- */
-
- SN_RESULT WINAPI apiSNGetVersion(HANDLE handle, char* szVersion);
-
- /*获取错误信息
- *
- * 参数:
- * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
- *
- * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
- *
- */
-
- SN_RESULT WINAPI apiSNGetError(HANDLE handle);
-
- /*释放句柄(内存)
- *
- * 参数:
- * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
- *
- * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
- *
- */
-
- SN_RESULT WINAPI apiSNDestroyHandle(HANDLE handle);
-
- #endif // !_SN_SDK_H__
2.其他编程语言
为了易于集成和使用,我们将鼠标轨迹算法封装为DLL(动态链接库)。这种封装方式不仅保留了算法的性能优势,还提供了跨平台和跨语言的兼容性,目前支持编程语言如下:
- C++
- Python
- 易语言
推算轨迹算法耗时均为毫秒级,<= 5ms ,速度超快,fps类型游戏完全无压力!
3.鼠标轨迹API调用流程图
注意:如果是多线程,每个线程都需要通过apiSNCreateHandle创建HANDLE句柄,这样才能多个线程互不影响
4.Python加载C++鼠标轨迹接口
- '''
-
- @SDK功能描述:鼠标轨迹
-
- '''
-
- import ctypes
- import os
- import sys
-
- # 创建句柄
- key = "SNKJuSrrrTnQ5UXYr4zr6XEveL7V2jg2X9h5BHGS5Des" # 字符串
- key_bytes = key.encode('utf-8') # 将字符串转换为 bytes
-
- #设置模型文件路径
- onnx = "d://SNTrack.onnx" # 字符串
- onnx_bytes = onnx.encode('utf-8') # 将字符串转换为 bytes
-
- # 假设 DLL 文件名为 SNSDK.dll
- sn_sdk = ctypes.WinDLL('d://SNSDK.dll')
-
- # 定义 SN_RESULT 结构体
- class SN_RESULT(ctypes.Structure):
- _fields_ = [("code", ctypes.c_int),
- ("message", ctypes.c_char * 4096)]
-
- # 定义 SN_POINT 结构体
- class SN_POINT(ctypes.Structure):
- _fields_ = [("x", ctypes.c_int),
- ("y", ctypes.c_int)]
-
- # 定义 SN_POINT_PARAMS 结构体
- class SN_POINT_PARAMS(ctypes.Structure):
- _fields_ = [("point", SN_POINT),
- ("delayTime", ctypes.c_int)]
-
- # 定义函数原型
- sn_sdk.apiSNCreateHandle.argtypes = [ctypes.POINTER(ctypes.c_char),ctypes.POINTER(ctypes.c_char), ctypes.POINTER(SN_RESULT)]
- sn_sdk.apiSNCreateHandle.restype = ctypes.c_void_p
-
- sn_sdk.apiSNGetVersion.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_char)]
- sn_sdk.apiSNGetVersion.restype = SN_RESULT
-
- sn_sdk.apiSNMouseMove.argtypes = [ctypes.c_void_p, SN_POINT, SN_POINT, ctypes.POINTER(SN_POINT_PARAMS)]
- sn_sdk.apiSNMouseMove.restype = SN_RESULT # 根据实际情况调整
-
- sn_sdk.apiSNDestroyHandle.argtypes = [ctypes.c_void_p]
- sn_sdk.apiSNDestroyHandle.restype = SN_RESULT
-
-
-
-
- result = SN_RESULT() # 创建 SN_RESULT 实例
- handle = sn_sdk.apiSNCreateHandle(key_bytes, onnx_bytes,ctypes.byref(result))
- if result.code != 0:
- message = result.message.decode('gbk', errors='replace').strip()
- print("Result message:", message)
- else:
- print("Handle created successfully")
-
- # 获取版本号
- version = ctypes.create_string_buffer(4096)
- version_result = sn_sdk.apiSNGetVersion(handle, version)
- if version_result.code != 0:
- message = result.message.decode('gbk', errors='replace').strip()
- print("Result message:", message)
- else:
- message = result.message.decode('gbk', errors='replace').strip()
- print("Result message:", version.value.decode())
-
- # 获取轨迹
- # 定义开始和结束坐标
- start_point = SN_POINT(100, 100)
- end_point = SN_POINT(800, 800)
-
- # 假设返回的轨迹点数量
- num_points = 4096
-
- # 创建一个数组来接收轨迹点
- points_array = (SN_POINT_PARAMS * num_points)()
-
- # 调用 apiSNMouseMove 函数
- move_result = sn_sdk.apiSNMouseMove(handle, start_point, end_point, points_array)
-
- # 检查结果
- if move_result.code != 0:
- message = result.message.decode('gbk', errors='replace').strip()
- print("Result message:", message)
- else:
- # 遍历并打印每个点
- for i in range(num_points):
- if points_array[i].point.x == -1 and points_array[i].point.y == -1:
- break # 轨迹结束
- print(f"Point {i}: ({points_array[i].point.x}, {points_array[i].point.y},{points_array[i].delayTime})") # X坐标 ,Y坐标 ,延时时间
-
-
-
- # 释放句柄
- destroy_result = sn_sdk.apiSNDestroyHandle(handle)
- if destroy_result.code != 0:
- message = result.message.decode('gbk', errors='replace').strip()
- print("Result message:", message)
- else:
- print("Handle destroyed successfully")
-
-
- '''
- 输出鼠标轨迹如下:
-
- Handle created successfully
- Result message: 1.0
- Point 0: (100, 100,0)
- Point 1: (100, 98,10)
- Point 2: (103, 98,15)
- Point 3: (111, 98,16)
- Point 4: (116, 101,15)
- Point 5: (122, 104,2)
- Point 6: (129, 107,13)
- Point 7: (135, 109,2)
- Point 8: (144, 112,14)
- Point 9: (155, 117,2)
- Point 10: (167, 123,14)
- Point 11: (180, 128,2)
- Point 12: (193, 134,13)
- Point 13: (209, 138,2)
- Point 14: (225, 144,13)
- Point 15: (238, 149,5)
- Point 16: (254, 157,10)
- Point 17: (269, 162,5)
- Point 18: (282, 168,11)
- Point 19: (298, 175,5)
- Point 20: (311, 180,10)
- Point 21: (326, 185,6)
- Point 22: (341, 193,9)
- Point 23: (369, 211,15)
- Point 24: (396, 231,16)
- Point 25: (419, 251,16)
- Point 26: (442, 270,16)
- Point 27: (461, 285,17)
- Point 28: (481, 300,15)
- Point 29: (491, 311,15)
- Point 30: (502, 319,2)
- Point 31: (513, 329,14)
- Point 32: (523, 343,2)
- Point 33: (535, 355,14)
- Point 34: (546, 369,0)
- Point 35: (558, 383,15)
- Point 36: (570, 397,2)
- Point 37: (582, 411,13)
- Point 38: (596, 427,2)
- Point 39: (608, 443,14)
- Point 40: (620, 459,5)
- Point 41: (633, 476,10)
- Point 42: (645, 490,5)
- Point 43: (656, 503,11)
- Point 44: (666, 515,5)
- Point 45: (675, 527,11)
- Point 46: (684, 538,5)
- Point 47: (694, 551,11)
- Point 48: (702, 565,5)
- Point 49: (710, 577,11)
- Point 50: (716, 588,5)
- Point 51: (723, 598,11)
- Point 52: (728, 606,5)
- Point 53: (733, 615,11)
- Point 54: (738, 622,5)
- Point 55: (743, 631,11)
- Point 56: (747, 637,5)
- Point 57: (750, 644,11)
- Point 58: (753, 652,5)
- Point 59: (756, 659,10)
- Point 60: (759, 666,5)
- Point 61: (761, 673,11)
- Point 62: (764, 680,5)
- Point 63: (766, 687,11)
- Point 64: (768, 694,5)
- Point 65: (769, 701,10)
- Point 66: (771, 708,5)
- Point 67: (772, 714,11)
- Point 68: (773, 722,5)
- Point 69: (774, 729,10)
- Point 70: (777, 743,16)
- Point 71: (778, 755,15)
- Point 72: (778, 764,16)
- Point 73: (780, 775,16)
- Point 74: (781, 784,16)
- Point 75: (781, 785,15)
- Point 76: (781, 789,2)
- Point 77: (781, 790,13)
- Point 78: (781, 792,2)
- Point 79: (782, 796,14)
- Point 80: (782, 796,2)
- Point 81: (782, 797,14)
- Point 82: (782, 798,15)
- Point 83: (782, 800,311)
- Point 84: (784, 800,16)
- Point 85: (784, 800,5)
- Point 86: (785, 800,10)
- Point 87: (786, 800,5)
- Point 88: (786, 800,11)
- Point 89: (788, 800,6)
- Point 90: (789, 800,9)
- Point 91: (790, 800,5)
- Point 92: (791, 800,10)
- Point 93: (793, 800,16)
- Point 94: (795, 800,16)
- Point 95: (796, 800,15)
- Point 96: (797, 800,15)
- Point 97: (797, 800,2)
- Point 98: (798, 800,15)
- Point 99: (798, 800,30)
- Point 100: (799, 800,15)
- Point 101: (799, 800,15)
- Handle destroyed successfully
-
- Process finished with exit code 0
-
- '''
5.云盘源码下载
云盘目录介绍:
demo - 包含各种编程语言的demo
dll - 分别是x86和x64平台所需要的dll/lib/h文件
windows 鼠标轨迹测试工具 - exe测试鼠标轨迹效果( demo 中的 c++ 工程编译后的exe可执行文件)
四.效果演示
1.开始坐标为(100,100),结束坐标为(800,800),通过调用接口获得 4 条鼠标轨迹
2.开始坐标为(1000,100),结束坐标为(800,800),通过调用接口获得 2 条鼠标轨迹
五.常见问题
1.是否支持多线程
支持
2.如何使用多线程
参考前面的《2.鼠标轨迹API调用流程图》,多线程和单线程类似;如果是多线程,那么每个线程都需要通过apiSNCreateHandle创建HANDLE句柄,这样才能多个线程互不影响
六.更新日志
- 2024.09.28 新增易语言demo
- 2024.09.21 修复部分水平/垂直轨迹出现负数的情况
- 2024.09.19 优化部分轨迹延迟时间为0的情况(可能会造成鼠标瞬移)
- 2024.08.20 优化部分轨迹可能出现负数的问题
- 2024.07.15 优化水平/垂直轨迹
- 2024.06.25 新增错误日志信息
- 2024.06.06 python 模拟人工鼠标轨迹demo
- 2024.02.06 c++ 模拟人工鼠标轨迹demo