The win32 API's SetCursorPos moves the mouse to a location on the screen. However, sometimes it does nothing.
See the following C++ code (Visual Studio 16.8.1, Windows 10)
#include <iostream>
#include <windows.h>
int main()
{
while (true)
{
std::cout << "Hello World!
";
SetCursorPos(100, 200);
Sleep(2000);
}
}
Every 2 seconds, if SetCursorPos
works then the cursor teleports to (100, 200)
. This works normally.
However if I leave this running and open the built-in Windows app called "On-Screen Keyboard", SetCursorPos
no longer works (the cursor no longer teleports every 2 seconds).
Note: If trying this yourself, On-Screen Keyboard usually unfocuses itself automatically. To keep it focused to observe SetCursorPos
not working, alt+tab
to it, which causes it not to unfocus itself.
There are other third-party applications that, when in foreground, also seem to obstruct SetCursorPos
. (When I observed those, I wasn't using the above C++ script. I was using the Python package mouse
which uses SetCursorPos
under the hood.)
How are these applications able to "swallow" my SetCursorPos
API calls, and is there a different API or technique I can use in place of SetCursorPos
to not allow those applications to interfere?
A physical USB mouse, as well as remote desktop software such as TeamViewer and Anydesk, are not obstructed by such applications, which leads me to believe they utilize some pathway besides SetCursorPos
to get reliable mouse movement.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…