List of usage examples for java.awt.event KeyEvent getKeyChar
public char getKeyChar()
From source file:MainClass.java
public static void main(String args[]) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField nameTextField = new JTextField(); frame.add(nameTextField, BorderLayout.NORTH); KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent keyEvent) { printIt("Pressed", keyEvent); }/* w w w. j a va 2 s .c o m*/ public void keyReleased(KeyEvent keyEvent) { printIt("Released", keyEvent); } public void keyTyped(KeyEvent keyEvent) { printIt("Typed", keyEvent); } private void printIt(String title, KeyEvent keyEvent) { int keyCode = keyEvent.getKeyCode(); String keyText = KeyEvent.getKeyText(keyCode); System.out.println(title + " : " + keyText + " / " + keyEvent.getKeyChar()); } }; nameTextField.addKeyListener(keyListener); frame.setSize(250, 100); frame.setVisible(true); }
From source file:KeyTest.java
public static void main(String args[]) { JFrame frame = new JFrame("Key Listener"); Container contentPane = frame.getContentPane(); KeyListener listener = new KeyListener() { public void keyPressed(KeyEvent e) { dumpInfo("Pressed", e); }//w w w.ja va 2 s . co m public void keyReleased(KeyEvent e) { dumpInfo("Released", e); } public void keyTyped(KeyEvent e) { dumpInfo("Typed", e); } private void dumpInfo(String s, KeyEvent e) { System.out.println(s); int code = e.getKeyCode(); System.out.println("\tCode: " + KeyEvent.getKeyText(code)); System.out.println("\tChar: " + e.getKeyChar()); int mods = e.getModifiersEx(); System.out.println("\tMods: " + KeyEvent.getModifiersExText(mods)); System.out.println("\tLocation: " + location(e.getKeyLocation())); System.out.println("\tAction? " + e.isActionKey()); } private String location(int location) { switch (location) { case KeyEvent.KEY_LOCATION_LEFT: return "Left"; case KeyEvent.KEY_LOCATION_RIGHT: return "Right"; case KeyEvent.KEY_LOCATION_NUMPAD: return "NumPad"; case KeyEvent.KEY_LOCATION_STANDARD: return "Standard"; case KeyEvent.KEY_LOCATION_UNKNOWN: default: return "Unknown"; } } }; JTextField text = new JTextField(); text.addKeyListener(listener); contentPane.add(text, BorderLayout.NORTH); frame.pack(); frame.show(); }
From source file:JTextFieldSample.java
public static void main(String args[]) { String title = (args.length == 0 ? "TextField Listener Example" : args[0]); JFrame frame = new JFrame(title); Container content = frame.getContentPane(); JPanel namePanel = new JPanel(new BorderLayout()); JLabel nameLabel = new JLabel("Name: "); nameLabel.setDisplayedMnemonic(KeyEvent.VK_N); JTextField nameTextField = new JTextField(); nameLabel.setLabelFor(nameTextField); namePanel.add(nameLabel, BorderLayout.WEST); namePanel.add(nameTextField, BorderLayout.CENTER); content.add(namePanel, BorderLayout.NORTH); JPanel cityPanel = new JPanel(new BorderLayout()); JLabel cityLabel = new JLabel("City: "); cityLabel.setDisplayedMnemonic(KeyEvent.VK_C); JTextField cityTextField = new JTextField(); cityLabel.setLabelFor(cityTextField); cityPanel.add(cityLabel, BorderLayout.WEST); cityPanel.add(cityTextField, BorderLayout.CENTER); content.add(cityPanel, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Command: " + actionEvent.getActionCommand()); }//from w w w . j a va 2s . com }; nameTextField.setActionCommand("Yo"); nameTextField.addActionListener(actionListener); cityTextField.addActionListener(actionListener); KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent keyEvent) { printIt("Pressed", keyEvent); } public void keyReleased(KeyEvent keyEvent) { printIt("Released", keyEvent); } public void keyTyped(KeyEvent keyEvent) { printIt("Typed", keyEvent); } private void printIt(String title, KeyEvent keyEvent) { int keyCode = keyEvent.getKeyCode(); String keyText = KeyEvent.getKeyText(keyCode); System.out.println(title + " : " + keyText + " / " + keyEvent.getKeyChar()); } }; nameTextField.addKeyListener(keyListener); cityTextField.addKeyListener(keyListener); InputVerifier verifier = new InputVerifier() { public boolean verify(JComponent input) { final JTextComponent source = (JTextComponent) input; String text = source.getText(); if ((text.length() != 0) && !(text.equals("Exit"))) { Runnable runnable = new Runnable() { public void run() { JOptionPane.showMessageDialog(source, "Can't leave.", "Error Dialog", JOptionPane.ERROR_MESSAGE); } }; SwingUtilities.invokeLater(runnable); return false; } else { return true; } } }; nameTextField.setInputVerifier(verifier); cityTextField.setInputVerifier(verifier); DocumentListener documentListener = new DocumentListener() { public void changedUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } public void insertUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } public void removeUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } private void printIt(DocumentEvent documentEvent) { DocumentEvent.EventType type = documentEvent.getType(); String typeString = null; if (type.equals(DocumentEvent.EventType.CHANGE)) { typeString = "Change"; } else if (type.equals(DocumentEvent.EventType.INSERT)) { typeString = "Insert"; } else if (type.equals(DocumentEvent.EventType.REMOVE)) { typeString = "Remove"; } System.out.print("Type : " + typeString + " / "); Document source = documentEvent.getDocument(); int length = source.getLength(); try { System.out.println("Contents: " + source.getText(0, length)); } catch (BadLocationException badLocationException) { System.out.println("Contents: Unknown"); } } }; nameTextField.getDocument().addDocumentListener(documentListener); cityTextField.getDocument().addDocumentListener(documentListener); frame.setSize(250, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame aWindow = new JFrame(); aWindow.setBounds(50, 100, 300, 300); aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField typingArea = new JTextField(20); typingArea.addKeyListener(new KeyListener() { /** Handle the key typed event from the text field. */ public void keyTyped(KeyEvent e) { displayInfo(e, "KEY TYPED: "); }//ww w . ja v a2 s.c o m /** Handle the key pressed event from the text field. */ public void keyPressed(KeyEvent e) { displayInfo(e, "KEY PRESSED: "); } /** Handle the key released event from the text field. */ public void keyReleased(KeyEvent e) { displayInfo(e, "KEY RELEASED: "); } protected void displayInfo(KeyEvent e, String s) { String keyString, modString, tmpString, actionString, locationString; int id = e.getID(); if (id == KeyEvent.KEY_TYPED) { char c = e.getKeyChar(); keyString = "key character = '" + c + "'"; } else { int keyCode = e.getKeyCode(); keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")"; } int modifiers = e.getModifiersEx(); modString = "modifiers = " + modifiers; tmpString = KeyEvent.getModifiersExText(modifiers); if (tmpString.length() > 0) { modString += " (" + tmpString + ")"; } else { modString += " (no modifiers)"; } actionString = "action key? "; if (e.isActionKey()) { actionString += "YES"; } else { actionString += "NO"; } locationString = "key location: "; int location = e.getKeyLocation(); if (location == KeyEvent.KEY_LOCATION_STANDARD) { locationString += "standard"; } else if (location == KeyEvent.KEY_LOCATION_LEFT) { locationString += "left"; } else if (location == KeyEvent.KEY_LOCATION_RIGHT) { locationString += "right"; } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) { locationString += "numpad"; } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN) locationString += "unknown"; } System.out.println(keyString); System.out.println(modString); System.out.println(actionString); System.out.println(locationString); } }); aWindow.add(typingArea); aWindow.setVisible(true); }
From source file:Main.java
/** * Set the text component in which only numerics (positive decimal integers * with character 0-9).//from w w w . ja v a2s. co m * * @param c * the specified text component */ public static void setTextComponetIntegerValid(final JTextComponent c) { c.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { if (e.getKeyChar() != KeyEvent.VK_BACK_SPACE) try { Integer.valueOf(c.getText() + e.getKeyChar()); } catch (NumberFormatException nfe) { e.consume(); Toolkit.getDefaultToolkit().beep(); } } }); }
From source file:Main.java
/** * Key to lowercase String, extracts the effective key that was pressed * (without shift, control, alt)// w ww. ja v a 2s. c o m */ public static String getKeyText(KeyEvent e) { // special cases if (e.getKeyCode() == KeyEvent.VK_DELETE) { return "delete"; } // prio 1: get text of unresolved code (shift-1 --> '1') String s = "" + e.getKeyChar(); if (e.getKeyCode() > 0) { int flags = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL; for (Field f : KeyEvent.class.getFields()) { if ((f.getModifiers() & flags) == flags) { try { if (f.getName().startsWith("VK_") && ((Integer) f.get(null)) == e.getKeyCode()) { s = f.getName().substring(3).toLowerCase(); break; } } catch (Throwable t) { // nop } } } } if (s.length() != 1) { // prio 2: check if the resolved char is valid (shift-1 --> '+') if (e.getKeyChar() >= 32 && e.getKeyChar() < 128) { s = "" + e.getKeyChar(); } } return s.toLowerCase(); }
From source file:com.SCI.centraltoko.utility.UtilityTools.java
public static void setAutoUpperCase(final JTextField textField) { textField.addKeyListener(new KeyAdapter() { @Override// ww w. j a v a2 s . c o m public void keyReleased(KeyEvent e) { if (Character.isLetter(e.getKeyChar())) { convertToUpperCase((JTextField) e.getSource()); } } }); }
From source file:com.SCI.centraltoko.utility.UtilityTools.java
public static void setAutoUpperCase(final JTextArea textArea) { textArea.addKeyListener(new KeyAdapter() { @Override/*from ww w .jav a 2s . c om*/ public void keyReleased(KeyEvent e) { if (Character.isLetter(e.getKeyChar())) { convertToUpperCase((JTextArea) e.getSource()); } } }); }
From source file:com.SCI.centraltoko.utility.UtilityTools.java
public static void setNumericTextOnly(final JTextField textField) { textField.addKeyListener(new KeyAdapter() { @Override/* ww w. ja v a 2 s . com*/ public void keyTyped(KeyEvent e) { if (!Character.isDigit(e.getKeyChar()) || BAD_CHARS.indexOf(e.getKeyChar()) > -1) { e.consume(); } } }); }
From source file:org.openconcerto.erp.core.common.ui.DeviseField.java
public static void addFilteringKeyListener(final JTextField textField) { textField.addKeyListener(new KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent keyEvent) { final char keychar = keyEvent.getKeyChar(); if (keychar == KeyEvent.VK_BACK_SPACE) { return; }/*from w ww . j av a 2 s.c o m*/ // pas plus de 2 chiffres apres la virgule int pointPosition = textField.getText().indexOf('.'); if (Character.isDigit(keychar)) { if (pointPosition > -1) { // System.err.println("Text Selected :: " + textField.getSelectedText()); if (textField.getSelectedText() == null) { if (textField.getCaretPosition() <= pointPosition) { return; } else { if (textField.getText().substring(pointPosition).length() <= 2) { return; } } } else { return; } } else { return; } } if (keychar == KeyEvent.VK_PERIOD && textField.getText().indexOf('.') < 0) return; // if (keychar == KeyEvent.VK_MINUS && (textField.getText().indexOf('-') < 0) && // textField.getCaretPosition() == 0) // return; keyEvent.consume(); } }); }