Java Robot Class - 将焦点添加到特定的运行应用程序? [英] Java Robot Class - Add focus to a specific running Application?

查看:864
本文介绍了Java Robot Class - 将焦点添加到特定的运行应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想弄清楚是否/如何让Java Robot类将焦点从正在运行的java应用程序更改为特定进程,例如ms word或firefox。

I am just trying to figure out if/how to get the Java Robot class to change focus from the running java app, to a specific process, such as ms word or firefox.

谢谢!

推荐答案

机器人不能自动执行此操作。您可以通过alt-tab激活另一个应用程序,如上所述,但您需要知道要激活的应用程序的z顺序。我认为要真正做到最好,你需要获得要激活的顶级窗口的窗口句柄(hWnd)(如果这是一个Windows应用程序),然后使用Windows user32库函数激活想要的窗口。为此,我建议使用 JNA 作为最简单的方法之一(与JNI相比)。您必须首先下载JNA jna.jar和platform.jar jar文件,并将它们放在类路径上,然后您可以轻松调用大多数OS方法。例如,我已经为Windows应用程序运行了这样的东西,我可以根据窗口名称(完整或部分)获取运行的顶级Windows应用程序的hWnd,然后使用该hWnd,调用user32的setForegroundWindow函数。如果您想要激活Windows应用程序并希望进一步追求,请回答此问题,我可以向您展示我的代码。如果是这样,你会想要详细了解你正在尝试做什么。

Robot can't do that automatically. You can activate another application via alt-tab as has been suggested above, but you'll need to know the z-order of the application that you want to activate. I think that to really do this best you'll want to get the window's handle (hWnd) of the top-level window that you want to activate (if this is a Windows app), and then using the Windows user32 library functions activate the desired window. For this I recommend using JNA as one of the easiest ways (when compared to JNI). You would have to first download the JNA jna.jar and platform.jar jar files, and place them on your classpath, and then you can call most OS methods with ease. For instance, I have just this sort of thing up and running for a Windows application where I can get the hWnd for a running top-level Windows application based on the window name (full or partial), and then using that hWnd, call user32's setForegroundWindow function. If you're wanting to activate a Windows application and want to pursue this further, comment to this answer, and I can show you what code I have for this. If so, you'll want to go into greater details on exactly what it is you're trying to do.

祝你好运!

这篇关于Java Robot Class - 将焦点添加到特定的运行应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆