/ Hacks

Keep your mouse moving on Windows

Ever wanted to solve the million dollar problem to keep the mouse moving in a Windows system. (more on this later)

Here is a solution that simply works! I just modified this gist a little for Python 3 and remove user inputs and added a bit of random clicking to make it more humane!

import sys
import time
import win32api
import win32con

# if (len(sys.argv) < 4):
# 	print ("Usage: python mousemove.py dx dy speed")
# 	sys.exit()

current = win32api.GetCursorPos()
cx = sx = current[0]
cy = sy = current[1]

# mx = int(sys.argv[1])
# my = int(sys.argv[2])
# vx = vy = int(sys.argv[3])

mx = 100
my = 100
vx = vy = 20

print ("Moving", mx, my, "with", vx, "pixels per second")
print ("Press 'q' to quit")

last = time.time()

while(True):
	if win32api.GetAsyncKeyState(ord('Q')):
		sys.exit()
		
	current = time.time()
	tick = current - last
	last = current
	
	if mx > 0:
		cx += vx * tick;
		if cx > mx + sx or cx < sx:
			vx = -vx;
			cx = max( sx, min( mx + sx, cx ) )
	if( my > 0 ):
		cy += vy * tick;
		if cy > my + sy or cy < sy:
			vy = -vy;
			cy = max( sy, min( my + sy, cy ) )
	
	win32api.SetCursorPos((int(cx),int(cy)))

	# Code to simulate mouse click
	x,y = win32api.GetCursorPos()
	win32api.SetCursorPos((x, y))
	#Left click
	win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0) 
	time.sleep(0.05) 
	win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0) 
	#Right click
# 	win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,x,y,0,0) 
# 	time.sleep(0.05) 
# 	win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,x,y,0,0)

	time.sleep(1)