Studyon Minte9.com
Java SE




Key Listener




package testexample; import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class Test extends JFrame { public static void main(String[] args) { JFrame frame = new Test(); frame.setBounds(200, 200, 300, 200); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); } public Test() { JTextField inputText = new JTextField(20); inputText.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_F1) { // --- Look Here --- // JOptionPane.showMessageDialog(null, "F1 pressed"); } // Alerts: "F1 pressed" if (e.isControlDown()) { // Ctrl pressed switch(e.getKeyCode()) { case KeyEvent.VK_0: JOptionPane.showMessageDialog(null, "Ctrl + 0 pressed"); // Alerts: "Ctrl + 0 pressed" break; case KeyEvent.VK_1: JOptionPane.showMessageDialog(null, "Ctrl + 1 pressed"); // Alerts: "Ctrl + 1 pressed" break; default: } } } }); JPanel panel = new JPanel(); panel.add(inputText); setLayout(new FlowLayout()); add(panel); } }
 


http://www.kodejava.org/examples/224.html
http://docs.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html
http://stackoverflow.com/questions/5970765/java-detect-ctrlx-key-combination-on-a-jtree