最快地将JFrame最小化,然后激活屏幕捕获程序

时间:2014-06-08 09:10:41

标签: java jframe keyevent minimize keyboardfocusmanager

我有一个程序,当用户按下任意键时我需要将其最小化。还有一件事需要通过按任何键来最小化程序,然后一些屏幕捕获软件捕获它(假设他只通过键盘激活)。我做了第一部分。它最小化了每个键(包括Prt Scr),但是当我使用Lightshot时,它首先被Lightshot冻结,当我关闭它时,我的程序被最小化。是否可以比Lighshot(或任何其他软件)变得更活跃地将其最小化?再次,假设这些软件仅通过按键盘激活(因为我可以禁用它的手动激活)。

这是我的测试课。

public class Test extends JFrame {

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Test frame = new Test();
                frame.setVisible(true);
                KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                manager.addKeyEventDispatcher(frame.new MyDispatcher(frame));
                frame.requestFocus();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Test() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(null);

}

class MyDispatcher implements KeyEventDispatcher {
    JFrame fr;

    public MyDispatcher(JFrame f) {
        this.fr = f;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent e) {
        if (e.getID() == KeyEvent.KEY_PRESSED) {
            StringSelection selection = new StringSelection("");
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(selection, selection);
            fr.setState(JFrame.ICONIFIED);
        } else if (e.getID() == KeyEvent.KEY_RELEASED) {
            StringSelection selection = new StringSelection("");
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(selection, selection);
            fr.setState(JFrame.ICONIFIED);
        } else if (e.getID() == KeyEvent.KEY_TYPED) {
            StringSelection selection = new StringSelection("");
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(selection, selection);
            fr.setState(JFrame.ICONIFIED);
        }
        return false;
    }
}
}

0 个答案:

没有答案