用Java获取当前活动窗口的标题 [英] Get current active window's title in Java

查看:1259
本文介绍了用Java获取当前活动窗口的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Java程序来记录我每5秒使用一次的应用程序(这是一个时间跟踪器应用程序)。我需要一些方法来找出当前活动窗口是什么。我找到了KeyboardFocusManager.getGlobalActiveWindow(),但我无法让它正常工作。最好是跨平台解决方案,但如果不存在,那么我正在使用X.Org开发Linux。谢谢。

I am trying to write a Java program that logs what application I'm using every 5 seconds (this is a time tracker app). I need some way to find out what the current active window is. I found KeyboardFocusManager.getGlobalActiveWindow() but I can't get it to work right. A cross platform solution is preferable, but if one doesn't exist, then I'm developing for linux with X.Org. Thanks.

推荐答案

我很确定你会发现没有办法用纯Java来枚举活动窗口(我之前看起来很难看,所以你需要为你想要定位的平台编码。

I'm quite certain that you'll find there's no way to enumerate the active windows in pure Java (I've looked pretty hard before), so you'll need to code for the platforms you want to target.

在X11上,您可以使用 xwininfo

On X11, you can use xwininfo.

在Windows上,您可以启动一些VBScript(例如此链接看起来很有希望。)

On Windows, you can probably launch some VBScript (e.g. this link looks promising).

如果您正在使用SWT,您可以在SWT库中找到一些未记录的非公开方法,因为SWT提供包装持有很多OS API(例如Cocoa上的SWT具有 org.eclipse.swt.internal.cocoa.OS#objc_msgSend()可用于访问操作系统的方法。 Windows和X11上的等效OS类可能包含您可以使用的API。

If you're using SWT, you may be able to find some undocumented, non-public methods in the SWT libs, since SWT provides wrappers for a lot of the OS API's (e.g. SWT on Cocoa has the org.eclipse.swt.internal.cocoa.OS#objc_msgSend() methods that can be used to access the OS). The equivalent "OS" classes on Windows and X11 may have API's you can use.

这篇关于用Java获取当前活动窗口的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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