Hi,

I've created a small application that allows the user to capture the screenshot and save the image at a user defined location. It also allows the user to capture the screenshot of the active window as well.

The issue is with the active window. I have learnt that Java does not provide a method to get the screenshot of the active window. So, I googled and found that developers have been using the Alt+PrntScrn to get the screenshot and get the image from the clipboard.

The code for this is:

                  robot.keyPress(KeyEvent.VK_ALT);
                  robot.keyPress(KeyEvent.VK_PRINTSCREEN);
                  robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
                  robot.keyRelease(KeyEvent.VK_ALT);    
                  Thread.sleep(1500);
                  RenderedImage image = (RenderedImage)getClipboard();
                  saveToImage(image,targetLocation);

Now what happens is that when the Alt key is pressed by the robot class, the focus goes on the toolbar of the application of which the user wants to get the screenshot and the screenshot is taken of the toolbar and not that of the window that the user has focus on.

Please advise on how to solve this problem.

Dani AI

Generated

— pressing Alt programmatically is exactly why the toolbar gets focus: Alt activates the window/menu system so the toolbar becomes the “active” area before the screenshot. Rather than simulating Alt+PrintScreen, get the OS handle of the foreground window and capture that window’s rectangle (or ask Windows to render the window off‑screen). That avoids changing UI focus and is far more reliable.

// requires jna and jna-platform on the classpath
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;

HWND hwnd = User32.INSTANCE.GetForegroundWindow();
RECT r = new RECT();
User32.INSTANCE.GetWindowRect(hwnd, r);
Rectangle rect = new Rectangle(r.left, r.top, r.right - r.left, r.bottom - r.top);
BufferedImage img = new Robot().createScreenCapture(rect);
ImageIO.write(img, "png", new File("active-window.png"));

Notes and caveats:

  • The Robot+GetWindowRect approach captures exactly what is visible on the screen. If the target window is minimized or covered by other windows it will capture what the user actually sees.
  • To capture a window even when it’s off‑screen/obscured, use the native PrintWindow technique (via JNA/JNI) to render the window into an HBITMAP and convert that to a BufferedImage — however PrintWindow can fail for layered/accelerated windows. See Microsoft’s PrintWindow docs for details.
  • On multi‑monitor or DPI‑scaled systems native coordinates may not match Java’s logical coordinates; either make the JVM DPI‑aware or scale coordinates accordingly (see SetProcessDpiAwareness / SetProcessDPIAware).
  • Because the app runs from the system tray (as mentioned by ), ensure the tray UI doesn’t steal focus: capture the foreground HWND first, then hide/restore your UI if needed, or capture directly without sending input events.

For an off‑screen/robust capture example and discussion of edge cases, consult the JNA project and Microsoft Win32 PrintWindow docs: JNA GitHub, PrintWindow (MSDN). — the snippet above is a minimal, runnable example to study and adapt.

Can u send me the entire code format so that i can learn this.is it execute in command prompt

The application runs from the System Tray.

What else code do you need
??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.