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

Reviewed-by: serb, aivanov
  • Loading branch information
rajamah authored and mrserb committed Mar 10, 2022
1 parent ff76620 commit 8aba4de
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

5 comments on commit 8aba4de

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 8aba4de Mar 21, 2022

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 8aba4de Mar 21, 2022

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch GoeLin-backport-8aba4de9 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 8aba4de9 from the openjdk/jdk repository.

The commit being backported was authored by Rajat Mahajan on 10 Mar 2022 and was reviewed by Sergey Bylokhov and Alexey Ivanov.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-8aba4de9:GoeLin-backport-8aba4de9
$ git checkout GoeLin-backport-8aba4de9
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-8aba4de9

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 8aba4de Mar 22, 2022

Choose a reason for hiding this comment

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

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 8aba4de Mar 22, 2022

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch GoeLin-backport-8aba4de9 in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 8aba4de9 from the openjdk/jdk repository.

The commit being backported was authored by Rajat Mahajan on 10 Mar 2022 and was reviewed by Sergey Bylokhov and Alexey Ivanov.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev GoeLin-backport-8aba4de9:GoeLin-backport-8aba4de9
$ git checkout GoeLin-backport-8aba4de9
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev GoeLin-backport-8aba4de9

Please sign in to comment.