Ole Ole - 5 months ago 14
Java Question

Why doesnt a java program proceed to do actions when the window is switched?

Im working on an Java program, in which a new external program is opened then the mouse gets moved to a position and then some keyboard actions are executed. I execute all those actions with a Robot but I noticed, that the Actions wont work after the external program gets opened. Do you have a clue how to fix this?

Robot robot = new Robot();
robot.mouseMove(400, 50);
Process process = new ProcessBuilder("PATH/TO/PROGRAM.exe").start();
copy(TEXT Which IS COPIED TO CLIPBOARD);
Thread.sleep(7000);
robot.mousePress(InputEvent.BUTTON1_DOWN);
robot.mouseRelease(InputEvent.BUTTON1_DOWN);


copy() is a method which copies a String into the clipboard

Answer

It seems that Frame Focus is changed once you've executed the PROGRAM.exe,So actions will not effect the program. You need to gain focus to PROGRAM.exe frame before creating any actions on it.

Try the following:

Robot robot = new Robot();
robot.mouseMove(400, 50);
Process process = new ProcessBuilder("PATH/TO/PROGRAM.exe").start();
//Gaining Focus using Alt+Tab Keys
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(10);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_TAB);
// Continuing the rest of program
Thread.sleep(7000);
robot.mousePress(InputEvent.BUTTON1_DOWN);
robot.mouseRelease(InputEvent.BUTTON1_DOWN);
Comments