当我按下键盘上的特定按钮时,我正在编写一个程序来捕获屏幕截图。我遇到的问题是,如果程序在后台运行,程序将不会拍摄我的屏幕截图。屏幕截图仅捕获我的程序是否位于前台。
我使用机器人类捕获并保存屏幕截图。我的节目如下......
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);
}
}
我想知道是否有一个java类或其他方式来获取用户输入而不让程序在前台运行。
谢谢