获取Java的当前活动窗口标题(Get current active window's ti

2019-07-04 02:02发布

我尝试写日志我使用的是什么应用程序每5秒(这是一个时间跟踪应用程序)的Java程序。 我需要一些方法来找出当前活动窗口是什么。 我发现KeyboardFocusManager.getGlobalActiveWindow(),但我不能让它的工作权利。 一个跨平台的解决方案是可取的,但如果不存在,那么我正在开发与X.Org Linux操作系统。 谢谢。

Answer 1:

我很肯定的是,你会发现没有办法一一列举在纯Java的活动窗口(我已经看了相当困难之前),所以你需要为你想要的目标平台的代码。

  • 在Mac OS X,你可以启动的AppleScript使用“osascript”。

  • 在X11上,你可以使用xwininfo 。

  • 在Windows中,你也许可以推出一些VBScript中(例如, 这个链接看起来很有希望)。

如果您使用SWT,您可以找到在SWT库的一些无证,非公开的方式,因为SWT包装提供了很多的OS API的(如可可SWT有org.eclipse.swt.internal.cocoa.OS#objc_msgSend()可用于访问OS方法)。 在Windows和X11相当于“OS”类可能的API就可以使用。



Answer 2:

我已经写了使用user361601的脚本的Java程序。 我希望这会帮助别人。

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class WindowAndProcessInfo4Linux {

public static final String WIN_ID_CMD = "xprop -root | grep " + "\"_NET_ACTIVE_WINDOW(WINDOW)\"" + "|cut -d ' ' -f 5";
public static final String WIN_INFO_CMD_PREFIX = "xwininfo -id ";
public static final String WIN_INFO_CMD_MID = " |awk \'BEGIN {FS=\"\\\"\"}/xwininfo: Window id/{print $2}\' | sed \'s/-[^-]*$//g\'";

public String execShellCmd(String cmd){
    try {  

        Runtime runtime = Runtime.getRuntime();  
        Process process = runtime.exec(new String[] { "/bin/bash", "-c", cmd });  
        int exitValue = process.waitFor();  
        System.out.println("exit value: " + exitValue);  
        BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));  
        String line = "";  
        String output = "";
        while ((line = buf.readLine()) != null) {
            output = line;
        }
        return output;
    } catch (Exception e) {  
        System.out.println(e);
        return null;
    }  
}

public String windowInfoCmd(String winId){
    if(null!=winId && !"".equalsIgnoreCase(winId)){
        return WIN_INFO_CMD_PREFIX+winId +WIN_INFO_CMD_MID;
    }
    return null;
}

public static void main (String [] args){
    WindowAndProcessInfo4Linux windowAndProcessInfo4Linux = new WindowAndProcessInfo4Linux();
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String winId = windowAndProcessInfo4Linux.execShellCmd(WIN_ID_CMD);
    String winInfoMcd = windowAndProcessInfo4Linux.windowInfoCmd(winId);
    String windowTitle = windowAndProcessInfo4Linux.execShellCmd(winInfoMcd);
    System.out.println("window title is: "+ windowTitle);

}
}

// Thread.sleep代码是存在的,这样你得到的时间切换到:)也,你可以使用石英从春天到安排它的其他窗口。



Answer 3:

要查找活动窗口(不论是一帧或一个对话框),你可以使用下面的递归方法Java Swing应用程序:

Window getSelectedWindow(Window[] windows) {
    Window result = null;
    for (int i = 0; i < windows.length; i++) {
        Window window = windows[i];
        if (window.isActive()) {
            result = window;
        } else {
            Window[] ownedWindows = window.getOwnedWindows();
            if (ownedWindows != null) {
                result = getSelectedWindow(ownedWindows);
            }
        }
    }
    return result;
}

这是从这里的窗口状态更多的线索在这里 。



Answer 4:

我已经写了注销当前活动窗口bash脚本: http://www.whitelamp.com/public/active-window-logger.html它使用wmctrl的补丁版本,但提供了一个可供选择的细节(慢)方法使用xprop和xwininfo。

到wmctrl贴剂源代码和脚本的链接可以在上面找到。



Answer 5:

我创造了这个AppleScript的同时寻找到类似的话题 - 这一个获取特定窗口大小

 global theSBounds tell application "System Events" set this_info to {} set theSBounds to {} repeat with theProcess in processes if not background only of theProcess then tell theProcess set processName to name set theWindows to windows end tell set windowsCount to count of theWindows if processName contains "xxxxxxxx" then set this_info to this_info & processName else if processName is not "Finder" then if windowsCount is greater than 0 then repeat with theWindow in theWindows tell theProcess tell theWindow if (value of attribute "AXTitle") contains "Genymotion for personal use -" then -- set this_info to this_info & (value of attribute "AXTitle") set the props to get the properties of the theWindow set theSBounds to {size, position} of props set this_info to this_info & theSBounds end if end tell end tell end repeat end if end if end if end repeat end tell return theSBounds 



Answer 6:

使用SWT内部,我能够把这个一起,似乎很好地工作:

    /** @return The currently active window's title */
    public static final String getActiveWindowText() {
        long /*int*/ handle = OS.GetForegroundWindow();
        int length = OS.GetWindowTextLength(handle);
        if(length == 0) return "";
        /* Use the character encoding for the default locale */
        TCHAR buffer = new TCHAR(0, length + 1);
        OS.GetWindowText(handle, buffer, length + 1);
        return buffer.toString(0, length);
    }
public static final void main(String[] args) {
    try {
        Thread.sleep(1000L);
    } catch(InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    System.out.println(getActiveWindowText());
}

打印: user interface - Get current active window's title in Java - Stack Overflow - Google Chrome



文章来源: Get current active window's title in Java