Detecting Physical Mouse Movement with Python and Windows 10 Without Cursor Movement
Asked Answered
C

1

7

I have this code which uses the change in mouse cursor position to detect mouse movement:

import win32api
from time import sleep

savedpos = win32api.GetCursorPos()
count = 0
while(True):

    curpos = win32api.GetCursorPos()
    if savedpos != curpos:
        count = count +1
        savedpos = curpos
        print("Mouse Movement # ", count)
    sleep(0.05)

The problem is that when the mouse cursor is in one of the corners of the screen, my Python code may not recognize mouse movement. For example, if the mouse cursor is in the upper left corner of the screen, moving my physical mouse up and/or left will not have any effect on my Python code (it won't print "Mouse Movement #").

Can someone please help me get my Python code to recognize physical mouse movement even when the mouse cursor is unable to move on the screen?

Cooperation answered 15/4, 2018 at 23:27 Comment(2)
Without capturing the mouse (see SetCapture()/ReleaseCapture()) the only way I can think is to set the cursor position to (1,1) when you detect the cursor is at (0,0), so that you can detect mouse movement again.Arnitaarno
That seems like a good workaround, thanks fferri yesterday!Cooperation
S
1

It is impossible to detect mouse movement if it's in corner and you move it into border.

Solifluction answered 21/9, 2023 at 17:47 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Giuditta

© 2022 - 2024 — McMap. All rights reserved.