Key Listener Event in Advanced Java Programming

Home  »  Java  »  Advance Java »    Java Event Handling »    KeyListener

The KeyListener interface declares methods that handle input from the keyboard. The methods defined in the KeyListener interface are:

  1. void keyPressed(KeyEvent ke) Is called when an end user presses a key on the keyboard.
  2. void keyReleased(KeyEvent ke) Is called when an end user releases a key on the keyboard.
  3. void keyTyped(KeyEvent ke) Is called when an end user types a key on the keyboard, which is the operation of pressing and releasing the key in one fast action.


The above code shows implementing the KeyListener Interface for accepting input from the keyboard to the applet window. The getKeyChar() method enables taking input from the keybaord and displaying the information in the applet window. The keyPressed method is used for defining the statements that are run when an end user enters input from the keyboard and code will be stored in the file called KeyListenerTest.java, which on compilation will crate a class file called KeyListenerTest.class. Now compile this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac KeyListenerTest.java
C:\>jdk1.4\bin>appletviewer KeyListenerTest.java


"Output of KeyListenerTest.java"


Download Complete Program

  -  



Event Listener


Action Listener Adjustment Listener Mouse Listener
Mouse Motion Listener Window Listener Key Listener
Focus Listener ITEM Listener Text Listener



No comments:

Post a Comment