问题
How do I code it when I want to perform something. Let's say that the focus on a JTextField and a messagebox will pop up when the user pressed ctrl+alt+backspace at the same time.
回答1:
An example with key bindings:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class JTextFieldMagic {
public static final String CTRL_ALT_BACK_SPACE = "ctrlAltBackspace";
public static void main(String[] args) {
JTextField field = new JTextField(10);
int condition = JComponent.WHEN_FOCUSED;
InputMap inputmap = field.getInputMap(condition);
ActionMap actionMap = field.getActionMap();
KeyStroke ctrlAltBackSpaceKeyStroke = KeyStroke.getKeyStroke(
KeyEvent.VK_BACK_SPACE,
KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK);
inputmap.put(ctrlAltBackSpaceKeyStroke, CTRL_ALT_BACK_SPACE);
actionMap.put(CTRL_ALT_BACK_SPACE, new CtrlAltBackspaceAction());
JOptionPane.showMessageDialog(null, field);
}
}
class CtrlAltBackspaceAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(JTextFieldMagic.CTRL_ALT_BACK_SPACE);
}
}
回答2:
Alternativly, if you after a "application global key listener", you could use either the KeyboardFocusManager.addKeyEventDispatcher or Toolkit.addAWTEventListener.
KeyboardFocusManager.addKeyEventDispatcher
public class GloablKeyListener {
public static void main(String[] args) {
new GloablKeyListener();
}
public GloablKeyListener() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class TestPane extends JPanel {
private JLabel label;
public TestPane() {
setLayout(new BorderLayout());
label = new JLabel("Nothing happening here");
label.setHorizontalAlignment(JLabel.CENTER);
add(label);
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && e.isControlDown() && e.isAltDown()) {
label.setText("Hit me");
} else {
label.setText("Nothing to see here...");
}
return false;
}
});
}
}
}
Toolkit.addAWTEventListener
public class GloablKeyListener {
public static void main(String[] args) {
new GloablKeyListener();
}
public GloablKeyListener() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class TestPane extends JPanel {
private JLabel label;
public TestPane() {
setLayout(new BorderLayout());
label = new JLabel("Nothing happening here");
label.setHorizontalAlignment(JLabel.CENTER);
add(label);
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
if (event instanceof KeyEvent) {
KeyEvent ke = (KeyEvent) event;
if (ke.getID() == KeyEvent.KEY_TYPED) {
if (ke.getKeyCode() == KeyEvent.VK_BACK_SPACE && ke.isControlDown() && ke.isAltDown()) {
label.setText("Hit me");
} else {
label.setText("Nothing to see here...");
}
}
}
}
}, AWTEvent.KEY_EVENT_MASK);
}
}
}
Personally, I prefer the KeyboardFocusManager.addKeyEventDispatcher
. It's simpler and easier to use.
You could set up you own singlton manager where you could assign KeyStroke
s against Action
s, much like the key bindings.
来源:https://stackoverflow.com/questions/12760311/performing-a-certain-task-after-pressing-ctrl-alt-backspace