Key Listener

keyPressed
keyReleased
keyTyped

Application :    Whatever typed in first TextField should appear simultaneously in second TextField. Also on clicking ALT+’x’ , application should get closed.









import java.awt.*;
import java.awt.event.*;
class FrameKey implements KeyListener
{
    Frame f;
    TextField tf1, tf2;
    boolean b1;

    FrameKey()
    {
        f=new Frame("Frame with Key");
        tf1 = new TextField(20);
        tf2 = new TextField(20);
       
        //Change in Layout Manager if necessary
        f.setLayout(new FlowLayout());

        f.add(tf1);
        f.add(tf2);

        tf1.addKeyListener(this);

        f.setSize(400,100);
        f.setVisible(true);
    }
    public void keyTyped(KeyEvent e)
      {
        String s = tf1.getText();
        char c = e.getKeyChar();
        tf2.setText(s+c);
        if(b1 && (e.getKeyChar() == 'x'))
        {
            System.exit(0);
        }
    }
    public void keyPressed(KeyEvent e)
    {
        if (e.getKeyCode() == e.VK_ALT)
        {
            b1=true;
        }
    }
    public void keyReleased(KeyEvent e)
    {
            b1=false;
    }
    public static void main(String [] args)
    {
        FrameKey x = new FrameKey();
    }
}