Home Java Example Java Awt How to use KeyListener
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

How to use KeyListener

Advertisement
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.*; 
import java.awt.event.*;
 

public class KeyListenerTester extends Frame implements KeyListener{  
  TextField t1;
  Label l1;
  public KeyListenerTester(String s ) {  
  super(s)
  Panel p =new Panel();
  l1 = new Label ("Key Listener!" ;
  p.add(l1);  
  add(p);
  addKeyListener this 
  setSize 200,100 );
  setVisible(true);
  addWindowListener(new WindowAdapter(){
 public void windowClosing(WindowEvent e){
  System.exit(0);
 }
  });
  }  
  public void keyTyped KeyEvent e ){  
  l1.setText("Key Typed");
 }  
  public void keyPressed KeyEvent e){  
  l1.setText "Key Pressed" 
  }  
  public void keyReleased KeyEvent e ){  
  l1.setText"Key Released" 
  }  
  public static void main (String[]args ){  
  new KeyListenerTester "Key Listener Tester" 
 }  
  }  
  

Output of program:

Download this program:

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Related Tutorials

Ask Questions?    Discuss: How to use KeyListener   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
qwerty
January 6, 2012
woah

Hell Yeah!!!
Surendra
January 13, 2012
Thanks!!

I like the example u posted. Thanxx.. Was nice to understand nd helped me..
Surendra
January 13, 2012
THanxx

It really helped a lot.. Thanx for the clear example..
sravan
October 22, 2012
thank you

thank you sir this prgrm helped me a lot.. i would like to learn game tutorials in java
ht
April 18, 2013
Thanks

Very helpful code. I really wish they'd called JFrame something slightly different, it's so hard to find good awt examples when all the more common swing components are so similarly named.
DMCA.com