0

I am writing a program to capture a screenshot of the screen when I press a particular button on my keyboard. The problem that I have is that the program won't take my screenshot if its in the background. The screenshot will only capture if my program is in the foreground.

I'm using the robot class to capture and save the screenshot. I have my program below...

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.AWTException;
import java.awt.DefaultKeyboardFocusManager;
import java.awt.KeyEventDispatcher;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;

public class FullScreenCaptureExample{

    private static int num = 1;

    public static void main(String[] args) throws AWTException{

        final File file = new File("C:\\screenshots");
        final Robot rbt = new Robot();
        JFrame frame = new JFrame();

        frame.setVisible(true);
        frame.setBounds(100, 100, 190, 73);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        if(!file.exists()){
            file.mkdir();
        }

        KeyEventDispatcher dispatcher = new KeyEventDispatcher(){       
            public boolean dispatchKeyEvent(KeyEvent e){
                if (e.getKeyCode() == KeyEvent.VK_5){
                    try {
                        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
                        BufferedImage screenshot = rbt.createScreenCapture(screenRect);
                        ImageIO.write(screenshot, "jpg", new File(file + "/" + num + ".jpg"));
                        num++;
                        System.out.println("screenshot taken");
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
                return false;
            }
        };

        DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
    }

}

I'm wondering if there is a java class or some other way to get user input without having the program run in the foreground.

Thank you

  • 2
    Basically, you can't, Java won't respond to global key events. You could try doing some research into JNI/JNA solutions, which would allow you monitor the OS's event event – MadProgrammer Jan 22 '15 at 00:26
  • Take a look at [this example](http://stackoverflow.com/questions/16179923/how-to-capture-global-key-presses-in-java) and see if it helps – MadProgrammer Jan 22 '15 at 00:28
  • For windows programs, I usually use AutoIt scripting since it's just so easy to use. This can then tie in nicely with my Java program if I so desire. – Hovercraft Full Of Eels Jan 22 '15 at 00:29
  • @MadProgrammer thank you for the response, i will take a look into the examples and see if i can implement it to my program – undefinedArray Jan 22 '15 at 00:33

0 Answers0