hmi.util
Class InputState

java.lang.Object
  extended by hmi.util.InputState
All Implemented Interfaces:
KeyListener, MouseListener, EventListener

public class InputState
extends Object
implements KeyListener, MouseListener

An InputState tracks the current state of the keyboard and mouse, by listening to AWT InputEvents for AWT/Swing Components. The key/mouse button state (pressed or released) is tracked, and used to determine which of a set of defined key patterns is currently "active" Such a pattern is itself defined by a set of keys that must be pressed, and a set of keys that are required to be released. Keys can be specified by means of virtual key codes, possibly combined with a keyboard location (like LEFT, RIGHT, NUMPAD).

Author:
Job Zwiers

Nested Class Summary
static class InputState.Key
          A InputState.Key defines a virtual key code and a key location.
 class InputState.KeyPat
          standard implementation of InputState.Pattern.
static interface InputState.Pattern
          The interface for key patterns: A pattern can be currently active or not, which can be checked by calling the isActive method.
 
Field Summary
static int A
           
private  boolean button1Down
           
private  boolean button2Down
           
private  boolean button3Down
           
private  int clickCount1
           
private  int clickCount2
           
private  int clickCount3
           
private static int KEYDIM
           
private  int[] keyDown
           
static int KEYLISTENER
          various listener modes; they can be combined by means of the | operator
static int L
          Constants for referring to keyboard locations.
private  int listenerMode
           
static int MB1
           
static int MB2
           
static int MB3
           
static int MOUSELISTENER
           
static int N
           
private  int[] navKey
           
static int NR_OF_KEYCODES
           
private static int NR_OF_MOUSE_BUTTONS
           
private  ArrayList<InputState.Pattern> patterns
           
static int R
           
static int S
           
private  int x
           
private  int x1Pressed
           
private  int x1Released
           
private  int x2Pressed
           
private  int x2Released
           
private  int x3Pressed
           
private  int x3Released
           
private  int y
           
private  int y1Pressed
           
private  int y1Released
           
private  int y2Pressed
           
private  int y2Released
           
private  int y3Pressed
           
private  int y3Released
           
 
Constructor Summary
InputState()
          Creates a new InputState, not yet listening to any Component.
InputState(Component c)
          Create a new InputState and adds it as a KeyListener to the specified Component.
InputState(Component c, int listenerMode)
           
 
Method Summary
 InputState.Pattern addPattern(InputState.Key key)
          Adds a key pattern, defined by a single key which must be down for activation.
 InputState.Pattern addPattern(InputState.Key[] down)
          Adds a key pattern, consisting of keys required to be down.
 InputState.Pattern addPattern(InputState.Key[] down, InputState.Key[] up)
          Adds a key pattern, consisting of keys required to be down, and keys required to be up.
 InputState.Pattern addPattern(int[] down, int[] up)
          Adds a key pattern, consisting of keys required to be down, and keys required to be up.
private  void classify()
           
 void detachFrom(Component c)
          removes the listeners from Component c
 int getClickCount1()
           
 int getClickCount2()
           
 int getClickCount3()
           
 int getX1pressed()
           
 int getX1released()
           
 int getX2pressed()
           
 int getX2released()
           
 int getX3pressed()
           
 int getX3released()
           
 int getY1pressed()
           
 int getY1released()
           
 int getY2pressed()
           
 int getY2released()
           
 int getY3pressed()
           
 int getY3released()
           
 boolean isButton1Down()
           
 boolean isButton2Down()
           
 boolean isButton3Down()
           
static InputState.Key key(int vkCode)
          Creates a InputState.Key, defined by a virtual key code.
static InputState.Key key(int vkCode, int locationMask)
          Creates a InputState.Key consisting of virtual key code and keylocation.
 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 required by the KeyListener interface. ketTyped calls are simply ignored.
static InputState.Key left(int vkCode)
          Creates a InputState.Key, defined by a virtual key code.
 void listenTo(Component c)
          adds listeners to Component c for all enabled events, according to the listenerMode
 String locationString(int location)
          produces a descriptive String for a key location: Left, Right, Numpad, S(tandard), or U(nknown).
 void mouseClicked(MouseEvent e)
          mouseClicked handler from the MouseListener interface for this tracker: ignored
 void mouseDragged(MouseEvent e)
          mouseDragged handler from the MouseMotionListener interface for this tracker.
 void mouseEntered(MouseEvent e)
          mouseEntered handler from the MouseListener interface for this tracker: ignored
 void mouseExited(MouseEvent e)
          mouseExited handler from the MouseListener interface for this tracker: ignored
 void mouseMoved(MouseEvent e)
          mouseMoved handler from the MouseMotionListener interface for this tracker.
 void mousePressed(MouseEvent e)
          mousePressed handler from the MouseListener interface for this tracker.
 void mouseReleased(MouseEvent e)
          mouseReleased handler from the MouseListener interface for this tracker.
 void mouseWheelMoved(MouseWheelEvent e)
          mouseDragged handler from the MouseWheelListener interface for this tracker.
static InputState.Key numpad(int vkCode)
          Creates a InputState.Key, defined by a virtual key code.
 InputState.Pattern orPattern(InputState.Pattern[] pats)
          Create a new key patterns from a given list of patterns., It is active iff at least one of the patterns from the list is active.
static InputState.Key right(int vkCode)
          Creates a InputState.Key, defined by a virtual key code.
static InputState.Key standard(int vkCode)
          Creates a InputState.Key, defined by a virtual key code.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NR_OF_KEYCODES

public static final int NR_OF_KEYCODES
See Also:
Constant Field Values

NR_OF_MOUSE_BUTTONS

private static final int NR_OF_MOUSE_BUTTONS
See Also:
Constant Field Values

KEYDIM

private static final int KEYDIM
See Also:
Constant Field Values

MB1

public static final int MB1
See Also:
Constant Field Values

MB2

public static final int MB2
See Also:
Constant Field Values

MB3

public static final int MB3
See Also:
Constant Field Values

L

public static final int L
Constants for referring to keyboard locations.

See Also:
Constant Field Values

R

public static final int R
See Also:
Constant Field Values

S

public static final int S
See Also:
Constant Field Values

N

public static final int N
See Also:
Constant Field Values

A

public static final int A
See Also:
Constant Field Values

navKey

private int[] navKey

keyDown

private int[] keyDown

KEYLISTENER

public static final int KEYLISTENER
various listener modes; they can be combined by means of the | operator

See Also:
Constant Field Values

MOUSELISTENER

public static final int MOUSELISTENER
See Also:
Constant Field Values

listenerMode

private int listenerMode

button1Down

private boolean button1Down

button2Down

private boolean button2Down

button3Down

private boolean button3Down

clickCount1

private int clickCount1

clickCount2

private int clickCount2

clickCount3

private int clickCount3

x1Pressed

private int x1Pressed

y1Pressed

private int y1Pressed

x1Released

private int x1Released

y1Released

private int y1Released

x2Pressed

private int x2Pressed

y2Pressed

private int y2Pressed

x2Released

private int x2Released

y2Released

private int y2Released

x3Pressed

private int x3Pressed

y3Pressed

private int y3Pressed

x3Released

private int x3Released

y3Released

private int y3Released

x

private int x

y

private int y

patterns

private ArrayList<InputState.Pattern> patterns
Constructor Detail

InputState

public InputState()
Creates a new InputState, not yet listening to any Component.


InputState

public InputState(Component c)
Create a new InputState and adds it as a KeyListener to the specified Component.


InputState

public InputState(Component c,
                  int listenerMode)
Method Detail

isButton1Down

public boolean isButton1Down()

isButton2Down

public boolean isButton2Down()

isButton3Down

public boolean isButton3Down()

getClickCount1

public int getClickCount1()

getClickCount2

public int getClickCount2()

getClickCount3

public int getClickCount3()

getX1pressed

public int getX1pressed()

getX2pressed

public int getX2pressed()

getX3pressed

public int getX3pressed()

getY1pressed

public int getY1pressed()

getY2pressed

public int getY2pressed()

getY3pressed

public int getY3pressed()

getX1released

public int getX1released()

getX2released

public int getX2released()

getX3released

public int getX3released()

getY1released

public int getY1released()

getY2released

public int getY2released()

getY3released

public int getY3released()

listenTo

public void listenTo(Component c)
adds listeners to Component c for all enabled events, according to the listenerMode


detachFrom

public void detachFrom(Component c)
removes the listeners from Component c


addPattern

public InputState.Pattern addPattern(int[] down,
                                     int[] up)
Adds a key pattern, consisting of keys required to be down, and keys required to be up. Key locations are not specified. Each of down and up can be null.


addPattern

public InputState.Pattern addPattern(InputState.Key[] down,
                                     InputState.Key[] up)
Adds a key pattern, consisting of keys required to be down, and keys required to be up. Keys are specified by means of InputState.Key elements, defining both a virtual key code as well as a keyboard location (-mask).


addPattern

public InputState.Pattern addPattern(InputState.Key[] down)
Adds a key pattern, consisting of keys required to be down.


addPattern

public InputState.Pattern addPattern(InputState.Key key)
Adds a key pattern, defined by a single key which must be down for activation. The Key specifies both a virtual key code and a keyboar


orPattern

public InputState.Pattern orPattern(InputState.Pattern[] pats)
Create a new key patterns from a given list of patterns., It is active iff at least one of the patterns from the list is active.


key

public static InputState.Key key(int vkCode,
                                 int locationMask)
Creates a InputState.Key consisting of virtual key code and keylocation. The latter can be one of S (standrad), N (numpad), L (left), R (right), or an "or-ed" combination of these.


key

public static InputState.Key key(int vkCode)
Creates a InputState.Key, defined by a virtual key code. The location mask is "A"


numpad

public static InputState.Key numpad(int vkCode)
Creates a InputState.Key, defined by a virtual key code. The location mask is "N", denoting a numpad key location.


left

public static InputState.Key left(int vkCode)
Creates a InputState.Key, defined by a virtual key code. The location mask is "L", denoting a "left" key location.


right

public static InputState.Key right(int vkCode)
Creates a InputState.Key, defined by a virtual key code. The location mask is "R", denoting a "right" key location.


standard

public static InputState.Key standard(int vkCode)
Creates a InputState.Key, defined by a virtual key code. The location mask is "S", denoting a "standard" key location.


classify

private void classify()

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

mouseClicked

public final void mouseClicked(MouseEvent e)
mouseClicked handler from the MouseListener interface for this tracker: ignored

Specified by:
mouseClicked in interface MouseListener

mouseEntered

public final void mouseEntered(MouseEvent e)
mouseEntered handler from the MouseListener interface for this tracker: ignored

Specified by:
mouseEntered in interface MouseListener

mouseExited

public final void mouseExited(MouseEvent e)
mouseExited handler from the MouseListener interface for this tracker: ignored

Specified by:
mouseExited in interface MouseListener

mousePressed

public final void mousePressed(MouseEvent e)
mousePressed handler from the MouseListener interface for this tracker. updates the buttonDown[123] states, the [xy][123]Pressed positions, and the clickCount[123] counters

Specified by:
mousePressed in interface MouseListener

mouseReleased

public final void mouseReleased(MouseEvent e)
mouseReleased handler from the MouseListener interface for this tracker. updates the buttonDown[123] states, the [xy][123]Released positions, and the clickCount[123] counters

Specified by:
mouseReleased in interface MouseListener

mouseDragged

public final void mouseDragged(MouseEvent e)
mouseDragged handler from the MouseMotionListener interface for this tracker. updates the xDragged and yDragged position.


mouseMoved

public final void mouseMoved(MouseEvent e)
mouseMoved handler from the MouseMotionListener interface for this tracker. updates the xMoved and yMoved position.


mouseWheelMoved

public final void mouseWheelMoved(MouseWheelEvent e)
mouseDragged handler from the MouseWheelListener interface for this tracker. updates the wheelRotation and accumulatedWheelRotation counters.


keyTyped

public final void keyTyped(KeyEvent evt)
keyTyped handler required by the KeyListener interface. ketTyped calls are simply ignored. (keyTyped really means keyPressed followed by keyReleased, and the latter two calls are handled)

Specified by:
keyTyped in interface KeyListener

locationString

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