0

On my Windows 10 machine, using Java 17, I have two monitors. A 1440p primary monitor (scaled to 125%) and a 1080p secondary monitor. I created a class that records time and mouse location (x,y coordinates) and another class to play back the recorded actions.

On playing back the actions, I noticed that as it got closer to the bottom of the screen, the actions movements from robot.mouseMove(x,y) were less and less accurate compared to the location they should have been moving to. After troubleshooting, I realized this was due to my primary monitor scaling, and that the issue (for my main monitor) is resolved through robot.mouseMove((int) (x/1.25), (int) (y/1.25));

But then the mouseMove() method breaks for the secondary monitor. I could easily hardcode the movement function to check if(x > 2560) { <Do_Things_Without_Scaling> }, or set my primary monitor to 100% scaling and have the coordinates match up.

With the hardcoded solutions acknowledged, is there a known way with the robot class to handle two monitors of different scalings? It seems to be doing fine with coordinates and them being different resolutions, but the scaling differences keeps throwing it off and making any recorded action not true to the coordinate logs.

Lefro
  • 1
  • 1
    Get the sizes and scales of each monitor ahead of time, then you do not need to hard code any values because you know exactly where each screen starts and ends, and can apply appropriate scaling. If you want to completely disable screen scaling and not worry about this issue at all, then you can use the VM arg `-Dsun.java2d.uiScale=1` or see here for more information: https://stackoverflow.com/questions/48622712/jframe-scaling-in-java-9/48623038 Be warned that this solution will prevent the app scaling, so your application may look small on 4k or larger monitors with 150% or larger scaling – sorifiend May 20 '22 at 03:48
  • Helpful answer if you want to get the scale of the screen: https://stackoverflow.com/questions/32586883/windows-scaling – sorifiend May 20 '22 at 03:49
  • Thank you for the scale of the screen question you linked. My googling skills came up short so I'm happy you shared, that is definitely nice to use. – Lefro May 20 '22 at 04:57
  • In response to your first message, right now I am able to get the correct absolute coordinates of the mouse moving without a problem, my only issue is the `robot.mouseMove()` method when being used on a scaled screen, does not follow absolute coordinates from my testing. If I tell `robot.mouseMove(50,1200)` to get near the lower left corner, of a 2560x1440 screen that is scaled, I would INSTEAD end up at coordinates (62, 1440), the original coordinates scaled by 1.25x – Lefro May 20 '22 at 05:03

0 Answers0