使用しているアプリケーションを 5 秒ごとに記録する Java プログラムを作成しようとしています (これはタイム トラッカー アプリです)。現在アクティブなウィンドウが何であるかを調べる方法が必要です。KeyboardFocusManager.getGlobalActiveWindow() を見つけましたが、正しく動作させることができません。クロス プラットフォーム ソリューションが望ましいですが、存在しない場合は、X.Org を使用して Linux 向けに開発しています。ありがとう。
6 に答える
純粋な 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 がある場合があります。
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 があるので、他のウィンドウに切り替える時間があります :) また、春のクォーツを使用してスケジュールすることもできます。
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;
}
現在アクティブなウィンドウをログに記録する bash スクリプトを作成しました: http://www.whitelamp.com/public/active-window-logger.html パッチを適用したバージョンの wmctrl を使用しますが、別の (遅い) メソッドの詳細を提供します。 xprop と xwininfo を使用します。
wmctrl パッチとソース コード、およびスクリプトへのリンクは上記にあります。