Skip to content

Instantly share code, notes, and snippets.

@bhuiyanmobasshir94
Created March 28, 2024 15:57
Show Gist options
  • Save bhuiyanmobasshir94/664fdbcd9e49a1eaf36e06e7bd7ecf72 to your computer and use it in GitHub Desktop.
Save bhuiyanmobasshir94/664fdbcd9e49a1eaf36e06e7bd7ecf72 to your computer and use it in GitHub Desktop.
import pyautogui
import time
# Define the idle threshold in seconds
idle_threshold = 10 # Adjust as needed
idle_time = 0
# Store the initial mouse position
prev_mouse_position = pyautogui.position()
while True:
# Wait for a short interval
time.sleep(1) # Check every second
# Get the current mouse position
current_mouse_position = pyautogui.position()
# Check if the mouse position has changed
if current_mouse_position != prev_mouse_position:
# Reset the idle timer if the mouse has moved
prev_mouse_position = current_mouse_position
else:
# Increment idle time
idle_time += 1
# If idle time exceeds the threshold, move the mouse
if idle_time >= idle_threshold:
# Move the mouse pointer to a new position
new_x = current_mouse_position[0] + 10 # Example: Move 10 pixels to the right
new_y = current_mouse_position[1] + 10 # Example: Move 10 pixels down
pyautogui.moveTo(new_x, new_y, duration=0.1) # Adjust duration as needed
# Reset idle time
idle_time = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment