This section provides an example that implements keyListener in java.
How to use KeyListener
Introduction
In this section, you will learn how to handle different key events on the Java Awt component by using the event handling in Java. When the given program is executed then you will see the several key events these are applied on the component like the key pressed, key release and so on.
All the key events are handled through the KeyListener Interface that has been implemented in the main class of the program.
Description of this program:
In this program, you will see how to show display the specific massage on the frame according to the event. When you press the key then the message "Key Pressed" will be seen on the frame and if when you stop pressing keys then the message "Key Released" will be seen.
This is possible by using the KeyListener interface. It will generate the KeyEvent and you can the check the exact event by using different method like the keyTyped(), keyPressed() and the keyReleased() method which are used for retrieving the generated specific event.
Here is the code of this program:
import java.awt.*;
|
Output of program: