我有当用户按任意键时需要最小化的程序。还有一件事需要通过按任何键来最小化程序,然后一些屏幕捕获软件捕获它(假设他仅通过键盘激活)。我做了第一部分。它会最小化每个键(包括 Prt Scr),但是当我使用 Lightshot 时,它首先会被 Lightshot 冻结,然后当我关闭它时,我的程序就会被最小化。是否可以比 Lightshot(或任何其他软件)激活它更快地最小化它?同样,假设这些软件只能通过按键盘来激活(因为我可以禁用手动激活它)。
这是我的测试课。
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;
}
}
}