Question

Having a little bit of trouble with this one. I have a MATLAB .m file, and as part of it I want to automatically move the mouse cursor to a specific part of a figure GUI as soon as the file is run. I have done a bit of searching around, but am having trouble implementing what I've found. One solution is to use Java:

import java.awt.Robot;
mouse = Robot;

mouse.mouseMove(0, 0);
screenSize = get(0, 'screensize');
for i = 1: screenSize(4)
    mouse.mouseMove(i, i);
    pause(0.00001);
end

This moves the cursor to the bottom of the screen. However, I can't seem to figure out the coordinate system here. I tried a number of different combinations of numbers in the "mouse.mouseMove(i, i)" line, but none of them seemed to have any effect. Any time I made a change it just moved the cursor to the top left of the screen, regardless of what I entered. Any suggestions?

I know there are also moveptr and PointerLocation properties; however, I can only find instructions on how to move those as part of a plot, and I am not sure if they would work for what I'm trying to do.

Was it helpful?

Solution

The coordinate system appears to be as follows: x=0, y=0 is the upper left corner of your primary monitor. Positive X moved left to right. Positive Y moves top to bottom. (Pretty typical)

(Incidentally, you should be using screenSizes = get(0, 'MonitorPositions') rather than screensize, as it properly handles multiple monitors.)

The following commands work for me:

%Setup
robot = java.awt.Robot;
screenSizes = get(0, 'MonitorPositions');

%Mouse to upper left of primary monitor
robot.mouseMove(1, 1)

%Mouse to center of primary monitor
robot.mouseMove(mean(screenSizes(1,[1 3])),mean(screenSizes(1,[2 4]))) 

%Mouse to hardcoded point 200 pixels down and 500 pixels to the right
robot.mouseMove(500, 200)

%Slow horizontal drag
for ix = 1:500
    robot.mouseMove(ix, 200);
    pause(0.01)
end

Incidentally, the code you posted appears to work for me, moving the mouse in a diagonal from the top left towards the bottom right. So unfortunately the above code (which works for me) may have the same problems that you were originally having. FWIW, my version information is:

MATLAB Version 7.12.0.635 (R2011a)
Operating System: Microsoft Windows XP Version 5.1 (Build 2600: Service Pack 3)
Java VM Version: Java 1.6.0_31-b05 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode

OTHER TIPS

Could be a pause issue.

Your example code works for me in Matlab R2011b on Windows XP: the pointer jumps to the upper left and then glides down and right. Maybe you did a pause off before calling this? That'll make this loop zip through so fast you won't see the movement, and it'll leave the pointer at the bottom of the screen. Try pause on and rerun it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top