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.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
I created this applescript while looking into similar topic - this one gets the specific window size
I have written a java program using user361601's script. I hope this will help others.
// the thread.sleep is there so that you get time to switch to other window :) also, you may use quartz from spring to schedule it.
I've written a bash script that logs the current active window: http://www.whitelamp.com/public/active-window-logger.html It uses a patched version of wmctrl but provides details of an alternative (slower) method using xprop and xwininfo.
The links to the wmctrl patch & source code and the script can be found above.
Using SWT internals, I was able to put this together, and it seems to work nicely:
Prints:
user interface - Get current active window's title in Java - Stack Overflow - Google Chrome
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.
On Mac OS X, you can launch an AppleScript using "osascript".
On X11, you can use xwininfo.
On Windows, you can probably launch some VBScript (e.g. this link looks promising).
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.To find the active Window(be it a frame or a dialog) in a java swing application you can use the following recursive method:
this is from here More clues on Window state here.