Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
how to use java.awt.Robot class in processing ...
//
// how to use java.awt.Robot class in processing ...
//
import java.awt.*;
import java.awt.event.*;
Robot robot;
PFont pfont;
Point save_p;
void setup() {
size(320, 240);
try {
robot = new Robot();
robot.setAutoDelay(0);
}
catch (Exception e) {
e.printStackTrace();
}
pfont = createFont("Impact", 32);
}
void draw() {
background(#ffffff);
fill(#000000);
Point p = getGlobalMouseLocation();
textFont(pfont);
text("now x=" + (int)p.getX() + ", y=" + (int)p.getY(), 10, 32);
if (save_p != null) {
text("save x=" + (int)save_p.getX() + ", y=" + (int)save_p.getY(), 10, 64);
}
}
void keyPressed() {
switch(key) {
case 's':
save_p = getGlobalMouseLocation();
break;
case 'm':
if (save_p != null) {
mouseMove((int)save_p.getX(), (int)save_p.getY());
}
break;
case 'c':
case ' ':
if (save_p != null) {
mouseMoveAndClick((int)save_p.getX(), (int)save_p.getY());
}
break;
}
}
Point getGlobalMouseLocation() {
// java.awt.MouseInfo
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
Point p = pointerInfo.getLocation();
return p;
}
void mouseMove(int x, int y) {
robot.mouseMove(x, y);
}
void mouseMoveAndClick(int x, int y) {
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.waitForIdle();
}
@hwd2002

This comment has been minimized.

Copy link

@hwd2002 hwd2002 commented Apr 15, 2019

Thanks, this is helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment