Introduction to Java KeyListener
In Java, the KeyListener class gets notified when there is a change in the Key state. The states include press, release, and type. It is especially useful when some functionality on the key has to be added to your application and any keyboard activity has to be monitored. KeyListener works by using the implements keyword and this can be retrieved from the package java.awt.event.
Declaration:
Java KeyListener interface can be declared using the below syntax.
Public interface KeyListener extends EventListener
Methods of Java KeyListener
The following are the commonly used methods:
- KeyPresses(KeyEventev): This method will be invoked when Key is pressed.
- KeyReleased(KeyEventev): This method will be invoked when a key is released.
- KeyTyped(KeyEventev): This method will be invoked when Key is typed.
How Java KeyListener Works?
Following are some points which show the working:
- When there is a class that has to process certain Key events, an object should be present in that which helps in implementing the interface.
- Since the object is already registered with the listener using the addKeyListener method, an event will be generated on press, type and release states of Key.
- This will help in the invocation of the relevant method in the object of the listener.
- After the invocation, the KeyEvent is passed to it.
Examples
In the following article, we are going to see how Java is used to monitor the key events happening in our applications.
Example #1
Java program to demonstrate the KeyListener.
Code:
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class JavaKeyListenerExample implements KeyListener
{
Label lb1, lbl2, lb;
TextField tf1;
Frame fr;
String s;
JavaKeyListenerExample()
{
//create a frame
fr = new Frame("KeyEventListener Example");
//create labels lb1, lbl2 and lb
lb1= new Label(" Key Events will be displayed based on the actions", Label.CENTER);
lbl2= new Label();
lb= new Label();
//create a text field
tf1 = new TextField(20);
//set the layout for frame
fr.setLayout(new FlowLayout());
//add label 1 to the frame
fr.add(lb1);
//add textfield to the frame
fr.add(tf1);
//add label 2 to the frame
fr.add(lbl2);
//on clicking the button, text field data will be read
tf1.addKeyListener(this);
//set the size of frame
fr.setSize(460,250);
//set the visibility as true
fr.setVisible(true);
}
//events to be done on pressing key
public void keyPressed(KeyEvent ev)
{
lbl2.setText("Pressed Key");
}
//events to be done on releasing key
public void keyReleased(KeyEvent ev)
{
lbl2.setText(" Released key");
}
//events to be done on typing key
public void keyTyped(KeyEvent ev)
{
lbl2.setText("Key is typed");
//set the visibility as true
fr.setVisible(true);
}
public static void main(String[] args)
{
new JavaKeyListenerExample();
}
}
Output:
This program simply displays the key event that is happening. When anything is typing, the label will be displayed on the basis of that.
Example #2
Java program to demonstrate the KeyListener with texts displaying on console on certain Key events along with the key code
Code:
//Java program to demonstrate the KeyListener with texts displaying on certain Key events along with the key code
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class JavaKeyListenerExample implements KeyListener
{
Label lb1, lbl2;
TextField tf1;
Frame fr;
String s;
JavaKeyListenerExample()
{
//create a frame
fr = new Frame("KeyEventListener Example");
//create labels lb1 and lbl2
lb1= new Label(" Key Events will be displayed based on the actions", Label.CENTER);
lbl2= new Label();
//create a text field
tf1 = new TextField(20);
//set the layout for frame
fr.setLayout(new FlowLayout());
//add label 1 to the frame
fr.add(lb1);
//add textfield to the frame
fr.add(tf1);
//add label 2 to the frame
fr.add(lbl2);
//on clicking the button, text field data will be read
tf1.addKeyListener(this);
//set the size of frame
fr.setSize(460,250);
//set the visibility as true
fr.setVisible(true);
}
//events to be done on pressing key
public void keyPressed(KeyEvent ev)
{
s= "Event : Key " + ev.getKeyCode() + " is pressed ";
//set the text as label
lbl2.setText(s);
//set the visibility as true
fr.setVisible(true);
}
//events to be done on releasing key
public void keyReleased(KeyEvent ev)
{
s+=" Event : Key Released ";
//set the text as label
lbl2.setText(s);
//set the visibility as true
fr.setVisible(true);
s="";
}
//events to be done on typing key
public void keyTyped(KeyEvent ev)
{
s+=" Event : Key Typed ";
//set the text as label
lbl2.setText(s);
//set the visibility as true
fr.setVisible(true);
}
public static void main(String[] args)
{
new JavaKeyListenerExample();
}
}
Output:
On executing the code, a text field will appear with a label as shown in the image.
Once a key is pressed, the key number and the corresponding events that happen will also be displayed as in the image below.
Example #3
Java program to demonstrate the KeyListener with words, its count and number of characters.
Code:
//Java program to demonstrate the KeyListener with words, its count and nuber of characters
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class JavaKeyListenerExample extends Frame implements KeyListener{
//label
Label lbl;
//text area
TextArea a;
JavaKeyListenerExample(){
//create a label
lbl=new Label();
//set the bounds for label
lbl.setBounds(30,50,210,30);
//create a text area
a=new TextArea();
//set the bounds for text area
a.setBounds(30 , 80 , 100 , 100);
a.addKeyListener(this);
//add label
add(lbl);
//add text area
add(a);
//set the size
setSize(600,600);
//set layout
setLayout(null);
//set the visibility as true
setVisible(true);
}
public void keyPressed( KeyEvent ev ) {
}
//on releasing the key
public void keyReleased( KeyEvent ev )
{
//get the text typed in text area
String t=a.getText();
//split the text
String w[]=t.split("\\s");
//set the text as label
lbl.setText("No. of words: "+ w.length +" No. of Characters: "+ t.length() );
}
public void keyTyped( KeyEvent ev ) {
}
//main method
public static void main(String[] args) {
new JavaKeyListenerExample();
} }
Output:
The total number of words and characters will be displayed on the successful execution of the code.
Conclusion
It is a class that gets notified when there is a change in the states of Key such as press, type or release of Key. More details such as declaration, methods, working, and implementation of this interface are discussed in this document in detail.
Recommended Articles
This is a guide to Java KeyListener. Here we discuss the Introduction and How it Works along with different examples and its code implementation. You may also look at the following articles to learn more –