Write a Java Program to demonstrate Keyboard event.
Program Code:
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
/* <applet code="KeyEvents.class" width=300 height=200> </applet> */
public class KeyEvents extends Applet implements KeyListener
{
String msg =" ";
int x=10,y=20;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent k)
{
showStatus("key down");
}
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);
}
}
Post A Comment:
0 comments: