I have a problem with SendInput. It works fine in 2D, but it doesn’t work in 3D applications like Paint, etc. In other programs, it fails completely. Can anyone help? When it’s in 2D, it moves the mouse perfectly, but when it’s in 3D, it moves the mouse upwards and spins in circles.
void moveCursorSmoothly(POINT startPos, POINT endPos) {
// Ograniczenie pozycji końcowej do obszaru FOV na początku
int dx = endPos.x - circleCenter.x;
int dy = endPos.y - circleCenter.y;
double distance = sqrt(dx * dx + dy * dy);
if (distance > fovRadius.load()) {
double scale = fovRadius.load() / distance;
endPos.x = circleCenter.x + static_cast<int>(dx * scale);
endPos.y = circleCenter.y + static_cast<int>(dy * scale);
}
if (startPos.x == endPos.x && startPos.y == endPos.y) {
std::cout << "Cursor already in target position.n";
return;
}
std::cout << "Moving cursor smoothly from (" << startPos.x << ", " << startPos.y << ") to (" << endPos.x << ", " << endPos.y << ")n";
POINT controlPoint1 = { startPos.x + (endPos.x - startPos.x) / 3, startPos.y };
POINT controlPoint2 = { startPos.x + 2 * (endPos.x - startPos.x) / 3, endPos.y };
int steps = static_cast<int>(sensitivity.load() * 10);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(-1, 1);
for (int i = 1; i <= steps; ++i) {
double t = static_cast<double>(i) / steps;
POINT newPos = bezierCurve(startPos, controlPoint1, controlPoint2, endPos, t);
newPos.x += dis(gen);
newPos.y += dis(gen);
// Dostosowanie do obszaru FOV podczas ruchu
dx = newPos.x - circleCenter.x;
dy = newPos.y - circleCenter.y;
distance = sqrt(dx * dx + dy * dy);
if (distance > fovRadius.load()) {
double scale = fovRadius.load() / distance;
newPos.x = circleCenter.x + static_cast<int>(dx * scale);
newPos.y = circleCenter.y + static_cast<int>(dy * scale);
}
// Dodatkowe sprawdzanie podczas ruchu
if (sqrt((newPos.x - circleCenter.x) * (newPos.x - circleCenter.x) + (newPos.y - circleCenter.y) * (newPos.y - circleCenter.y)) > fovRadius.load()) {
std::cerr << "Cursor position outside FOV during movement. Adjusting position." << std::endl;
double scale = fovRadius.load() / distance;
newPos.x = circleCenter.x + static_cast<int>(dx * scale);
newPos.y = circleCenter.y + static_cast<int>(dy * scale);
}
INPUT input = { 0 };
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
input.mi.dx = (newPos.x * 65535) / GetSystemMetrics(SM_CXSCREEN);
input.mi.dy = (newPos.y * 65535) / GetSystemMetrics(SM_CYSCREEN);
SendInput(1, &input, sizeof(INPUT));
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
// Ostateczne ustawienie pozycji końcowej, upewniając się, że jest w FOV
dx = endPos.x - circleCenter.x;
dy = endPos.y - circleCenter.y;
distance = sqrt(dx * dx + dy * dy);
if (distance > fovRadius.load()) {
double scale = fovRadius.load() / distance;
endPos.x = circleCenter.x + static_cast<int>(dx * scale);
endPos.y = circleCenter.y + static_cast<int>(dy * scale);
}
INPUT finalInput = { 0 };
finalInput.type = INPUT_MOUSE;
finalInput.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
finalInput.mi.dx = (endPos.x * 65535) / GetSystemMetrics(SM_CXSCREEN);
finalInput.mi.dy = (endPos.y * 65535) / GetSystemMetrics(SM_CYSCREEN);
SendInput(1, &finalInput, sizeof(INPUT));
}
I tried SetCursorPos, but it doesn’t work either.
New contributor
Marcin krakowiak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.