Java tutorial
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class Main extends JFrame implements KeyListener, ActionListener { JTextField typingArea; public static void main(String[] args) { Main frame = new Main(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addComponentsToPane(); frame.pack(); frame.setVisible(true); } private void addComponentsToPane() { JButton button = new JButton("Clear"); button.addActionListener(this); typingArea = new JTextField(20); typingArea.addKeyListener(this); getContentPane().add(typingArea, BorderLayout.PAGE_START); getContentPane().add(button, BorderLayout.PAGE_END); } public void keyTyped(KeyEvent e) { displayInfo(e, "KEY TYPED: "); } public void keyPressed(KeyEvent e) { displayInfo(e, "KEY PRESSED: "); } public void keyReleased(KeyEvent e) { displayInfo(e, "KEY RELEASED: "); } public void actionPerformed(ActionEvent e) { typingArea.setText(""); typingArea.requestFocusInWindow(); } private void displayInfo(KeyEvent e, String keyStatus) { int id = e.getID(); String keyString; int modifiersEx = e.getModifiersEx(); String modString = "extended modifiers = " + modifiersEx; String tmpString = KeyEvent.getModifiersExText(modifiersEx); if (tmpString.length() > 0) { modString += " (" + tmpString + ")"; } else { modString += " (no extended modifiers)"; } String actionString = "action key? "; if (e.isActionKey()) { actionString += "YES"; } else { actionString += "NO"; } System.out.println(keyStatus + "\n" + modString + "\n" + actionString + "\n"); } }