Is it possible to create multiple robots and have them run in multiple "desktops". I use a mac and it is possible to create multiple desktops (also known as spaces) and have many windows running in each. Is it possible to have multiple java command line tools using the robot class at once each running in a different desktop. If so, how would I go about doing this.
-
What have you tried so far? What was the problem that you encountered when you did that?Andy Turner– Andy Turner2016-06-06 07:08:08 +00:00Commented Jun 6, 2016 at 7:08
-
Since the Robot class has a constructor accepting a GraphicsDevice argument, the answer is "most likely". Try it and see what you get.Durandal– Durandal2016-06-06 07:31:11 +00:00Commented Jun 6, 2016 at 7:31
Add a comment
|
1 Answer
It is possible, in order to do that you'll need to use threads in java (also known as multi threading). Here is the sample program to help you understand. The threads won't run exactly at the same time, but will run simultaneously. To make them run simultaneously, the synchronization of the thread is required, which will make code more complex
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
// Thread is what we are using
class Anything1 extends Thread {
boolean activate = true;
public void run() {
// add here your robot class with actions
while (activate) {
try {
Robot robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Hello1");
}
}}
class Anything2 extends Thread {
boolean activate = true;
public void run() {
// add here your robot class with actions
while (activate) {
try {
Robot robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// for testing
System.out.println("Hello2");
}
}
}
public class App {
public static void main(String[] args) {
// activates the process
Anything1 p = new Anything1();
Anything2 r = new Anything2();
p.start();
r.start();
}
}