Reputation: 1173
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;
}
}
}
Upvotes: 0
Views: 72