Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def move_slow(x, y, t, point_delta = 1):
- x1, y1, x2, y2 = 0, 0, int(x), int(y)
- points = []
- # Check if linear movement is steep
- is_steep = abs(y2 - y1) > abs(x2 - x1)
- # If the linear movement is steep, reverse the values.
- if is_steep:
- x1, y1 = y1, x1
- x2, y2 = y2, x2
- reverse = False
- if x1 > x2:
- x1, x2 = x2, x1
- y1, y2 = y2, y1
- reverse = True
- # Get the delta (differernce) between each axial value
- delta_x = x2 - x1
- delta_y = abs(y2 - y1)
- # Get margin for error
- error = int(delta_x / 2)
- y = y1
- y_step = None
- if y1 < y2:
- y_step = 1
- else:
- y_step = -1
- # Calculate each point from 0,0 to x2, y2
- for x in range(x1, x2 + 1):
- if is_steep:
- points.append((y, x))
- else:
- points.append((x, y))
- error -= delta_y
- if error < 0:
- y += y_step
- error += delta_x
- # Reverse the points if the values were reversed at the start of the function.
- if reverse:
- points.reverse()
- # Add 0,0 to start of the list so no errors occur when using the generated points
- points.insert(0, (0, 0))
- # Convert points to a more compact array generating less movements:
- new_points = points[0::point_delta]
- new_points[-1] = points[-1]
- # Get a ratio for the time value,
- # Allowing us to loop through and move the mouse within that time.
- ratio_t = float(len(new_points)) / float(t)
- idx, count = 1, 0.0
- for i in range(t):
- # Sleep for 1ms each iteration.
- ctypes.windll.kernel32.SleepEx(1, False)
- count += ratio_t
- # Move mouse while our count value is greater than 1, keeping us on track.
- while count >= 1.0:
- if idx == len(new_points): break
- # Get the difference between the last values and current values.
- delta_x = new_points[idx][0] - new_points[idx - 1][0]
- delta_y = new_points[idx][1] - new_points[idx - 1][1]
- idx += 1
- # Move the mouse relative to current position based on the delta.
- Mouse.move_rel(delta_x, delta_y)
- count -= 1.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement