mouseClick.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 input_string(text):
  58. print("input string : " + str(text))
  59. for char in text:
  60. # 获取字符的虚拟键码(大写字母或数字)
  61. vk_code = win32api.VkKeyScan(char)
  62. # 按下键
  63. win32api.keybd_event(vk_code & 0xff, 0, 0, 0)
  64. time.sleep(0.05) # 短暂延迟,确保系统处理
  65. # 释放键
  66. win32api.keybd_event(vk_code & 0xff, 0, win32con.KEYEVENTF_KEYUP, 0)
  67. time.sleep(0.05)
  68. @staticmethod
  69. def move_mouse(dst_x, dst_y, isQuick = False):
  70. random_range = 4
  71. # 获取当前鼠标位置作为起点
  72. start_x, start_y = pyautogui.position()
  73. if abs(dst_x - start_x) < 10 and abs(dst_y - start_y) < 10: #认为已经移到对应位置了
  74. return
  75. distance = math.sqrt((dst_x - start_x) ** 2 + (dst_y - start_y) ** 2)
  76. duration, steps = get_durationAndSteps(distance)
  77. points = generate_control_points((start_x, start_y), (dst_x, dst_y), 3)
  78. interval = duration / steps
  79. for i in range(steps + 1):
  80. t = i / steps
  81. x, y = bezier(t, points)
  82. # 添加随机性到坐标
  83. x += random.randint(-random_range, random_range)
  84. y += random.randint(-random_range, random_range)
  85. # 计算速度和加速度
  86. speed_factor = math.sin(t * math.pi)
  87. acceleration_factor = 2 * t * (1 - t)
  88. speed = speed_factor * (1 + acceleration_factor) * interval
  89. if isQuick:
  90. speed = speed / 2
  91. try:
  92. win32api.SetCursorPos((int(x), int(y)))
  93. finally:
  94. time.sleep(speed) # 添加停顿时间
  95. @staticmethod
  96. def click(dst_x, dst_y):
  97. pcacc_mouse.move_mouse(dst_x, dst_y)
  98. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, dst_x, dst_y, 0, 0)
  99. cur_times = random.randint(0, 1)
  100. if cur_times == 0:
  101. time.sleep(1.0 / random.randint(60, 130))
  102. elif cur_times == 1:
  103. time.sleep(1.0 / random.randint(100, 200))
  104. dst_x = dst_x + random.randint(-3, 3)
  105. dst_y = dst_y + random.randint(-3, 3)
  106. win32api.SetCursorPos((dst_x, dst_y))
  107. time.sleep(1.0 / random.randint(100, 200))
  108. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, dst_x + random.randint(-3, 3), dst_y + random.randint(-3, 3), 0, 0)
  109. @staticmethod
  110. def quickclick(dst_x, dst_y):
  111. pcacc_mouse.move_mouse(dst_x, dst_y, True)
  112. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, dst_x, dst_y, 0, 0)
  113. time.sleep(1.0 / random.randint(300, 500))
  114. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, dst_x + random.randint(-3, 3), dst_y + random.randint(-3, 3), 0, 0)
  115. @staticmethod
  116. def quickclick_current():
  117. src_x, src_y = pyautogui.position()
  118. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, src_x + random.randint(-3, 3), src_y + random.randint(-3, 3), 0, 0)
  119. time.sleep(1.0 / random.randint(300, 500))
  120. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, src_x + random.randint(-3, 3), src_y + random.randint(-3, 3), 0, 0)
  121. @staticmethod
  122. def drag_from(src_x, src_y, dst_x, dst_y):
  123. pcacc_mouse.move_mouse(src_x, src_y)
  124. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, src_x, src_y, 0, 0)
  125. pcacc_mouse.move_mouse(dst_x, dst_y)
  126. time.sleep(1.0 / random.randint(60, 130))
  127. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, dst_x, dst_y, 0, 0)
  128. @staticmethod
  129. def drag(dst_x, dst_y):
  130. src_x, src_y = pyautogui.position()
  131. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, src_x, src_y, 0, 0)
  132. pcacc_mouse.move_mouse(dst_x, dst_y)
  133. time.sleep(1.0 / random.randint(60, 130))
  134. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, dst_x, dst_y, 0, 0)
  135. if __name__ == '__main__':
  136. # 移动鼠标
  137. pcacc_mouse.drag_from(500, 500, 100, 100)