Skip to content

Commit

Permalink
8249592: Robot.mouseMove moves cursor to incorrect location when disp…
Browse files Browse the repository at this point in the history
…lay scale varies and Java runs in DPI Unaware mode

Backport-of: 8aba4de98477a3bcfcde8db71e0d797965f774c7
  • Loading branch information
GoeLin committed Apr 1, 2022
1 parent 0182066 commit 372e359
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/java.desktop/windows/native/libawt/windows/awt_Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,33 @@
#include "awt_Component.h"
#include <winuser.h>

static int signum(int i) {
// special version of signum which returns 1 when value is 0
return i >= 0 ? 1 : -1;
}

static void MouseMove(jint x, jint y)
{
INPUT mouseInput = {0};
mouseInput.type = INPUT_MOUSE;
mouseInput.mi.time = 0;
mouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
mouseInput.mi.dx = (x * 65536 /::GetSystemMetrics(SM_CXSCREEN)) + signum(x);
mouseInput.mi.dy = (y * 65536 /::GetSystemMetrics(SM_CYSCREEN)) + signum(y);

// The following calculations take into account a multi-monitor setup using
// a virtual screen for all monitors combined.
// More details from Microsoft are here --
// https://docs.microsoft.com/en-us/windows/win32/gdi/the-virtual-screen

x -= ::GetSystemMetrics(SM_XVIRTUALSCREEN);
y -= ::GetSystemMetrics(SM_YVIRTUALSCREEN);

mouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE |
MOUSEEVENTF_VIRTUALDESK;

int scW = ::GetSystemMetrics(SM_CXVIRTUALSCREEN);
int scH = ::GetSystemMetrics(SM_CYVIRTUALSCREEN);

// The following calculation to deduce mouse coordinates is based on
// empirical data
mouseInput.mi.dx = (x * 65536 + scW - 1) / scW;
mouseInput.mi.dy = (y * 65536 + scH - 1) / scH;

::SendInput(1, &mouseInput, sizeof(mouseInput));

}

static void MousePress(jint buttonMask)
Expand Down

1 comment on commit 372e359

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.