How can I programmatically control mouse motion and clicks with MATLAB?
494 views (last 30 days)
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.
mouse = Robot;
screenSize = get(0, 'screensize');
for i = 1: screenSize(4)
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.
mouse = Robot;
More Answers (4)
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: https://www.mathworks.com/help/matlab/app-testing-framework.html
This Developer Zone blog post also provides a tour of the functionality: https://blogs.mathworks.com/developer/2018/05/11/app-testing-framework/
The intended use for this framework is for testing (less so demonstrating) your apps, although it may suit your needs.
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 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
mouse = Robot;
mouse.mousePress(InputEvent.BUTTON2_MASK); //left click press
mouse.mouseRelease(InputEvent.BUTTON2_MASK); //left click release
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