0

I have program which I need to minimize when user presses ANY key. One more thing what needs to happen by pressing any key it to minimize program faster then some screen capture software captures it (suppose he activates only by keyboard). I did first part. It minimizes on every key (including Prt Scr), but when I use Lightshot, it first freezes by Lightshot and when I close it my program is then minimized. Is it possible to minimize it faster than Lighshot (or any other software) gets active? Again, suppose that these software activate only by pressing keyboard (because I can disable manual activation of it).

Here it is my test class.

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;
    }
}
}
Aleksandar
  • 1,163
  • 22
  • 41
  • The issue will come down to at what the key strokes are recognized by the programs and when they reach your key handler... – MadProgrammer Jun 08 '14 at 09:26
  • The problem is, Lightshot might be getting the key notification before your program, and because of the potentional delay between the native messaging queue (in Java) getting the event and it been dispatched to the Event Queue within Java and processed, Lightshot may have been able to respond quicker...so in essence, there's not much you can do at this level. You "might" be able to use a JNI/JNA key listener instead, but you'd have to dig around and try – MadProgrammer Jun 08 '14 at 10:32
  • I already tried something unsuccessfully with JNA. Ok, thanks anyway. – Aleksandar Jun 08 '14 at 10:35
  • You might try using something like this to capture your desktop window - [JCropFrame](http://java-articles.info/articles/?p=76). – Gilbert Le Blanc Jun 08 '14 at 14:44
  • And what to do with that? – Aleksandar Jun 08 '14 at 14:54
  • @MadProgrammer I found that Cpp's BlockInput() can disable everything for every application. But I have problem, I don't know Cpp and I am totally unfamiliar with JNA/JNI. Could you help me? How to use this method in my Java code? – Aleksandar Jun 08 '14 at 16:08

0 Answers0