Coordinates of active window on the desktop with java - java

Is there any way to get the title and coordinates of the active window on the desktop? Window can belong to any process, not necessarily to the one that I'm developing. I'd like to do it on java, probably with the calls of some native API.

Someone correct me if i am wrong but you wont be able to use a native API with java if it isnt part of java already, meaning you wont be able to find a pure Java solution.
You can use JNI(Java Native Interface) to create a 'link' using C/C++ and that would allow you to use native API from Java.
EDIT: JNA(Java Native Access) is another possible solution i believe.
You could also use C# or VB.net which i believe that already have support for windows API.
The functions you are looking for might be
GetForegroundWindow to get the window itself.
GetWindowRect to get coordinates.
( note that these links are for C++, C#/VB might have access to those in some way )
For you to use Java you will have to ,as i said, somehow 'link' the native API with java because Java alone can't.
This question about getting window title might have answers that explain Java's limits for this type of things better.
Get current active window's title in Java

Related

Getting HWND for ShutdownBlockReasonCreate / Destroy from an Eclipse SWT Application Window

I want to be able to call windows api shutdownBlockReasonCreate from my Java code. I know it's a complicated process involving JNI call to a custom c++ code that generates a dll. But I was able to get there eventually. However I have a problem with obtaining a HWND from my shell window (I use SWT) as it's one of the key parameters to shutdownBlockReasonCreate and shutdownBlockReasonDestroy functions. Without it I couldn't get these functions to work.
A particular implementation online https://github.com/seraphy/JavaGracefulShutdownForWin7 seems to provide the capabilities required and a demo in how this can be done. Unfortunately this was done with JFrame (using JAWT.lib) whereas my application window was written in Eclipse SWT / JFace. I'm not sure if Eclipse has anything equivalent.
I tried many different approaches such as using FindWindow function and ::FindWindowEx, as well as passing in Shell.handle to the native methods. None of them worked for me as neither of them returns meaningful HWND value but '0'.
Since I'm not using JFrame, I cannot get HWND from JFrame like "seraphy" did. And certainly there's no way we can convert SWT Shell to a JFrame so I can reuse some of its code.
I also don't think JNA would help much in here either given my Google research over the last couple of days. I can see it was able to obtain a handle to a console window (How to return HWND in JAVA) but are we able to call shutdownBlockReasonCreate methods directly from JNA? Nothing online I found so far seemed to suggest that. In which case we still have to use JNI instead.
I'm very new to c++ so I'm a little bit struggled in getting this to work. Anyone who can shed some light or point me to the right direction would be very much appreciated!

Get Image from an Application

Sorry, this question is pretty broad, but I haven't a clue how to go about it.
In Java, how can I get whatever a Windows application is displaying in the form of an Image?
I'd like the contents of the Image to include just a frame from one application window, not a whole monitor.
I think getting the application window from the title of the window may be a start, but does Java even have access to the GUIs of other processes?
The best you can do in pure Java is to use the Robot class to capture a specified rectangle. The following Q&A show how ... for the case where the rectangle is the entire screen.
Screenshot capture in Window 8 OS
(This should work for all non-headless Java SE platforms.)
That's the easy bit. The hard bit is figuring out what rectangle to screenshot. The Java libraries provide no APIs for finding information about the screen locations of "other" applications. To find that kind of information, your Java application would need to interact with the native OS via a native library or an external utility command. This will inevitably be platform specific ... meaning that it will break for Mac and Linux, and possibly for other versions of Windows as well.
I wouldn't try to do this in Java at all. Instead, I would look for an OS-specific screenshot application that can be run as an external application; e.g. using Process.exec. Alternatively, I'd figure out how to write such an app, in (say) C#.
I think getting the application window from the title of the window may be a start, but does Java even have access to the GUIs of other processes?
Nope and nope.

View Java GUI inside .NET Form / Visual Studio editor?

My goal is to write a Visual Studio plugin (a VSPackage) for my Java application. I was wondering if it was possible to view some JPanels inside a System.Windows.Forms instance, or rather as an Microsoft.VisualStudio.Editor.
I was thinking an applet but I'm pretty much stuck there...
Is streaming a Swing component as JPEG and displaying it in a Form an applicable idea?
EDIT:
I would really appreciate answers that are more then a "yes"/"no"/"why would you do this?". I made my mind about working this way, so I ask for:
A detailed solution for achieving my goal, OR,
Good insights/ideas of what my approach should be, OR,
A thorough explanation for why it is impossible to achieve.
( ) Way of the warrior
Load the JVM from your extension (use jvm.dll).
Implement your own Applet Container. Something like this AppletViewer.
Put the Applet Container inside your native form. This is hard, there are ways to get the Applet Container HWND (In Java Swing how do you get a Win32 window handle (hwnd) reference to a window?), but I am not sure a simple SetParent can do the trick.
Load your Applet.
( ) Way of the master
Use NPAPI/NPRuntime, like the browsers do.
Load the npjp2.dll Java Plug-In.
Load your applet.
(X) The solution adopted was:
Run the VS Plugin part written in Java (like an Applet) with JVM in another process (the standard for Java).
Get the HWND using FindWindow (Win32 API).
Use SetParent (Win32 API) and MoveWindow (Win32 API), to dock the window and resize it.
I could name it the "the easiest way", but running Java in another process will give you more stability, so this is the "way that works". My only fear about using a window from another process, was the thread that process Windows messages in Java could interfere in Visual Studio. But from what I see, there is nothing to fear.
:-)

Capture key stroke from separate process in Java

I want this to happen:
Start my java program
My java program launches notepad.exe
If I type a letter in notepad my java program should react and print out the letter
In short: Is it possible to make a key event listener for another process? The java program won't have focus.
If it's impossible I want to be able to change focus between my Java swing window and the notepad process. For example:
A) My java program has focus (small swing window), I type the letter "A"
B) Notepad is given focus quickly and the letter A is typed there (for example using the Robot class)
C) My java program gets focus again
Quoting the answer given in Creating a keyboard hook for a Java program that sits in the System Tray
so , creating a keyboard hook isn't as easy as it looks , java doesn't provide a direct way to interact with the system events for purposes of security ; as it might lead to serious problems concerning the JVM , system's memory and the portability of Java Framework..
you have 4 ways to make global keyboard hooks :
Learn JNI / JNA, and I prefer JNA since its much easier than JNI , in both cases you shall deal with .dll files.
Use JIntellitype , which - as you said - issues some problems.
the elegant solution by Sergei Biletnikov here http://biletnikov-dev.blogspot.com/2009/09/global-hotkeys-for-java-applications_25.html
ignore Java , and try Autoit V3 ( I'm not 100% sure about it , but I think you could send signals/events from Autoit to your Java app , so Autoit would just work as a bridge that catches the global key strokes)
Going with number 3, which is a good tutorial. If the link has 404'd maybe the project source is still up: gigapeta.com/dl/1917618aba749a
With your presented solution #1:
Learn JNI / JNA, and I prefer JNA since its much easier than JNI , in both cases you shall deal with .dll files.
You should take a look at this stackoverflow thread:
JNA Keyboard Hook in Windows
There is copy/pastable code that demonstrates a JNA key hook. You will need to download the JNA dependencies from http://jna.java.net/ for the example to work. You won't even need to fiddle with silly DLL's.
Additionally, you might want to give this library a try. While I have no experience with it, it popped up on my google search for "java keyboard hook." It seems to accomplish the goal of intercepting the keystrokes:
http://code.google.com/p/jnativehook/
Note that you would need some additional native code to see what the current "active window" is. If the active window matches "Notepad.exe" then you should record a native key event.
Good luck!

How to implement file icon overlay in java, the way, e.g., turtoisesvn or dropbox do

I am looking for the best (fast and easy to implement, not necessary most robust) way to overlay file icons in Windows 7 Explorer from a prototype written in java. I have seen on SO questions on the icon overlaying in Net and Python, none for java.
I'm affraid this cannot be entirely done in java. You must create and register a COM object that implements the IExtractIcon interface among other things, and associate it with the files you want to manage. more info here
Look into JNA (https://github.com/twall/jna) - it will allow you to call native code - eg. from user32.dll, etc. - without the burden than JNI would put on you. We're using JNA to get other windows' titles for instance. If you find the information about making icon overlays from native code, you should be able to translate it into JNA quite easly.

Categories

Resources