11

使用しているアプリケーションを 5 秒ごとに記録する Java プログラムを作成しようとしています (これはタイム トラッカー アプリです)。現在アクティブなウィンドウが何であるかを調べる方法が必要です。KeyboardFocusManager.getGlobalActiveWindow() を見つけましたが、正しく動作させることができません。クロス プラットフォーム ソリューションが望ましいですが、存在しない場合は、X.Org を使用して Linux 向けに開発しています。ありがとう。

4

6 に答える 6

7

純粋な Java でアクティブなウィンドウを列挙する方法がないことに気付くことは間違いありません (私は以前かなり調べました)。そのため、対象とするプラットフォーム用にコーディングする必要があります。

  • Mac OS X では、 「osascript」を使用してAppleScriptを起動できます。

  • X11 では、xwininfoを使用できます。

  • Windows では、おそらくいくつかの VBScript を起動できます (たとえば、このリンクは有望に見えます)。

org.eclipse.swt.internal.cocoa.OS#objc_msgSend()SWT を使用している場合、SWT は多くの OS API のラッパーを提供するため (たとえば、Cocoa 上の SWT には、OS)。Windows と X11 の同等の「OS」クラスには、使用できる API がある場合があります。

于 2010-04-01T05:21:38.013 に答える
4

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 があるので、他のウィンドウに切り替える時間があります :) また、春のクォーツを使用してスケジュールすることもできます。

于 2012-06-05T06:57:21.090 に答える
2

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;
}

これはここからです。 ウィンドウの状態に関するその他の手がかりはここにあります。

于 2009-08-30T16:46:03.173 に答える
2

現在アクティブなウィンドウをログに記録する bash スクリプトを作成しました: http://www.whitelamp.com/public/active-window-logger.html パッチを適用したバージョンの wmctrl を使用しますが、別の (遅い) メソッドの詳細を提供します。 xprop と xwininfo を使用します。

wmctrl パッチとソース コード、およびスクリプトへのリンクは上記にあります。

于 2010-06-08T16:56:00.103 に答える