Handle keyboard events, which echoes keystrokes to the applet window and shows the status of each key event in the status bar.

 /* Demonstrate the key event handlers   02-10-2017 */


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

/*
<applet code="SimpleKey" width=400 height=200>
</applet>
*/

public class SimpleKey extends Applet implements KeyListener
{
    String msg = "";
    int X = 10, Y = 20; // output coordinates
    public void init()
    {
        addKeyListener(this);
        //requestFocus();
    }

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

Comments

Popular posts from this blog

Design GUI to handle Choice Control event.

Write a Java program that reads an integer number (between 1 and 255) from the user and prints the binary representation of the number. The answer should be printed as a String. Note: The output displayed should contain 8 digits and should be padded with leading 0s(zeros), in case the returned String contains less than 8 characters. For example, if the user enters the value 16, then the output should be 00010000 and if the user enters the value 100, the output should be 01100100 (Hint : You may use String.format() method for the expected output)