Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, on windows, how to wait until mouse moves?

I'm developing a screen capture tool, this tools aims to help software developers understand how users ended up crashing the application. The idea is to start screen capture once the mouse starts moving and stop it after 5 minutes the mouse didn't move. screen capture works fine through subprocess with ffmpeg, the only remaining problem (except for the application crashes) is to start and stop the screen capture. How can I do this? ideally it would work with condition variable but even a loop which tests if the mouse moved in the last second would do. Is there any chance python supports something like OnMouseMove()?

like image 790
e271p314 Avatar asked Feb 14 '26 23:02

e271p314


1 Answers

A loop + pywin32, like this:

import win32api
from time import sleep

count = 0
savedpos = win32api.GetCursorPos()
while(True):
    if count>20*5: # break after 5sec
        break

    curpos = win32api.GetCursorPos()
    if savedpos != curpos:
        savedpos = curpos
        print "moved to " + str(savedpos)

    sleep(0.05)
    count +=1

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!