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);
}
}


Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: