Write a java Applet program to perform KeyBoard Event
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Key" width=300 height=400>
</applet>
*/
public class Key extends Applet
implements KeyListener
{
int X=20,Y=30;
String msg="KeyEvents--->";
public void init()
{
addKeyListener(this);
requestFocus();
setBackground(Color.green);
setForeground(Color.blue);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=k.getKeyCode();
switch(key)
{
case KeyEvent.VK_UP:
showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:
showStatus("Move to Right");
break;
case KeyEvent.VK_F1:
showStatus("F1");
break;
case KeyEvent.VK_F2:
showStatus("F2");
break;
case KeyEvent.VK_F3:
showStatus("F3");
break;
case KeyEvent.VK_F4:
showStatus("F4");
break;
case KeyEvent.VK_F5:
showStatus("F5");
break;
}
repaint();
}
public void keyReleased(KeyEvent k)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent k)
{
msg+=k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}
**********************************************
HTML
<html>
<applet code="Key.class" width="800" height="500">
</applet>
</html>
Leave a Comment