How To Implement Keyboard Events In Java

In this tutorial we will show you how to implement keyboard event in java. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.


Source Code
KeyEvents.Java

//This program is to demonstrate the key board handling n java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="KeyEvents" width=300 height=300></applet>*/
public class KeyEvents extends Applet implements KeyListener
{
String msg="Typed";
int x=30,y=50;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyTyped(KeyEvent ke)
{
msg+=ke.getKeyChar();
repaint();
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Up!");
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Down!");
}
public void paint(Graphics G)
{
G.drawString(msg,x,y);
}
}


Source Code