You are here: Home / Topics / Program to explain Key Event in Java

Program to explain Key Event in Java

Filed under: Java on 2023-10-19 07:03:47

//  Program to explain Key Event.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet  code="KeyEventTest"  width="300" height="200">
</applet>
*/

public class KeyEventTest extends Applet implements KeyListener 
{
String msg = "";
int X = 10, Y = 20; 

public void init() 
{
 addKeyListener(this);
}

public void keyPressed(KeyEvent ke) 
{
 showStatus("Key Down");
}

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

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.