è necessario aggiungere abbastanza ritardo per l'applicazione per inizializzare completamente e ottenere messa a fuoco.
Ecco un esempio di lavoro di base ... Andreas_D è corretto che avete bisogno di emulare la chiave di sistema per passare da un programma ... (Alt +Tab su Windows, Cmd +Tab su OS X)
import java.awt.*;
import static java.awt.event.KeyEvent.*;
import java.io.IOException;
public class RobotSample {
//https://stackoverflow.com/questions/4782231
private static Integer[] KEY_CODES = { VK_S, VK_T, VK_A, VK_C, VK_K, VK_O, VK_V, VK_E, VK_R, VK_F, VK_L,VK_O, VK_W, VK_DECIMAL, VK_C, VK_O, VK_M, VK_SLASH, VK_Q, VK_U, VK_E, VK_S, VK_T, VK_I, VK_O, VK_N, VK_S, VK_SLASH, VK_4, VK_7, VK_8, VK_2, VK_2, VK_3, VK_1, VK_ENTER };
public static void main(String[] args) throws IOException {
try {
Robot robot = new Robot();
Runtime runtime = Runtime.getRuntime();
runtime.exec("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
robot.keyPress(VK_ALT);
robot.keyPress(VK_TAB);
robot.keyRelease(VK_ALT);
robot.keyRelease(VK_TAB);
//Chill a sec...
robot.delay(1000);
for(int i = 0; i < KEY_CODES.length; ++i) {
robot.keyPress(KEY_CODES[i]);
robot.keyRelease(KEY_CODES[i]);
robot.delay(80);
}
} catch(AWTException e) {
e.getMessage();
}
}
}
fonte
2014-09-11 10:19:37
Un'opzione alternativa è che la mia applicazione Java non viene mai la messa a fuoco, in modo che l'applicazione delle finestre ha la messa a fuoco e quando si fa clic su un pulsante sulla applicazione Java l'attenzione non si perde mai nella finestra originale. Non ho idea se sia possibile? –
per chi viene a questa domanda via google, su OSX: http://stackoverflow.com/questions/14859733/set-focus-of-any-application-with-java-osx – ericsoco