boppreh / mouse

Hook and simulate global mouse events in pure Python
MIT License
904 stars 136 forks source link

Mouse Movements Jerky in Windows 10 #101

Open rob51852 opened 2 years ago

rob51852 commented 2 years ago

I have recorded some mouse movements. When I replay them on my Windows 8.1 machine they look exactly like they look in realtime. However, when I replay them on a Windows 10 Pro machine they appear jerky.

I have tried recording the movements on Windows 10 and replaying them on that machine but I still get the same issue.

I have no idea why this is happening. Can anyone think of a reason?

Here's the code:

import pyautogui, bezier, random, numpy as np, time, mouse
import mouse, pickle, keyboard

start_position = mouse.get_position()
events = mouse.record()
end_position = mouse.get_position()
print(start_position)
print(events)
print(end_position)
dump_file = open('click_open.pkl','wb')
pickle.dump(start_position, dump_file)
pickle.dump(events, dump_file)
pickle.dump(end_position, dump_file)
dump_file.close()
AlanZookie commented 1 year ago

Replace time.sleep() in mouse/init.py

def play(events, speed_factor=1.0, include_clicks=True, include_moves=True, include_wheel=True): last_time = None for event in events: if speed_factor > 0 and last_time is not None: _time.sleep((event.time - last_time) / speed_factor) last_time = event.time

with the function below

def delayMicrosecond(t): start,end=0,0 start=_time.time() t=(t-3)/1000000 while end-start<t: end=_time.time()

Because the common delay function of python is time.sleep, and the minimum delay precision is 0.001 seconds, that is, time.sleep (0.001) delay is 1 millisecond. Under the Windows 10 operating system, the measured delay of the logic analyzer is 13 millisecond. My CPU is Intel i5 10400F, and the expected delay has changed from 1 millisecond to 13 millisecond.

muhaha211 commented 1 year ago

Replace time.sleep() in mouse/init.py

def play(events, speed_factor=1.0, include_clicks=True, include_moves=True, include_wheel=True): last_time = None for event in events: if speed_factor > 0 and last_time is not None: _time.sleep((event.time - last_time) / speed_factor) last_time = event.time

with the function below

def delayMicrosecond(t): start,end=0,0 start=_time.time() t=(t-3)/1000000 while end-start<t: end=_time.time()

Because the common delay function of python is time.sleep, and the minimum delay precision is 0.001 seconds, that is, time.sleep (0.001) delay is 1 millisecond. Under the Windows 10 operating system, the measured delay of the logic analyzer is 13 millisecond. My CPU is Intel i5 10400F, and the expected delay has changed from 1 millisecond to 13 millisecond.

i replaced _time.sleep in play function to delayMicrosecond function you made but now its too fast that i cant even see it moving.. like every recorded movement finishes in less than 0.1 secs. any idea how to solve?

for event in events:
    if speed_factor > 0 and last_time is not None:
        delayMicrosecond((event.time - last_time) / speed_factor)