从我的Java应用程序中,我尝试将重点放在Windows中的另一个打开的应用程序上,然后发送一些KeyEvent。 一切正常,直到第二个窗口打开(由于我发送的KeyEvents并且是正常的预期行为)在上述应用程序中。当第二个窗口打开时,我的代码中的下一个KeyEvents不再执行。有什么帮助吗?
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
public class RobotANB {
public static void main(String[] args) throws AWTException, IOException {
Runtime.getRuntime().exec("[path to EXE]");
Robot robot = new Robot();
robot.delay(20);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.delay(20);
robot.keyPress(KeyEvent.VK_N);
robot.delay(20);
robot.keyRelease(KeyEvent.VK_N);
robot.delay(20);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(20);
robot.keyPress(KeyEvent.VK_ALT);
robot.delay(20);
robot.keyPress(KeyEvent.VK_D);
robot.delay(20);
robot.keyRelease(KeyEvent.VK_D);
robot.delay(20);
robot.keyRelease(KeyEvent.VK_ALT);
robot.delay(20);
// S
robot.keyPress(KeyEvent.VK_S);
robot.delay(20);
robot.keyRelease(KeyEvent.VK_S);
robot.delay(20);
// Second window in other application opens here
// Code below no longer executed and should be
// executed in the second window
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(20);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(20);
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(20);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(20);
}
}
答案 0 :(得分:0)
添加1000的延迟解决了问题
...
// S
robot.keyPress(KeyEvent.VK_S);
robot.delay(20);
robot.keyRelease(KeyEvent.VK_S);
robot.delay(1000);
// Second window in other application opens here
// Code below no longer executed and should be
// executed in the second window
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(20);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(20);
...