hmi.util
Class KeyState

java.lang.Object
  extended by hmi.util.KeyState
All Implemented Interfaces:
FocusListener, KeyListener, EventListener

public class KeyState
extends Object
implements KeyListener, FocusListener

KeyState is a KeyListener, that keeps track of the current key states So, it converts the Java event based interface for the keyboard into a state based interface. The current state of keys is available in the form of a public array "keyDown": keyDown[keycode] is true as long as that key is down, i.e. pressed and not yet released. Note that several keys can be down at the same time. Also, it records the state for "raw" key codes, i.e. a modifier key like the SHIFT key means that keyDown[KeyEvent.VK_SHIFT] is true, but does not modify the key code for other keys. Only keycodes in the range 0 .. KEYCODES are recorded. In practice, this includes all keys on a standard keyboard, but it does not include all Unicode keys. In addition to keyDown, there is an int array keyLocation, which records the keyboard location of a key, while it is down. (Distinguishes between KEY_LOCATION_LEFT , KEY_LOCATION_RIGHT, KEY_LOCATION_NUMPAD etc).

Author:
Job Zwiers

Field Summary
static int KEYCODES
           
private  boolean[] keyDown
           
private  int[] keyLocation
           
 
Constructor Summary
KeyState()
          create a new KeyState.
KeyState(Component c)
          creates a KeyState that listens to Component c for KeyEvents
 
Method Summary
 void focusGained(FocusEvent e)
           
 void focusLost(FocusEvent e)
           
 boolean isKeyDown(int keyCode)
          returns the current value of keyDown[keyCode]
 void keyPressed(KeyEvent evt)
          keyPressed handler from the KeyListener interface for this tracker.
 void keyReleased(KeyEvent evt)
          keyReleased handler from the KeyListener interface for this tracker.
 void keyTyped(KeyEvent evt)
          keyTyped handler from the KeyListener interface: ignored.
 String locationString(int location)
          produces a descriptive String for a key location: Left, Right, Numpad, S(tandard), or U(nknown).
private  void reset()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEYCODES

public static final int KEYCODES
See Also:
Constant Field Values

keyDown

private boolean[] keyDown

keyLocation

private int[] keyLocation
Constructor Detail

KeyState

public KeyState()
create a new KeyState.


KeyState

public KeyState(Component c)
creates a KeyState that listens to Component c for KeyEvents

Method Detail

keyPressed

public final void keyPressed(KeyEvent evt)
keyPressed handler from the KeyListener interface for this tracker. updates the keyDown and keyLocation arrays

Specified by:
keyPressed in interface KeyListener

keyReleased

public final void keyReleased(KeyEvent evt)
keyReleased handler from the KeyListener interface for this tracker. updates the keyDown and keyLocation arrays

Specified by:
keyReleased in interface KeyListener

keyTyped

public final void keyTyped(KeyEvent evt)
keyTyped handler from the KeyListener interface: ignored.

Specified by:
keyTyped in interface KeyListener

isKeyDown

public boolean isKeyDown(int keyCode)
returns the current value of keyDown[keyCode]


locationString

public String locationString(int location)
produces a descriptive String for a key location: Left, Right, Numpad, S(tandard), or U(nknown).


reset

private void reset()

focusGained

public void focusGained(FocusEvent e)
Specified by:
focusGained in interface FocusListener

focusLost

public void focusLost(FocusEvent e)
Specified by:
focusLost in interface FocusListener