How can I programmatically control mouse motion and clicks with MATLAB?

494 views (last 30 days)
I am creating a demo using MATLAB and I would like to programmatically move and click the mouse to demonstrate my application.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 6 May 2015
The ability to control mouse pointer position, motion and clicks is not available in MATLAB. To work around this issue, you can use the Java class java.awt.Robot which has this ability. Please note that this approach is not documented and hence MathWorks does not guarantee that the approach will be successful.
Below is a simple example that illustrates how you can move the mouse with MATLAB code using Java functionality. Note that we are unable to provide support for Java classes directly as they are not products of MathWorks. For further assistance regarding the Java classes and methods, refer to the following Java documentation page:
The following MATLAB code example demonstrates how one can programmatically control mouse motion using the java.awt.Robot class to move the mouse diagonally across the screen. First, import the class into MATLAB, create an object of this type, and then execute the mouseMove method in a loop to simulate motion.
import java.awt.Robot;
mouse = Robot;
mouse.mouseMove(0, 0);
screenSize = get(0, 'screensize');
for i = 1: screenSize(4)
mouse.mouseMove(i, i);
The following example demonstrates how one can programmatically click the right mouse button to bring up the context menu. Again, import the required Java classes, create an object of this type, and then use the mousePress and mouseRelease functions to simulate a click. Before executing this code, place the mouse over a portion of the screen where a context menu can appear.
import java.awt.Robot;
import java.awt.event.*;
mouse = Robot;
Tyler on 5 Dec 2018
Edited: Tyler on 5 Dec 2018
One interesting note - this works fine for me with a normal mouse configuration. If I change the mapping of mouse to pointer location (such as with Sakasa Mouse), the mouse.move command with java sends my mouse to a different location depending on where my mouse starts from, even if my command doesn't change. I found Chris Pyne's answer below got around this.

Sign in to comment.

More Answers (4)

Steve McClure
Steve McClure on 12 Oct 2018
Edited: Steve McClure on 12 Oct 2018
As of R2018a, users can programmatically interact with their MATLAB Apps via the App Testing Framework:
This Developer Zone blog post also provides a tour of the functionality:
The intended use for this framework is for testing (less so demonstrating) your apps, although it may suit your needs.

Chris Pyne
Chris Pyne on 10 May 2018
I am not sure about mouse clicks... but you can set the cursor position using the following code.
hroot=groot; % get the root handle
set(hroot,'PointerLocation',[400,600]); % default units are pixels so the domain is your screen resolution.

Amirhosein Ghenaati
Amirhosein Ghenaati on 5 Dec 2014
Edited: Amirhosein Ghenaati on 5 Dec 2014
i used following code to simulate mouse left click action unfortunately the action command (press mouse) executes for ever and my status will be busy forever and i have to restart windows to work with matlab properly how can i fix this bug i want to simulate simple left click
import java.awt.Robot;
import java.awt.event.*;
mouse = Robot;
mouse.mouseMove(100, 100);
mouse.mousePress(InputEvent.BUTTON2_MASK); //left click press
mouse.mouseRelease(InputEvent.BUTTON2_MASK); //left click release
Guy Reading
Guy Reading on 29 Jul 2019
Very late reply, but for those reading this now: I copied Amirhosein's code and got wierd results (I wanted to select a program but ended up opening a new instance of the program instead). After a quick search I believe BUTTON2_MASK is the equivalent of clicking the middle (scroll) button. For left click you'd need:
mouse.mouseMove(810, 1060); % relates to my personal location of open program instance
mouse.mousePress(InputEvent.BUTTON1_MASK); % actual left click press
mouse.mouseRelease(InputEvent.BUTTON1_MASK); % actual left click release

Sign in to comment.

Chuck on 30 Apr 2016
Edited: Chuck on 30 Apr 2016
Unfortunately, this is one area that MATLAB is behind other alternatives, such as Python. If you know how to code in Python, you can easily do it by using the following command:
import win32api, win32con
def click(x,y):
  1 Comment
Dmitry Kostyuk
Dmitry Kostyuk on 29 Apr 2019
I searched the method for the mouse left click detection and I find this great way to solve my issue using Windows API.
I need to get the x and y coordinates of mouse left click of the area, that isn't a figure or current MATLAB window. By this time, I can get x and y of pointer using groot. But I still didn't find an apropriate way to detect the mouse left click. I think that WM_LBUTTONDOWN message of Windows API can do it. But by this moment unfortunatly I didn't find how to program the WinAPI commands into MATLAB code.
Can you explain me how to do it well?

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!