我正在使用生成新 JFrame 窗口的第三方 java 库。如何使 java.awt.Robot 在出现时将点击和键盘输入发送到该特定窗口?我会在一个单独的线程中运行它,以便在机器人将输入发送到特定的 JFrame 窗口时,可以在主应用程序上执行其他活动。
请注意,我没有对生成此 JFrame 窗口的进程的 api 访问权限。
目前,当 JFrame 窗口最小化或关闭时,机器人将继续向任何当前可见的 JFrame 窗口发送输入。
您将获得应用程序启动的所有帧:
Frame[] frames = JFrame.getFrames();
//find the frame your looking for and call click(frame)
单击组件的中心
click(Component c){
//get center
Dimension size = c.getSize();
Point center = new Point(size.width/2, size.height/2);
//you might want to check if the component is showing.
Robot.mouseMove(center.getX(), center.getY());
Robot.keyPress(KeyEvent.VK_A);
}