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

200 ビュー (過去 30 日間)
I am creating a demo using MATLAB and I would like to programmatically move and click the mouse to demonstrate my application.

採用された回答

MathWorks Support Team
MathWorks Support Team 2015 年 5 月 6 日
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);
pause(0.00001);
end
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;
mouse.mousePress(InputEvent.BUTTON3_MASK);
mouse.mouseRelease(InputEvent.BUTTON3_MASK);
  2 件のコメント
Steven Lord
Steven Lord 2017 年 6 月 30 日
Looking at the Javadoc for java.awt.Color the getRed, getGreen, and getBlue methods look like they will be useful for you.
robbie = java.awt.Robot;
theColor = getPixelColor(robbie, 378, 645);
javaToRGB = @(javaColor) [ getRed(javaColor), ...
getGreen(javaColor), ...
getBlue(javaColor)];
javaToRGB(theColor)
Writing the function to perform the conversion isn't technically necessary, but if you're going to perform this conversion repeatedly it will save you some typing.
Steven Lord
Steven Lord 2018 年 3 月 6 日
I haven't tried this, but I believe calling mousePress, mouseMove, and mouseRelease should be the equivalent of click and drag. See the documentation linked in the MathWorks Support Team's answer for more information on those methods.

サインインしてコメントする。

その他の回答 (1 件)

Chuck
Chuck 2016 年 4 月 30 日
編集済み: Chuck 2016 年 4 月 30 日
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):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)

カテゴリ

Help Center および File ExchangeCall Java from MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by