Python模拟鼠标轨迹

一.鼠标轨迹模拟简介

传统的鼠标轨迹模拟依赖于简单的数学模型,如直线或曲线路径。然而,这种方法难以捕捉到人类操作的复杂性和多样性。AI大模型的出现,能够通过深度学习技术,学习并模拟更自然的鼠标移动行为。

二.鼠标轨迹算法实现

AI大模型通过学习大量的人类鼠标操作数据,能够识别和模拟出自然且具有个体差异的鼠标轨迹。以下是实现这一技术的关键步骤:

  1. 数据收集:收集不同玩家在各种游戏环境中的鼠标操作数据,包括移动速度、停顿、加速度等。
  2. 模型训练:利用深度学习算法,训练AI模型识别鼠标操作中的模式和规律。
  3. 轨迹生成:在给定起点和终点的情况下,AI模型能够生成符合人类操作习惯的鼠标轨迹。

三.鼠标轨迹API应用场景

游戏中通过分析玩家的鼠标轨迹,检测是否为脚本,例如:大部分游戏都有数据行为检测!

四.API跨语言平台支持

底层实现采用[C/C++](https://winsdk.cn/)语言,利用其高性能和系统级访问能力,开发出高效的鼠标轨迹模拟算法。通过将算法封装为DLL(动态链接库),可以方便地在不同的编程环境中调用,实现跨语言的兼容性。

通过DLL封装,开发者可以在C++、Python、易语言、按键精灵等多种编程语言中使用[鼠标轨迹模拟技术](https://winsdk.cn/)。这种封装方式提供了一种简便的接口,使得不同背景的开发者都能够轻松集成和使用这一技术。

五.鼠标轨迹 API 介绍

1.头文件

  1. #ifndef _SN_SDK_H__
  2. #define _SN_SDK_H__
  3. #include <windows.h>
  4. //返回参数
  5. typedef struct SN_RESULT {
  6. int code; //错误码,如果为 0 表示成功,否则表示错误号
  7. char message[4096]; //错误信息,如果为 "OK" 表示成功,否则返回错误信息
  8. }SN_RESULT;
  9. //坐标参数
  10. typedef struct SN_POINT
  11. {
  12. int x; //屏幕坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
  13. int y; //屏幕坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
  14. }SN_POINT;
  15. //轨迹参数
  16. typedef struct SN_POINT_PARAMS
  17. {
  18. struct SN_POINT point;//屏幕坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
  19. int delayTime; //延时时间(单位:毫秒),仅供参考
  20. }SN_POINT_PARAMS;
  21. /*创建句柄
  22. *
  23. * 参数:
  24. * [in] szKey: 卡密(购买卡密:https://shop.4yuns.com/links/7C9F16B7)
  25. * [in] pOnnxFilePath:设置 onnx 模型文件路径,如果设置为 NULL,默认和 DLL文件同级目录
  26. * [out] pResult: 返回错误信息,参数pResult.code(错误码)如果为 0 表示成功,否则表示错误号;
  27. *
  28. * 返回值:成功返回句柄,失败返回NULL
  29. *
  30. */
  31. HANDLE WINAPI apiSNCreateHandle(char* szKey, char* pOnnxFilePath, SN_RESULT* pResult);
  32. /*获取鼠标移动轨迹
  33. *
  34. * 参数:
  35. * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
  36. * [in] startPoint: 开始坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
  37. * [in] endPoint: 结束坐标,左上角(0,0),右下角(1920,1080 - 以实际屏幕为准)
  38. * [out] points: 轨迹数组,如果数组中元素 point 出现(-1,-1),表示鼠标轨迹结束
  39. *
  40. * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
  41. *
  42. */
  43. SN_RESULT WINAPI apiSNMouseMove(HANDLE handle, SN_POINT startPoint, SN_POINT endPoint, SN_POINT_PARAMS* points);
  44. /*获取版本号
  45. *
  46. * 参数:
  47. * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
  48. * [out] szVersion: 版本号
  49. *
  50. * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
  51. *
  52. */
  53. SN_RESULT WINAPI apiSNGetVersion(HANDLE handle, char* szVersion);
  54. /*获取错误信息
  55. *
  56. * 参数:
  57. * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
  58. *
  59. * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
  60. *
  61. */
  62. SN_RESULT WINAPI apiSNGetError(HANDLE handle);
  63. /*释放句柄(内存)
  64. *
  65. * 参数:
  66. * [in] handle: 句柄(通过调用apiSNCreateHandle得到)
  67. *
  68. * 返回值:返回参数SN_RESULT.code(错误码)如果为 0 表示成功,否则表示错误号;
  69. *
  70. */
  71. SN_RESULT WINAPI apiSNDestroyHandle(HANDLE handle);
  72. #endif // !_SN_SDK_H__

2.Python代码

  1. '''
  2. @SDK功能描述:鼠标轨迹
  3. '''
  4. import ctypes
  5. import os
  6. import sys
  7. # 创建句柄
  8. key = "SNKJuSrrrTnQ5UXYr4zr6XEveL7V2jg2X9h5BHGS5Des" # 字符串
  9. key_bytes = key.encode('utf-8') # 将字符串转换为 bytes
  10. #设置模型文件路径
  11. onnx = "d://SNTrack.onnx" # 字符串
  12. onnx_bytes = onnx.encode('utf-8') # 将字符串转换为 bytes
  13. # 假设 DLL 文件名为 SNSDK.dll
  14. sn_sdk = ctypes.WinDLL('d://SNSDK.dll')
  15. # 定义 SN_RESULT 结构体
  16. class SN_RESULT(ctypes.Structure):
  17. _fields_ = [("code", ctypes.c_int),
  18. ("message", ctypes.c_char * 4096)]
  19. # 定义 SN_POINT 结构体
  20. class SN_POINT(ctypes.Structure):
  21. _fields_ = [("x", ctypes.c_int),
  22. ("y", ctypes.c_int)]
  23. # 定义 SN_POINT_PARAMS 结构体
  24. class SN_POINT_PARAMS(ctypes.Structure):
  25. _fields_ = [("point", SN_POINT),
  26. ("delayTime", ctypes.c_int)]
  27. # 定义函数原型
  28. sn_sdk.apiSNCreateHandle.argtypes = [ctypes.POINTER(ctypes.c_char),ctypes.POINTER(ctypes.c_char), ctypes.POINTER(SN_RESULT)]
  29. sn_sdk.apiSNCreateHandle.restype = ctypes.c_void_p
  30. sn_sdk.apiSNGetVersion.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_char)]
  31. sn_sdk.apiSNGetVersion.restype = SN_RESULT
  32. sn_sdk.apiSNMouseMove.argtypes = [ctypes.c_void_p, SN_POINT, SN_POINT, ctypes.POINTER(SN_POINT_PARAMS)]
  33. sn_sdk.apiSNMouseMove.restype = SN_RESULT # 根据实际情况调整
  34. sn_sdk.apiSNDestroyHandle.argtypes = [ctypes.c_void_p]
  35. sn_sdk.apiSNDestroyHandle.restype = SN_RESULT
  36. result = SN_RESULT() # 创建 SN_RESULT 实例
  37. handle = sn_sdk.apiSNCreateHandle(key_bytes, onnx_bytes,ctypes.byref(result))
  38. if result.code != 0:
  39. message = result.message.decode('gbk', errors='replace').strip()
  40. print("Result message:", message)
  41. else:
  42. print("Handle created successfully")
  43. # 获取版本号
  44. version = ctypes.create_string_buffer(4096)
  45. version_result = sn_sdk.apiSNGetVersion(handle, version)
  46. if version_result.code != 0:
  47. message = result.message.decode('gbk', errors='replace').strip()
  48. print("Result message:", message)
  49. else:
  50. message = result.message.decode('gbk', errors='replace').strip()
  51. print("Result message:", version.value.decode())
  52. # 获取轨迹
  53. # 定义开始和结束坐标
  54. start_point = SN_POINT(100, 100)
  55. end_point = SN_POINT(800, 800)
  56. # 假设返回的轨迹点数量
  57. num_points = 4096
  58. # 创建一个数组来接收轨迹点
  59. points_array = (SN_POINT_PARAMS * num_points)()
  60. # 调用 apiSNMouseMove 函数
  61. move_result = sn_sdk.apiSNMouseMove(handle, start_point, end_point, points_array)
  62. # 检查结果
  63. if move_result.code != 0:
  64. message = result.message.decode('gbk', errors='replace').strip()
  65. print("Result message:", message)
  66. else:
  67. # 遍历并打印每个点
  68. for i in range(num_points):
  69. if points_array[i].point.x == -1 and points_array[i].point.y == -1:
  70. break # 轨迹结束
  71. print(f"Point {i}: ({points_array[i].point.x}, {points_array[i].point.y},{points_array[i].delayTime})") # X坐标 ,Y坐标 ,延时时间
  72. # 释放句柄
  73. destroy_result = sn_sdk.apiSNDestroyHandle(handle)
  74. if destroy_result.code != 0:
  75. message = result.message.decode('gbk', errors='replace').strip()
  76. print("Result message:", message)
  77. else:
  78. print("Handle destroyed successfully")
  79. '''
  80. 输出鼠标轨迹如下:
  81. Handle created successfully
  82. Result message: 1.0
  83. Point 0: (100, 100,0)
  84. Point 1: (100, 98,10)
  85. Point 2: (103, 98,15)
  86. Point 3: (111, 98,16)
  87. Point 4: (116, 101,15)
  88. Point 5: (122, 104,2)
  89. Point 6: (129, 107,13)
  90. Point 7: (135, 109,2)
  91. Point 8: (144, 112,14)
  92. Point 9: (155, 117,2)
  93. Point 10: (167, 123,14)
  94. Point 11: (180, 128,2)
  95. Point 12: (193, 134,13)
  96. Point 13: (209, 138,2)
  97. Point 14: (225, 144,13)
  98. Point 15: (238, 149,5)
  99. Point 16: (254, 157,10)
  100. Point 17: (269, 162,5)
  101. Point 18: (282, 168,11)
  102. Point 19: (298, 175,5)
  103. Point 20: (311, 180,10)
  104. Point 21: (326, 185,6)
  105. Point 22: (341, 193,9)
  106. Point 23: (369, 211,15)
  107. Point 24: (396, 231,16)
  108. Point 25: (419, 251,16)
  109. Point 26: (442, 270,16)
  110. Point 27: (461, 285,17)
  111. Point 28: (481, 300,15)
  112. Point 29: (491, 311,15)
  113. Point 30: (502, 319,2)
  114. Point 31: (513, 329,14)
  115. Point 32: (523, 343,2)
  116. Point 33: (535, 355,14)
  117. Point 34: (546, 369,0)
  118. Point 35: (558, 383,15)
  119. Point 36: (570, 397,2)
  120. Point 37: (582, 411,13)
  121. Point 38: (596, 427,2)
  122. Point 39: (608, 443,14)
  123. Point 40: (620, 459,5)
  124. Point 41: (633, 476,10)
  125. Point 42: (645, 490,5)
  126. Point 43: (656, 503,11)
  127. Point 44: (666, 515,5)
  128. Point 45: (675, 527,11)
  129. Point 46: (684, 538,5)
  130. Point 47: (694, 551,11)
  131. Point 48: (702, 565,5)
  132. Point 49: (710, 577,11)
  133. Point 50: (716, 588,5)
  134. Point 51: (723, 598,11)
  135. Point 52: (728, 606,5)
  136. Point 53: (733, 615,11)
  137. Point 54: (738, 622,5)
  138. Point 55: (743, 631,11)
  139. Point 56: (747, 637,5)
  140. Point 57: (750, 644,11)
  141. Point 58: (753, 652,5)
  142. Point 59: (756, 659,10)
  143. Point 60: (759, 666,5)
  144. Point 61: (761, 673,11)
  145. Point 62: (764, 680,5)
  146. Point 63: (766, 687,11)
  147. Point 64: (768, 694,5)
  148. Point 65: (769, 701,10)
  149. Point 66: (771, 708,5)
  150. Point 67: (772, 714,11)
  151. Point 68: (773, 722,5)
  152. Point 69: (774, 729,10)
  153. Point 70: (777, 743,16)
  154. Point 71: (778, 755,15)
  155. Point 72: (778, 764,16)
  156. Point 73: (780, 775,16)
  157. Point 74: (781, 784,16)
  158. Point 75: (781, 785,15)
  159. Point 76: (781, 789,2)
  160. Point 77: (781, 790,13)
  161. Point 78: (781, 792,2)
  162. Point 79: (782, 796,14)
  163. Point 80: (782, 796,2)
  164. Point 81: (782, 797,14)
  165. Point 82: (782, 798,15)
  166. Point 83: (782, 800,311)
  167. Point 84: (784, 800,16)
  168. Point 85: (784, 800,5)
  169. Point 86: (785, 800,10)
  170. Point 87: (786, 800,5)
  171. Point 88: (786, 800,11)
  172. Point 89: (788, 800,6)
  173. Point 90: (789, 800,9)
  174. Point 91: (790, 800,5)
  175. Point 92: (791, 800,10)
  176. Point 93: (793, 800,16)
  177. Point 94: (795, 800,16)
  178. Point 95: (796, 800,15)
  179. Point 96: (797, 800,15)
  180. Point 97: (797, 800,2)
  181. Point 98: (798, 800,15)
  182. Point 99: (798, 800,30)
  183. Point 100: (799, 800,15)
  184. Point 101: (799, 800,15)
  185. Handle destroyed successfully
  186. Process finished with exit code 0
  187. '''

得到轨迹数据后使用 PyAutoGUI 移动鼠标即可

六.鼠标轨迹 Demo 效果演示

  • 1.开始坐标为(100,100),结束坐标为(800,800),通过调用接口获得 4 条鼠标轨迹
  • 2.开始坐标为(1000,100),结束坐标为(800,800),通过调用接口获得 2 条鼠标轨迹

七.鼠标轨迹 Demo 下载

百度云盘下载
夸克云盘下载
123云盘下载
posted @ 2024-09-25 11:38  猿说编程  阅读(149)  评论(0编辑  收藏  举报