Detect mouse click on Desktop, not on figure

18 次查看(过去 30 天)
I want to detect the left/right mouse click. I know how to do this in a figure. However I need this outside of a figure, on the 'normal' desktop screen.
It guess it should work with Java. To press and release a mouse button in Matlab on the Desktop is simple, but how do I get the information if a mouse button was pressed on the Desktop. I was trying MouseEvent but my Java knowledge is not enough for that.
I have searched a lot on net, but was not successful.
Thanks for help
import java.awt.Robot;
import java.awt.event.*;
import java.awt.event.MouseEvent;
robot = Robot;
% 5s later, move the mouse to point (640,640) where the 'go' button is,
% then click it.
%pause(5);
robot.mouseMove(40, 500);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
for i= 1:1000
pause (0.50)
if MouseEvent.getButton() == MouseEvent.BUTTON1
& Do somehting here if mouse is clicked.
end
end
Maybe this Java code gives some idea how to implement MouseEvent into Matlab:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Main extends JFrame {
public Main() {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextArea textArea = new JTextArea();
textArea.setText("Click Me!");
textArea.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.NOBUTTON) {
textArea.setText("No button clicked...");
} else if (e.getButton() == MouseEvent.BUTTON1) {
textArea.setText("Button 1 clicked...");
} else if (e.getButton() == MouseEvent.BUTTON2) {
textArea.setText("Button 2 clicked...");
} else if (e.getButton() == MouseEvent.BUTTON3) {
textArea.setText("Button 3 clicked...");
}
System.out.println("Number of click: " + e.getClickCount());
System.out.println("Click position (X, Y): " + e.getX() + ", " + e.getY());
}
});
getContentPane().add(textArea);
}
public static void main(String[] args) {
new Main().setVisible(true);
}
}
  1 个评论
Kaveh Vejdani
Kaveh Vejdani 2020-5-20
MouseEvent.getButton() throws an error:
No method 'getButton' with matching signature found for class 'java.awt.event.MouseEvent'.
Please advise. Thanks.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by