java.awt.Robot.keyPress()/mousePress() 之后重新获得焦点

发布于 2024-08-19 19:03:50 字数 338 浏览 4 评论 0原文

我编写一个仅使用键盘控制另一个应用程序的应用程序。更具体地说,当按下键盘上的某个键时,应用程序会模拟按键和鼠标单击。例如,按“x”键模拟鼠标单击右上角的 [X],然后短暂休眠 2 秒,然后按“回车”确认退出对话框。很容易。我正在用 Java 开发这个应用程序。

使用 java.awt.Robot 发送按键或鼠标点击非常容易。我面临一个小问题。假设我配置了一个按键,可以单击屏幕上的某个位置。问题是连续的按键不再被捕获,因为我的应用程序由于鼠标在窗口外单击而失去了焦点。

我现在的问题是:确保我的主应用程序保持焦点的最佳方法是什么?有没有办法在按键和鼠标点击发出后再次聚焦我的应用程序?有更好的办法吗?

提前致谢。

I writing an application which controls another application by using the keyboard only. To more concrete, the application simulates key presses and mouse clicks when a certain key is pressed on the keyboard. For example, pressing on the 'x' key simulates a mouse click on the [X] in the rop right corner, followed by a little sleep of 2 seconds and an 'enter' to confirm the exit dialog. Pretty easy. I am developing this application in Java.

Sending a key press or a mouse click is very easy with java.awt.Robot. I am facing one little problem. Say I have configured a key which will click somewhere on the screen. The problem is that consecutive key presses aren't catched anymore, as my application lost its focus caused by the mouse click outside it's window.

My question now is: what is the best way to be sure that my main application keeps the focus? Is there a way to focus my application again after the key presses and mouse clicks are sent out? Is there a better way?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝梦月影 2024-08-26 19:03:50

如果您的应用程序失去了焦点。因为您或您的机器人点击了其他地方,所以机器人必须在发送新密钥之前再次点击该应用程序。在 C/C++ 中,您可以将焦点强制转移到应用程序(一项不平凡的任务),而不是在 Java 中!

If your application lost the focus. because you or your Robot clicked to somwhere else, the Robot must click on the application again before sending a new key. In c/c++ you could force the focus to the application (a non-trivial task), not in Java!

清风挽心 2024-08-26 19:03:50

您可能想看看 Component.requestFocus() 看看是否可以做你想要的。

但请注意,窗口聚焦具有非常依赖于平台的行为,因此您可能需要进行大量测试以确保您的代码在所有情况下都能满足您的要求。

You might want to take a look at Component.requestFocus() to see if can do what you want.

Be aware however that window focusing has very platform dependent behaviour, so you will probably need to do quite a bit of testing to ensure that your code does what you want in all circumstances.

黑白记忆 2024-08-26 19:03:50

我找到了一种方法来防止应用程序失去 Java 的所有焦点。

通过在框架(或对话框)上放置 WindowFocusListener 并调用 setVisible(false),然后在 windowLostFocus 中调用 setVisible(true),组件将重新启动- 一旦消失就出现(不是最漂亮的解决方案,但它确实有效)。

然后调用 component.requestFocus() 你的机器人应该能够从中断的地方继续

I managed a way to prevent applications from losing all focus in Java.

By placing a WindowFocusListener on the frame (or dialog) and calling setVisible(false) followed by setVisible(true) in windowLostFocus the component will re-appear as soon as it is dissapears (not the prettiest solution but it does work).

By then calling component.requestFocus() your robot should be able to continue where it left off

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文