mouseClick.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # -*- coding: utf-8 -*-
  2. import pyautogui
  3. import random
  4. import math
  5. import time
  6. import win32api
  7. import win32con
  8. # 计算贝塞尔曲线上的点
  9. def bezier(t, points):
  10. n = len(points) - 1
  11. x = 0
  12. y = 0
  13. for i in range(n + 1):
  14. coefficient = math.comb(n, i) * (1 - t) ** (n - i) * t ** i
  15. x += points[i][0] * coefficient
  16. y += points[i][1] * coefficient
  17. return x, y
  18. def get_durationAndSteps(distance):
  19. local_duration = 0
  20. local_steps = 0
  21. if distance < 500:
  22. local_duration = 150 + distance + random.randint(-100, 100)
  23. local_steps = 30 + local_duration / 10
  24. else:
  25. local_duration = 300 + distance / 2 + random.randint(-200, 200)
  26. local_steps = 60 + local_duration / 15
  27. return local_duration / 1000, int(local_steps)
  28. #生成控制点,需在随机点添加正态分布
  29. def generate_control_points(start_point, end_point, num_points):
  30. control_points = []
  31. control_points.append(start_point)
  32. # 计算起点和终点之间的距离
  33. distance = ((end_point[0] - start_point[0]) ** 2 + (end_point[1] - start_point[1]) ** 2) ** 0.5
  34. # 计算每个等分点的距离
  35. interval = distance / (num_points + 1)
  36. srcPoint = start_point
  37. for i in range(1, num_points + 1):
  38. # 计算当前等分点的位置
  39. t = i / (num_points + 1)
  40. dstPoint = ((end_point[0] - start_point[0]) / num_points * i + start_point[0], (end_point[1] - start_point[1]) / num_points * i + start_point[1])
  41. # 生成随机的控制点
  42. control_point = (
  43. random.uniform(srcPoint[0], dstPoint[0]),
  44. random.uniform(srcPoint[1], dstPoint[1])
  45. )
  46. srcPoint = dstPoint
  47. # 将控制点添加到列表中
  48. control_points.append(control_point)
  49. out_points = (((end_point[0] - start_point[0]) / num_points / 3 + end_point[0], (end_point[1] - start_point[1]) / num_points / 3 + end_point[1]))
  50. control_points.append(out_points)
  51. control_points.append(end_point)
  52. return control_points
  53. class pcacc_mouse:
  54. def __init__(specifically):
  55. random.seed()
  56. @staticmethod
  57. def move_mouse(dst_x, dst_y, isQuick = False):
  58. random_range = 4
  59. # 获取当前鼠标位置作为起点
  60. start_x, start_y = pyautogui.position()
  61. if abs(dst_x - start_x) < 10 and abs(dst_y - start_y) < 10: #认为已经移到对应位置了
  62. return
  63. distance = math.sqrt((dst_x - start_x) ** 2 + (dst_y - start_y) ** 2)
  64. duration, steps = get_durationAndSteps(distance)
  65. points = generate_control_points((start_x, start_y), (dst_x, dst_y), 3)
  66. interval = duration / steps
  67. for i in range(steps + 1):
  68. t = i / steps
  69. x, y = bezier(t, points)
  70. # 添加随机性到坐标
  71. x += random.randint(-random_range, random_range)
  72. y += random.randint(-random_range, random_range)
  73. # 计算速度和加速度
  74. speed_factor = math.sin(t * math.pi)
  75. acceleration_factor = 2 * t * (1 - t)
  76. speed = speed_factor * (1 + acceleration_factor) * interval
  77. if isQuick:
  78. speed = speed / 2
  79. try:
  80. win32api.SetCursorPos((int(x), int(y)))
  81. finally:
  82. time.sleep(speed) # 添加停顿时间
  83. @staticmethod
  84. def click(dst_x, dst_y):
  85. pcacc_mouse.move_mouse(dst_x, dst_y)
  86. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, dst_x, dst_y, 0, 0)
  87. cur_times = random.randint(0, 1)
  88. if cur_times == 0:
  89. time.sleep(1.0 / random.randint(60, 130))
  90. elif cur_times == 1:
  91. time.sleep(1.0 / random.randint(100, 200))
  92. dst_x = dst_x + random.randint(-3, 3)
  93. dst_y = dst_y + random.randint(-3, 3)
  94. win32api.SetCursorPos((dst_x, dst_y))
  95. time.sleep(1.0 / random.randint(100, 200))
  96. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, dst_x + random.randint(-3, 3), dst_y + random.randint(-3, 3), 0, 0)
  97. @staticmethod
  98. def quickclick(dst_x, dst_y):
  99. pcacc_mouse.move_mouse(dst_x, dst_y, True)
  100. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, dst_x, dst_y, 0, 0)
  101. time.sleep(1.0 / random.randint(300, 500))
  102. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, dst_x + random.randint(-3, 3), dst_y + random.randint(-3, 3), 0, 0)
  103. @staticmethod
  104. def drag_from(src_x, src_y, dst_x, dst_y):
  105. pcacc_mouse.move_mouse(src_x, src_y)
  106. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, src_x, src_y, 0, 0)
  107. pcacc_mouse.move_mouse(dst_x, dst_y)
  108. time.sleep(1.0 / random.randint(60, 130))
  109. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, dst_x, dst_y, 0, 0)
  110. @staticmethod
  111. def drag(dst_x, dst_y):
  112. src_x, src_y = pyautogui.position()
  113. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, src_x, src_y, 0, 0)
  114. pcacc_mouse.move_mouse(dst_x, dst_y)
  115. time.sleep(1.0 / random.randint(60, 130))
  116. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, dst_x, dst_y, 0, 0)
  117. if __name__ == '__main__':
  118. # 移动鼠标
  119. pcacc_mouse.drag_from(500, 500, 100, 100)