Keyboard Input

KeyListener Example: (Run Applet)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KeyListenerDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    class View extends JPanel implements KeyListener
    {
        private String message = "A"; // initial letter
        private int x = 100;          // Initial coordinates of letter
        private int y = 100;

        public View()
        {
            super();
            this.setBackground(Color.white);

            // addKeyListener() and setFocusable() must both be included in order to allow key input
            this.addKeyListener(this);
            this.setFocusable(true);
        }

        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawString(this.message, this.x, this.y);
        }

        @Override
        public void keyPressed(KeyEvent e)
        {
            // allow user to select a letter
            final char c = e.getKeyChar();
            if ((c >= 'A') && (c <= 'z'))
            {
                this.message = String.valueOf(c);
            }


            // allow user to use the shift key
            if (e.isShiftDown())
            {
                this.setBackground(Color.RED);
                this.setFont(new Font("timesroman", Font.ITALIC, 60));
            }
            else
            {
                this.setBackground(Color.YELLOW);
                this.setFont(new Font("timesroman", Font.PLAIN, 20));
            }


            // allow user to select an arrow key
            switch (e.getKeyCode())
            {
                case KeyEvent.VK_LEFT:
                {
                    this.x--;
                    break;
                }
                case KeyEvent.VK_RIGHT:
                {
                    this.x++;
                    break;
                }
                case KeyEvent.VK_UP:
                {
                    this.y--;
                    break;
                }
                case KeyEvent.VK_DOWN:
                {
                    this.y++;
                    break;
                }
            }

            this.repaint();
        }

        @Override
        public void keyReleased(KeyEvent e)
        {
        }

        @Override
        public void keyTyped(KeyEvent e)
        {
        }
    }
}

The KeyListener class is used to control keyboard input. KeyListener contains the following methods:

public void keyTyped(KeyEvent e)    // only works for alpha keys
public void keyPressed(KeyEvent e)  // works for all keys
public void keyReleased(KeyEvent e) // works for all keys

In order to use a keyListener(), a conponent must call the two methods:

addKeyListener(this); // add the listener
setFocusable(true); // allow key input
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>