Home » Desktop Java » awt » event » A complete KeyListener Java 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 Java 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:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
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 of how to use KeyListener in Java.

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

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments