1. Technology

Using a KeyListener Interface


Man using laptop computer
Tim Robbins/Mint Images RF/Getty Images

The KeyListener interface provides the ability to find out what keys a user is pressing while a graphical component (e.g., JTextField, JLabel, JTextArea) has the focus of the graphical user interface.

The KeyListener Interface

The interface has three methods that must be implemented by a class:

public interface KeyListener{

    public void keyPressed(KeyEvent e);
    public void keyReleased(KeyEvent e);
    public void keyTyped(KeyEvent e);


The keyPressed method and the keyReleased method are called when any key is pressed down or released respectively. The keyTyped method is only called when a valid Unicode character is entered. For example,  if the user holds the SHIFT key down, the keyPressed method is invoked. When the user releases the SHIFT key the keyReleased method is invoked. But unless the user presses the SHIFT key and a character key (i.e., any letter or number) together the keyTyped is not invoked.

The KeyEvent Object

The KeyEvent object is passed to a KeyListener method when it is called. It contains information about the key event that has occurred. The KeyEvent object has some useful methods, including:

  • getKeyChar - returns the character for the key event.
  • getKeyCode - returns the Unicode value for the character of the key event
  • isActionKey - .a boolean value determining if the key is classed as an action key (e.g., Enter key, Delete key)

Using the information in these methods helps to determine the key(s) that fired the key event.

Implementing the KeyListener Interface

Let's implement the KeyListener interface for a JTextArea component. Here's the listing for a simple GUI that uses a JFrame to hold two JTextAreas - one will listen for the key events and the other will sit inside a JScrollPane providing feedback about the key events being triggered:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

public class KeyListenerExample {
    JTextArea inputText;
    JTextArea feedbackText;

     //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {

         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
             public void run()
                 new KeyListenerExample();         
    public KeyListenerExample()
        JFrame guiFrame = new JFrame();
        //make sure the program exits when the frame closes
        guiFrame.setTitle("Creating a Table Example");
        //This will center the JFrame in the middle of the screen
        feedbackText = new JTextArea();
        JScrollPane scrollText = new JScrollPane(feedbackText);
        inputText = new JTextArea();

        //place keylistener code here
        guiFrame.add(inputText, BorderLayout.NORTH);
        guiFrame.add(scrollText, BorderLayout.CENTER);

First add the import statements for the KeyListener interface and KeyEvent object to the top of the class:

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

Now, we can implement the KeyListener as an anonymous inner class attached to the inputText JTextArea by using the addKeyListener method: 

inputText.addKeyListener(new KeyListener()
    public void keyPressed(KeyEvent e)
        feedbackText.append("Key Pressed: " + e.getKeyChar() + "\n");

    public void keyReleased(KeyEvent e)
        feedbackText.append("Key Released: " + e.getKeyChar() + "\n");
    public void keyTyped(KeyEvent e)
        feedbackText.append("Key Typed: " + e.getKeyChar() + " " + KeyEvent.getKeyModifiersText(e.getModifiers()) + "\n");

The text that is appended to the feedbackText JTextArea will depend on the keys that the user types into the inputText JTextArea. If the user presses a normal character key then all three methods will be called in order of keyPressed, keyReleased and keyTyped. If the user just types the SHIFT key then only the keyPressed and keyReleased methods will be invoked.

The full Java code listing can be found in A KeyListener Example Program.

  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. Handling Events
  6. Java Syntax: Using a Key Listener Interface

©2014 About.com. All rights reserved.