Created
January 6, 2014 09:35
-
-
Save yoggy/8280330 to your computer and use it in GitHub Desktop.
how to use java.awt.Robot class in processing ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks alot!