ユーザーが任意のキーを押したときに最小化する必要があるプログラムがあります。もう 1 つ必要なことは、プログラムを最小化するために任意のキーを押して、一部のスクリーン キャプチャ ソフトウェアがそれをキャプチャするよりも速く実行することです (彼がキーボードのみでアクティブ化するとします)。第一部やった。すべてのキー (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;
}
}
}