I know that there are plenty of threads about mousePressed not working right, but I have yet to find an answer to my problem. I made a program, just for fun and experience, but I have run into problems with the mousePressed method of the Robot class. This is my code:
//Program to say "Hi!" by moving cursor
//https://sketch.io/sketchpad/
//Open up sketchpad, run OCursor, then click on the sketchpad
import java.awt.*;
import javax.swing.*;
import java.awt.event.InputEvent;
public class OCursor {
public static void main(String[] args) {
try {
// These are the screen coordinates
int cd1 = 400;
int cd2 = 600;
int cd3 = 700;
int cd4 = 750;
int cd5 = 800;
int cd6 = 1000;
int cd7 = 1100;
int cd8 = 1200;
int cd9 = 1400;
// This is the time and amount of steps
int t = 300, n = 5000;
// 5 second delay to click on sketchpad
Robot robot = new Robot();
robot.delay(5000);
// Move and click the cursor
robot.mouseMove(cd2, cd5);
robot.mousePress(InputEvent.BUTTON1_MASK);
mouseGlide(cd2, cd5, cd2, cd1, t, n);
mouseGlide(cd2, cd2, cd5, cd2, t, n);
mouseGlide(cd5, cd5, cd5, cd1, t, n);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
Robot robot2 = new Robot();
robot2.mouseMove(cd6, cd1);
robot2.mousePress(InputEvent.BUTTON1_MASK);
mouseGlide(cd6, cd1, cd8, cd1, t, n);
mouseGlide(cd7, cd1, cd7, cd5, t, n);
mouseGlide(cd6, cd5, cd8, cd5, t, n);
robot2.mouseRelease(InputEvent.BUTTON1_MASK);
robot2.delay(100);
Robot robot3 = new Robot();
robot3.mouseMove(cd9, cd5);
robot3.mousePress(InputEvent.BUTTON1_MASK);
mouseGlide(cd9, cd5, cd9, cd4, t, n);
robot3.mouseRelease(InputEvent.BUTTON1_MASK);
robot3.delay(100);
robot3.mouseMove(cd9, cd3);
robot3.mousePress(InputEvent.BUTTON1_MASK);
mouseGlide(cd9, cd3, cd9, cd1, t, n);
robot3.mouseRelease(InputEvent.BUTTON1_MASK);
robot3.delay(100);
}
catch (AWTException err) {
err.printStackTrace();
}
}
public static void mouseGlide(int x1, int y1, int x2, int y2, int t, int n) {
// "t" being time and "n" being amount of steps, with more steps being smoother
// mouseGlide code borrowed from http://stackoverflow.com/questions/9387483/how-to-move-a-mouse-smoothly-throughout-the-screen-by-using-java
try {
Robot robot = new Robot();
double dx = (x2 - x1) / ((double) n);
double dy = (y2 - y1) / ((double) n);
double dt = t / ((double) n);
for (int step = 1; step <= n; step++) {
Thread.sleep((int) dt);
robot.mouseMove((int) (x1 + dx * step), (int) (y1 + dy * step));
}
}
catch (AWTException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
The problem I have is mousePress won't actually release properly. It will either not release at all, or release when it is supposed to press. I overcame that by just using mouseMove every time I need it to release, but this shouldn't be necessary. I heard putting delays after each mouseRelease should fix it, but it hasn't done anything. I don't understand why it is not working correctly and why I have to use mouseMove instead of mouseRelease. I also tried using new robots for every letter, but it doesn't make any difference.
If the code is run without mouseMove in between each letter it is drawing, it will not work properly.
Also, I used someone else's code for mouseGlide because this is just me testing the Robot class, as I have never used it.