Home » Desktop Java » awt » event » A complete KeyListener example

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

A complete KeyListener example

In this example we are going to see how you can use a KeyListener in Java, in order to monitor key events that take place in your applications. This is particularly useful when you want to add some key functionality to your app and it’s very important you want to monitor that has a very intensive keyboard activity.

Basically, to use a KeyListener in Java you have to:

  • Create a new KeyListener.
  • Override the methods that correspond to the key events you want to monitor e.g keyPressed, keyReleased, keyTyped.
  • Create a JTextField component
  • Use it’s addKeyListener method to add to it the KeyListener you’ve created.

Let’s take a closer look at the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

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

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

public class KeyListenerExample {

    public static void main(String args[]) {

  JFrame frame = new JFrame("Key Listener");

  Container contentPane = frame.getContentPane();

  KeyListener listener = new KeyListener() {

@Override

public void keyPressed(KeyEvent event) {

    printEventInfo("Key Pressed", event);

}

@Override

public void keyReleased(KeyEvent event) {

    printEventInfo("Key Released", event);

}

@Override

public void keyTyped(KeyEvent event) {

    printEventInfo("Key Typed", event);

}

private void printEventInfo(String str, KeyEvent e) {

    System.out.println(str);

    int code = e.getKeyCode();

    System.out.println("   Code: " + KeyEvent.getKeyText(code));

    System.out.println("   Char: " + e.getKeyChar());

    int mods = e.getModifiersEx();

    System.out.println("    Mods: "

+ KeyEvent.getModifiersExText(mods));

    System.out.println("    Location: "

+ keyboardLocation(e.getKeyLocation()));

    System.out.println("    Action? " + e.isActionKey());

}

private String keyboardLocation(int keybrd) {

    switch (keybrd) {

  case KeyEvent.KEY_LOCATION_RIGHT:

return "Right";

  case KeyEvent.KEY_LOCATION_LEFT:

return "Left";

  case KeyEvent.KEY_LOCATION_NUMPAD:

return "NumPad";

  case KeyEvent.KEY_LOCATION_STANDARD:

return "Standard";

  case KeyEvent.KEY_LOCATION_UNKNOWN:

  default:

return "Unknown";

    }

}

  };

  JTextField textField = new JTextField();

  textField.addKeyListener(listener);

  contentPane.add(textField, BorderLayout.NORTH);

  frame.pack();

  frame.setVisible(true);
    }
}

 
This was an example on how to use KeyListener in Java.

(-2 rating, 2 votes)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of