Here is the answer for Windows (not sure if alt + printScr works on linux: P)
I guess one way to achieve this
1. using the Robot class to run the alt + printScreen command (this captures the active window on the clipboard)
2. read the clipboard
Here are two pieces of code that do this. I actually did not try, but something that I put together.
Code to Fire Commands for the Active Clipboard Window
import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; public class ActiveWindowScreenShot { public static void main(String[] args) { Robot robot; try { robot = new Robot(); } catch (AWTException e) { throw new IllegalArgumentException("No robot"); }
Code to read image on clipboard
// If an image is on the system clipboard, this method returns it; // otherwise it returns null. public static Image getClipboard() { Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); try { if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) { Image text = (Image)t.getTransferData(DataFlavor.imageFlavor); return text; } } catch (UnsupportedFlavorException e) { } catch (IOException e) { } return null; }
You can control the control as you need! Let me know if this works for you. but it is definitely on my todo to try it!
source share