import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="KeyEvents" width=300 height=100>
</applet>
*/
public class KeyEvents extends Applet implements KeyListener
{
String msg = "";
int X = 10, Y = 20; // output coordinates
public void init()
{
addKeyListener(this);
requestFocus(); // request input focus
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Down");
int key = ke.getKeyCode();
switch(key)
{
case KeyEvent.VK_F1:
msg+="<F1>";
break;
case KeyEvent.VK_F2:
msg+="<F2>";
break;
case KeyEvent.VK_F3:
msg+="<F3>";
break;
case KeyEvent.VK_F4:
msg+="<F4>";
break;
case KeyEvent.VK_F5:
msg+="<F5>";
break;
case KeyEvent.VK_F6:
msg+="<F6>";
break;
case KeyEvent.VK_F7:
msg+="<F7>";
break;
case KeyEvent.VK_F8:
msg+="<F8>";
break;
case KeyEvent.VK_F9:
msg+="<F9>";
break;
case KeyEvent.VK_F10:
msg+="<F10>";
break;
case KeyEvent.VK_PAGE_DOWN:
msg+="<pgdn>";
break;
case KeyEvent.VK_PAGE_UP:
msg+="<pgup>";
break;
case KeyEvent.VK_LEFT:
msg+="<left arrow>";
break;
case KeyEvent.VK_RIGHT:
msg+="<right arrow>";
break;
case KeyEvent.VK_UP:
msg+="<Uper arrow>";
break;
case KeyEvent.VK_DOWN:
msg+="<down arrow>";
break;
}
repaint();
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke)
{
msg += ke.getKeyChar();
repaint();
}
// Display keystrokes.
public void paint(Graphics g)
{
g.drawString(msg, X, Y);
}
}