我的目标是让 printscreen 键触发我的应用程序关闭(理想情况下,在它可以实际复制到剪贴板之前,但并不重要)我有其他键绑定工作 - 但我不知道如何触发 printscreen。在下面的代码中,如果我用“VK_M”(或其他键)替换“VK_PRINTSCREEN”,字母 m 将关闭应用程序 - 具有我想要的功能。关于如何正确调用 printscreen 的任何建议?或有关如何实现此目的的任何其他想法?
private void bindKeys() {
JRootPane rp = getRootPane();
int c = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = rp.getInputMap(c);
ActionMap actionMap = rp.getActionMap();
inputMap.put(KeyStroke.getKeyStroke("UP"), "UP");
actionMap.put("UP", upAction);
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "DOWN");
actionMap.put("DOWN", downAction);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_PRINTSCREEN,0),"PRINT");
actionMap.put("PRINT", rightAction);
}
private AbstractAction upAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println("UP");
}
};
private AbstractAction downAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println("DOWN");
}
};
private AbstractAction rightAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println("PRINT");
System.exit(0);
}
};