I have a java program that launches a program on start up. This can be any executable. Problem is that the java app is applying several inputs to any running app when it’s focused ( the app performs keyboard input/output)
Someone told me it’s not possible to make such call directly from Java to see which program is focused. So I can’t make a check whether the desired application is focused or not.
like
if(processName == launchedExeName) // Do code
Are there any tools or libraries which I can implement in my java project?
thanks,
Sidar
You won’t be able to do that directly from java without some native calls. Some library may already do that for you, but I’m not aware of any. So you’ll have to write it yourself – also that’s obviously completely OS dependent.
For windows it’s relatively simple (and afaik there’s some JNI wrapper for the win32 api so you don’t have to do that yourself, maybe look around a bit), I actually had to write something similar some time ago, so here’s the basic version (without error checking for simplicity):
We now have the handle to the process and you can do whatever you want with it. If you want the path of the process, this would go something like:
Trivial really (it gets a good bit more complicated though if you want to receive events whenever the foreground changes), just the error handling blows the code up as usual.
As I said I remember reading about some Win32 wrappers in Java, so you may not have to write the JNI/JNA stuff yourself. And no idea about *nix here.