Java AWT KeyEvent get modifier text

Description

Java AWT KeyEvent get modifier text

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main {
   public static void main(String[] args) {
      JFrame frame = new JFrame("Window Title");

      frame.setBounds(50, 100, 300, 300);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JTextField typingArea = new JTextField(20);
      typingArea.addKeyListener(new KeyListener() {
         public void keyTyped(KeyEvent e) {
            displayInfo(e, "KEY TYPED: ");
         }/*from www . j  a  va2  s .c  o  m*/
         public void keyPressed(KeyEvent e) {
            displayInfo(e, "KEY PRESSED: ");
         }
         public void keyReleased(KeyEvent e) {
            displayInfo(e, "KEY RELEASED: ");
         }
         protected void displayInfo(KeyEvent e, String s) {
            int modifiers = e.getModifiersEx();
            System.out.println("modifiers = " + modifiers);
            System.out.println(KeyEvent.getModifiersExText(modifiers));         
         }
      });
      frame.add(typingArea);
      frame.setVisible(true);
   }
}



PreviousNext

Related