Detect mouse click on Desktop, not on figure
조회 수: 3 (최근 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
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 Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!