Info

This question is closed. Reopen it to edit or answer.

Move mouse between monitors with java.awt.Robot

1 view (last 30 days)
Daniel Gordon
Daniel Gordon on 8 Mar 2019
Closed: Sabin on 19 Dec 2022
Hi,
I'm automating a process making use of the java.awt.Robot class to move the mouse. However, I've run in to an issue with my multi-monitor setup. Consider this code:
mouse = java.awt.Robot();
mouse.mouseMove(0, 0);
When this runs, the mouse cursor moves to the top left of the monitor that it's currently on when the code is run. To get around this, I tried adding the following:
screens = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
mouse = java.awt.Robot(screens(1));
mouse.mouseMove(0, 0);
I expected that following this change the mouse would only move to the origin of whichever screen corresponds to the first element of the screens array. However, this is not the case, and I see the exact same behaviour that the origin is relative to where the mouse is when running the code. Adding the screen input to the constructor seems to have not changed the behaviour as I expected (see documentation below).
I would be greatful for any help from someone familiar with using these java classes in Matlab.
Cheers

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!