Ok, so I wanted to know wether there is some library that allows you to control what the mouse does (which buttons are being pressed and where it moves). I think I’ve seen something like this in c# or something idk and when googling for such a library I couldn’t find anything. Does anyone know about such a library (or perhaps even an already built-in function in Processing I didn’t know of)?
Simon
1 Like
You’re probably looking for https://docs.oracle.com/javase/8/docs/api/java/awt/Robot.html
1 Like
This has been discussed on the forum many times.
1 Like
Thanks a lot, but how do I get it to work in Processing?
Like any other built-in Java library - you need to import it. For example (make sure you know how to quit using Escape!)
import java.awt.Robot;
Robot robot;
int x,y;
void setup() {
try {
robot = new Robot();
} catch (Exception ex) {
println(ex);
}
}
void draw() {
x++;
x %= 500;
y++;
y %= 500;
robot.mouseMove(x,y);
}
If you’re using P2D / P3D it’s better to use the JOGL library for this though.
1 Like
Thank you! It works just as I wanted
Just one more thing, when I want to press the left mouse button, which parameter do i need to put into the robot.mousePress()
function?
I believe that is BUTTON1 – although I haven’t tested the ways of referring to it.
https://docs.oracle.com/javase/7/docs/api/java/awt/event/InputEvent.html
Note in particular:
int button = InputEvent.BUTTON1_MASK
int button = InputEvent.getMaskForButton(1);
int button = InputEvent.getMaskForButton(MouseEvent.BUTTON1);