Created
May 10, 2018 08:10
-
-
Save Domiii/914842a3ff055e782cd38e7139a98c5a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package mouseclicks; | |
| import java.awt.Robot; | |
| import java.awt.event.InputEvent; | |
| import java.awt.event.KeyEvent; | |
| public class RobotMouseTest { | |
| static Robot bot; | |
| public static void main(String[] args) throws Exception { | |
| Thread.sleep(50); | |
| bot = new Robot(); | |
| funWithClicks(); | |
| glide(1000, 0, 50, 500, 500); | |
| } | |
| static void funWithClicks() throws Exception { | |
| int n = 2; | |
| for (int i = 0; i < n; ++i) { | |
| Thread.sleep(200); | |
| move(163, 80); | |
| doubleClick(); | |
| Thread.sleep(5); | |
| key(); | |
| Thread.sleep(100); | |
| move(263, 90); | |
| Thread.sleep(80); | |
| click(); | |
| //System.out.println(i); | |
| } | |
| } | |
| static void glide(int speed, int x1, int y1, int x2, int y2) throws Exception { | |
| int fps = 60; | |
| int stepDelay = 1000/fps; | |
| float xDist = x2-x1; | |
| float yDist = y2-y1; | |
| float dist = (float)Math.sqrt(xDist * xDist + yDist * yDist); | |
| float step = speed / fps; | |
| float xNorm = xDist/dist; | |
| float yNorm = yDist/dist; | |
| float xDelta = xNorm * step; | |
| float yDelta = yNorm * step; | |
| int nSteps = Math.round(dist/step); | |
| for (int i = 1; i <= nSteps; ++i) { | |
| int x = x1 + (int)Math.round(i * xDelta); | |
| int y = y1 + (int)Math.round(i * yDelta); | |
| move(x, y); | |
| Thread.sleep(stepDelay); | |
| } | |
| } | |
| static void key() throws Exception { | |
| bot.keyPress(KeyEvent.VK_META); | |
| Thread.sleep(5); | |
| bot.keyPress(KeyEvent.VK_W); | |
| Thread.sleep(5); | |
| // receiving application should receive a "key down" event here | |
| bot.keyRelease(KeyEvent.VK_W); | |
| Thread.sleep(5); | |
| bot.keyRelease(KeyEvent.VK_META); | |
| } | |
| static void move(int x, int y) throws Exception { | |
| bot.mouseMove(x, y); | |
| } | |
| public static void doubleClick() throws Exception{ | |
| click(); | |
| Thread.sleep(80); // need to wait 5-450 ms (tested on Mac) | |
| click(); | |
| } | |
| public static void click() throws Exception{ | |
| bot.mousePress(InputEvent.BUTTON1_MASK); | |
| Thread.sleep(10); | |
| bot.mouseRelease(InputEvent.BUTTON1_MASK); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment