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

Program to explain Key Event with Virtual Key in Java

Filed under: Java on 2023-10-19 07:04:19

//  Program to explain Key Event with Virtual Key.

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

/*
<applet code="KeyEventTest1" width="300" height="100">
</applet>
*/

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

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

public void keyPressed(KeyEvent ke) 
{
 showStatus("Key Down");
 int key = ke.getKeyCode();

 switch(key) 
 {
  case KeyEvent.VK_F1:
   msg += "<F1>";
   break;
  case KeyEvent.VK_F2:
   msg += "<F2>";
   break;
  case KeyEvent.VK_F3:
   msg += "<F3>";
   break;
  case KeyEvent.VK_PAGE_DOWN:
   msg += "<PgDn>";
   break;
  case KeyEvent.VK_PAGE_UP:
   msg += "<PgUp>";
   break;
  case KeyEvent.VK_LEFT:
   msg += "<Left Arrow>";
   break;
  case KeyEvent.VK_RIGHT:
   msg += "<Right Arrow>";
   break;
 } 
 repaint();
}

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.