KeyEvent: getKeyChar() : KeyEvent « java.awt.event « Java by API






KeyEvent: getKeyChar()

 

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

public class MainClass {
  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);
      }

      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);
  }
}
           
         
  








Related examples in the same category

1.KeyEvent.KEY_LOCATION_LEFT
2.KeyEvent.KEY_LOCATION_NUMPAD
3.KeyEvent.KEY_LOCATION_RIGHT
4.KeyEvent.KEY_LOCATION_STANDARD
5.KeyEvent.KEY_TYPED
6.KeyEvent.VK_B
7.KeyEvent.VK_DOWN
8.KeyEvent.VK_LEFT
9.KeyEvent.VK_N
10.KeyEvent.VK_RIGHT
11.KeyEvent.VK_U
12.KeyEvent.VK_UP
13.KeyEvent: getKeyCode()
14.KeyEvent: getKeyLocation()
15.KeyEvent: getKeyText(int keyCode)
16.KeyEvent: isActionKey()