objectdraw
Class KeyInterpreter

java.lang.Object
  extended by objectdraw.KeyInterpreter
All Implemented Interfaces:
KeyListener, EventListener

public class KeyInterpreter
extends Object
implements KeyListener

KeyInterpreter is the class that processes key events and calls the appropriate methods in WindowController. Additional key interpreters can be created to pipe these events to other objects as well. For example, you may want an ActiveObject to be controlled by keys, and you can do this by making a new KeyInterpreter that has that object as its target and attaching it to the canvas.

There are three types of methods called when a key is pressed. Suppose that I press the space bar, hold it down for a few seconds, and release it. When it is first pressed, the method onSpacePress() of the target will be called, if it exists. While the key is held down, periodically the method onSpaceHold() will be called. Finally, when the key is released, the method onSpaceRelease() will be called.

In general, it is a good idea to not rely on receiving the onSpaceHold() event, because different computers produce different sets of events when a key is held, with different timings. Instead, use a variable to remember that the key is held, changing it in the press and release methods.

There are two other method you may make use of to process key events. Sometimes you want to know, not what key was pressed, but what text it typed. For example, you may want to have someone type their name into the applet and have it appear on the screen. The method onKeyTyped(String text) will be called when a key has been typed, and text will be whatever text that key produces, properly taking into account what modifiers (such as the shift key) are down.

Another option for receiving key events is to implement the onOtherPress(String name) method (and corresponding ones for release or hold). This method is called when a key was pressed for which no other specific method was found to handle it. The name will be the name of the key, such as "a" or "space". If the shift key is down, the name will be capitalized. If the alt key (option, on a Mac) is down, this will be preceded by a "~" symbol. If the control key is down, there will be a "^" before that. If the meta key (command, on a Mac) is down, there will be a "&" symbol before that. So, a name of "^T" would mean that the T key was pressed with shift and control held down. This method allows you to easily react only to certain combinations of keys and modifiers.

Author:
Russell Zahniser (russell@zahniser.net)

Constructor Summary
KeyInterpreter(Object target, DrawingCanvas canvas)
          Construct a KeyInterpreter that will listen for key events on the given canvas and relay them to the target object.
 
Method Summary
 void addToCanvas(DrawingCanvas canvas)
          Add this KeyInterpreter to a canvas.
 DrawingCanvas getCanvas()
          Return the canvas this listener is attached to.
 void removeFromCanvas()
          Remove this KeyInterpreter from the canvas.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.awt.event.KeyListener
keyPressed, keyReleased, keyTyped
 

Constructor Detail

KeyInterpreter

public KeyInterpreter(Object target,
                      DrawingCanvas canvas)
Construct a KeyInterpreter that will listen for key events on the given canvas and relay them to the target object.

Parameters:
target - Object that will be notified of key events. It should have method such as onSpacePress() for the keys that it is interested in.
canvas - Canvas from which events will be captured.
Method Detail

addToCanvas

public void addToCanvas(DrawingCanvas canvas)
Add this KeyInterpreter to a canvas. It will automatically be removed from whatever canvas it was previously attached to.

Parameters:
canvas - DrawingCanvas to which this KeyInterpreter should listen.

getCanvas

public DrawingCanvas getCanvas()
Return the canvas this listener is attached to.

Returns:
the DrawingCanvas that this interpreter is attached to.

removeFromCanvas

public void removeFromCanvas()
Remove this KeyInterpreter from the canvas. It will no longer detect key events, unless you add it back to a canvas.