The KeyListener Interface
The KeyListener interface listens for events which are related to the keyboard. It is similar to the MouseListener interface. It listens for when a key is pressed or released, and when a key is typed (a combination of being pressed and released).
The three methods implemented by the interface are keyPressed, keyReleased, and keyTyped. Again, these methods should be self-explanatory. They are all void methods that take a single KeyEvent object as their parameter.
One distinction should be made about the KeyEvent class. When determining which key was pressed, you can examine the actual character type, or its virtual key code. When you are interested in the actual character generated (such as “a” versus “A”), use the getKeyChar method, which returns a char. However, when you are only interested in which key on the keyboard was typed, use the getKeyCode method, which returns an int. So for example, if the user holds down the Shift key and types the ‘G’ key, then getKeyChar will generate a “G.” However, if just the “G” key is pressed, then getKeyChar will produce the character “g.” In both cases, getKeyCode will produce the integer constant VK_G.
All key codes are prefixed by VK_. There are about 200 virtual key codes defined in the KeyEvent class.
The following applet, KeyTest, shows the use of both the getKeyChar and the getKeyCode methods. As you read the source code or type it into your text editor, think about the advantages and disadvantages of both methods.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
// this applet allows the user to move a black rectangle using the arrow keys.
// the user can also change the applet's background color with the
// 'R', 'G', 'B', and 'W' keys. It also shows the different ways a KeyEvent
// object can be interpreted.
public class KeyTest extends Applet implements KeyListener
{
// moveable black rectangle
private Rectangle r;
// current background color for the applet
private Color backColor;
// this method overrides the init method from the Applet class
public void init()
{
r = new Rectangle(0, 0, 20, 10);
backColor = Color.WHITE;
addKeyListener(this);
}
// paints the rectangle at the updated position
public void paint(Graphics g)
{
setBackground(backColor);
g.fillRect(r.x, r.y, r.width, r.height);
}
/** methods implemented in the KeyListener interface */
public void keyPressed(KeyEvent e)
{
// for this method, use the key code to generate movement
int keyCode = e.getKeyCode();
// move the rectangle
if(keyCode == KeyEvent.VK_LEFT)
{
r.x -= 5;
if(r.x < 0) r.x = 0;
repaint();
}
else if(keyCode == KeyEvent.VK_RIGHT)
{
r.x += 5;
if(r.x > getSize().width-r.width)
{
r.x = getSize().width-r.width;
}
repaint();
}
else if(keyCode == KeyEvent.VK_UP)
{
r.y -= 5;
if(r.y < 0) r.y = 0;
repaint();
}
else if(keyCode == KeyEvent.VK_DOWN)
{
r.y += 5;
if(r.y > getSize().height-r.height)
{
r.y = getSize().height-r.height;
}
repaint();
}
}
public void keyReleased(KeyEvent e)
{
// do nothing...
}
public void keyTyped(KeyEvent e)
{
// for this method, use the actual key char to call action
char keyChar = e.getKeyChar();
// change the background color
switch(keyChar)
{
case 'r':
{
backColor = Color.RED;
repaint();
break;
}
case 'g':
{
backColor = Color.GREEN;
repaint();
break;
}
case 'b':
{
backColor = Color.BLUE;
repaint();
break;
}
case 'w':
{
backColor = Color.WHITE;
repaint();
break;
}
}
}
} // KeyTest
Additionally, moving the rectangle around with the arrow keys, you can also change the applet’s background color to red, green, blue, and back to white. If the keyboard doesn’t seem to move the block around the screen, make sure that the window is the currently focused window on the desktop (this can be done by clicking the mouse anywhere in the KeyTest window).


LinkBack URL
About LinkBacks
Reply With Quote

LinkBacks Enabled by vBSEO
Bookmarks