找出Java中关注的应用程序(窗口) [英] Find out what application (window) is in focus in Java

查看:93
本文介绍了找出Java中关注的应用程序(窗口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何编写一个知道哪个Windows应用程序是焦点的Java程序。我可以打开很多窗口,但我想知道正在使用的那个窗口(就像我正在键入的那样,就像谷歌Chrome一样)。

I'd like to know how I can code a Java program that knows which Windows application is in focus. I can have many windows open but I want to know the one that's being used (like Google Chrome right now as I'm typing this).

我不需要在窗口或应用程序中更改任何内容,只需要知道它的名称。

I don't need to change anything in the window or application, just need to know its name.

推荐答案

我担心没有java api。 JVM对它不管理的窗口一无所知。您可能必须使用JNI并调用此函数

I'm afraid there's no java api for that. JVM does not know anything about the windows it does not manage. You'll probably have to use JNI and call this function

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

MSDN链接

PS。这是一个 GetWindowText 函数,如果你需要获取窗口的标题,你可能想要使用它。

PS. THere is a GetWindowText function that you might want to use if you need to grab the title of the window.

这篇文章包含可能对您有所帮助的JNI示例。

This post has JNI examples that might be helpful for you.

这篇关于找出Java中关注的应用程序(窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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