import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="KeyBoardEvent.class" width=200 height=200></applet> */
public class KeyBoardEvent extends Applet implements KeyListener
{
String str1="You pressed:nothing";
String str2="You typed:";
String kp="";
int ch,kc;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyTyped(KeyEvent e)
{
ch=1;
str2+=e.getKeyChar();
repaint();
}
public void keyPressed(KeyEvent e)
{
ch=2;
kp="Your pressed:";
kc=e.getKeyCode();
switch(kc)
{
case KeyEvent.VK_PAGE_UP: kp+="page Up Key";
break;
case KeyEvent.VK_PAGE_DOWN: kp+="page Down Key";
break;
case KeyEvent.VK_HOME: kp+="Home Key";
break;
case KeyEvent.VK_END: kp+="End Key";
break;
case KeyEvent.VK_LEFT: kp+="Left Arrow Key";
break;
case KeyEvent.VK_RIGHT: kp+="Right Arrow Key";
break;
case KeyEvent.VK_UP: kp+="Up Arrow Key";
break;
case KeyEvent.VK_DOWN: kp+="Down Arrow Key";
break;
case KeyEvent.VK_F1: kp+="Functional Key F1";
break;
case KeyEvent.VK_F2: kp+="Functional Key F2";
break;
}
repaint();
showStatus("KEY DOWN");
}
public void keyReleased(KeyEvent e)
{
showStatus("KEY UP");
}
public void paint(Graphics g)
{
if(ch==1)
g.drawString(str2,15,15);
else if(ch==2)
{
g.drawString(kp,15,15);
str2="You typed:";
}
else
g.drawString(str1,15,15);
}
}
import java.awt.event.*;
import java.applet.*;
/* <applet code="KeyBoardEvent.class" width=200 height=200></applet> */
public class KeyBoardEvent extends Applet implements KeyListener
{
String str1="You pressed:nothing";
String str2="You typed:";
String kp="";
int ch,kc;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyTyped(KeyEvent e)
{
ch=1;
str2+=e.getKeyChar();
repaint();
}
public void keyPressed(KeyEvent e)
{
ch=2;
kp="Your pressed:";
kc=e.getKeyCode();
switch(kc)
{
case KeyEvent.VK_PAGE_UP: kp+="page Up Key";
break;
case KeyEvent.VK_PAGE_DOWN: kp+="page Down Key";
break;
case KeyEvent.VK_HOME: kp+="Home Key";
break;
case KeyEvent.VK_END: kp+="End Key";
break;
case KeyEvent.VK_LEFT: kp+="Left Arrow Key";
break;
case KeyEvent.VK_RIGHT: kp+="Right Arrow Key";
break;
case KeyEvent.VK_UP: kp+="Up Arrow Key";
break;
case KeyEvent.VK_DOWN: kp+="Down Arrow Key";
break;
case KeyEvent.VK_F1: kp+="Functional Key F1";
break;
case KeyEvent.VK_F2: kp+="Functional Key F2";
break;
}
repaint();
showStatus("KEY DOWN");
}
public void keyReleased(KeyEvent e)
{
showStatus("KEY UP");
}
public void paint(Graphics g)
{
if(ch==1)
g.drawString(str2,15,15);
else if(ch==2)
{
g.drawString(kp,15,15);
str2="You typed:";
}
else
g.drawString(str1,15,15);
}
}
No comments:
Post a Comment