Java KeyEvent.isActionKey()

Syntax

KeyEvent.isActionKey() has the following syntax.

public boolean isActionKey()

Example

In the following code shows how to use KeyEvent.isActionKey() method.


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;
//from  ww w .  j a v  a  2  s . c  om
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");
  }
}




















Home »
  Java Tutorial »
    java.awt.event »




AdjustmentListener
ActionEvent
ActionListener
HierarchyEvent
HierarchyListener
InputEvent
ItemEvent
ItemListener
KeyAdapter
KeyEvent
KeyListener
MouseAdapter
MouseEvent
MouseListener
MouseMotionAdapter
MouseMotionListener
MouseWheelEvent
MouseWheelListener
WindowAdapter
WindowEvent
WindowFocusListener
WindowStateListener