简体   繁体   English

Java Swing:是否可以将机械手键盘输入发送到特定的JFrame窗口?

[英]Java Swing: Possible to send robot keyboard input to specific JFrame windows?

I am using a third party java library which spawns new JFrame windows. 我正在使用产生新JFrame窗口的第三方Java库。 How do I make java.awt.Robot to send clicks and keyboard inputs to that specific window when it appears? 出现时,如何使java.awt.Robot将点击和键盘输入发送到该特定窗口? I would run it in a separate thread so that other activities could be performed on the main application while robot is sending inputs to the specific JFrame window. 我将在单独的线程中运行它,以便当机器人将输入发送到特定的JFrame窗口时,可以在主应用程序上执行其他活动。

Note, I don't have api access to the process which spawns this JFrame window. 注意,我没有对生成此JFrame窗口的进程的api访问权限。

Currently, when the that JFrame window is minimized or closed, the robot will continue sending inputs to whatever current JFrame window is visible. 当前,当最小化或关闭该JFrame窗口时,机器人将继续向可见的当前JFrame窗口发送输入。

You get all the frames launched by your application: 您将获得应用程序启动的所有框架:

Frame[] frames = JFrame.getFrames();
//find the frame your looking for and call click(frame)

Click on the center of a component 单击组件的中心

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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM