下面的代码使用JNA库输出活动窗口的标题。
private static final int MAX_TITLE_LENGTH = 1024;
public static void main(String[] args) throws Exception {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
TimerTask task = new TimerTask() {
@Override
public void run() {
char[] buffer = new char[MAX_TITLE_LENGTH * 2];
HWND hwnd = User32.INSTANCE.GetForegroundWindow();
User32.INSTANCE.GetWindowText(hwnd, buffer, MAX_TITLE_LENGTH);
System.out.println("Active window title: " + Native.toString(buffer));
try {
BufferedWriter bw = new BufferedWriter(
new FileWriter(
new File("C:\\Users\\" + System.getProperty("user.name") + "\\AppData\\Roaming\\system\\useracivity.txt"),
true
)
);
bw.write(Native.toString(buffer) + " TIME WAS " + dtf.format(now));
bw.newLine();
bw.close();
} catch (Exception e) {
}
}
};
Timer timer = new Timer();
timer.schedule(task, new Date(), 5000);
}
但是,我如何获取应用程序的名称,例如Chrome "chrome.exe"?
提前感谢
https://stackoverflow.com/questions/50694754
复制相似问题