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;
}
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);
}
}
Output:
Editorial Team
We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.
12 thoughts on “Java applet program for handling Keyboard events”
nice program.
what is the function of requestFocus(); method?
if you send the answer in my mail then i will obliged to you.
HI this is very good web site for understanding the handling java programs
thanks it helped me
Hi fellas, just want to warn you – don’t pay $500 for platinum roulette system,
you can find it for free, just search in google – platinum roulette system – you
will find interesting post about this system
Leave a Reply
Get the latest updates on your inbox
Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.
nice program.
what is the function of requestFocus(); method?
if you send the answer in my mail then i will obliged to you.
HI this is very good web site for understanding the handling java programs
thanks it helped me
Hi fellas, just want to warn you – don’t pay $500 for platinum roulette system,
you can find it for free, just search in google – platinum roulette system – you
will find interesting post about this system