Java KeyListener Interface
The Java KeyListener is notified whenever you change the state of key. It is notified against KeyEvent. The KeyListener interface is found in java.awt.event package, and it has three methods.
Following is the declaration for java.awt.event.KeyListener interface:
Methods of KeyListener interface
The signature of 3 methods found in KeyListener interface are given below:
public abstract void keyPressed (KeyEvent e);
It is invoked when a key has been pressed.
public abstract void keyReleased (KeyEvent e);
It is invoked when a key has been released.
public abstract void keyTyped (KeyEvent e);
It is invoked when a key has been typed.
This interface inherits methods from the following interface:
Java KeyListener Example
In the following example, we are implementing the methods of the KeyListener interface.
Java KeyListener Example 2: Count Words & Characters
In the following example, we are printing the count of words and characters of the string. Here, the string is fetched from the TextArea and uses the KeyReleased() method of KeyListener interface.