Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views2 pages

KeyListener Interface

The KeyListener interface in Java is used to handle key events like press, release, and type. It contains 3 methods - keyPressed, keyReleased, and keyTyped. The example shows adding a KeyListener to a TextArea to display which key event occurred.

Uploaded by

Tejas Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

KeyListener Interface

The KeyListener interface in Java is used to handle key events like press, release, and type. It contains 3 methods - keyPressed, keyReleased, and keyTyped. The example shows adding a KeyListener to a TextArea to display which key event occurred.

Uploaded by

Tejas Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Dr Bharti

KeyListener Interface
 In Java, KeyListener Interface is under java.awt.event
package.
 This interface is used when the state of the key is
changed.
 It has 3 methods which are as following:

1. public abstract void keyPressed(KeyEvent e);

2. public abstract void keyReleased(KeyEvent e);

3. public abstract void keyTyped(KeyEvent e);

Example:
In this example, we are using keylistener interface to handle
key events that can be key release, typed etc. see the below
example.

import java.awt.*;
import java.awt.event.*;
public class KeyListenerDemo1 extends Frame implements
KeyListener
{
Label kL_l;
TextArea kL_area;
KeyListenerDemo1(){
kL_l=new Label();
kL_l.setBounds(20,50,500,20);
kL_area=new TextArea();
kL_area.setBounds(20,80,300, 300);
kL_area.addKeyListener(this);
add(kL_l);
add(kL_area);
Dr Bharti

setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed (KeyEvent e) {
kL_l.setText("Key Pressed");
}
public void keyReleased(KeyEvent e) {
kL_l.setText("Key Released");
}
public void keyTyped(KeyEvent e) {
kL_l.setText("Key Typed");
}

public static void main(String[] args) {


new KeyListenerDemo1();
}
}

You might also like