Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.awt
Class AWTKeyStroke  view AWTKeyStroke download AWTKeyStroke.java

java.lang.Object
  extended byjava.awt.AWTKeyStroke
All Implemented Interfaces:
java.io.Serializable

public class AWTKeyStroke
extends java.lang.Object
implements java.io.Serializable

This class mirrors KeyEvents, representing both low-level key presses and key releases, and high level key typed inputs. However, this class forms immutable strokes, and can be efficiently reused via the factory methods for creating them.

For backwards compatibility with Swing, this supports a way to build instances of a subclass, using reflection, provided the subclass has a no-arg constructor (of any accessibility).

Since:
1.4

Field Summary
private static java.util.LinkedHashMap cache
          The cache of recently created keystrokes.
private static java.lang.reflect.Constructor ctor
          The no-arg constructor of a subclass, or null to use AWTKeyStroke.
private  char keyChar
          The typed character, or CHAR_UNDEFINED for key presses and releases.
(package private)  int keyCode
          The virtual key code, or VK_UNDEFINED for key typed.
private  int modifiers
          The modifiers in effect.
private static int MODIFIERS_MASK
          The mask for modifiers.
private  boolean onKeyRelease
          True if this is a key release; should only be true if keyChar is CHAR_UNDEFINED.
private static AWTKeyStroke recent
          The most recently generated keystroke, or null.
private static long serialVersionUID
          Compatible with JDK 1.4+.
(package private) static java.util.HashMap vktable
          A table of keyCode names to values.
 
Constructor Summary
protected AWTKeyStroke()
          Construct a keystroke with default values: it will be interpreted as a key typed event with an invalid character and no modifiers.
protected AWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean onKeyRelease)
          Construct a keystroke with the given values.
 
Method Summary
 boolean equals(java.lang.Object o)
          Tests two keystrokes for equality.
private static int extend(int mod)
          Converts the modifiers to the appropriate format.
static AWTKeyStroke getAWTKeyStroke(char keyChar)
          Returns a keystroke representing a typed character.
static AWTKeyStroke getAWTKeyStroke(java.lang.Character keyChar, int modifiers)
          Returns a keystroke representing a typed character with the given modifiers.
private static AWTKeyStroke getAWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean release)
          Gets the appropriate keystroke, creating one if necessary.
static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers)
          Returns a keystroke representing a pressed key event, with the given modifiers.
static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers, boolean release)
          Returns a keystroke representing a pressed or released key event, with the given modifiers.
static AWTKeyStroke getAWTKeyStroke(java.lang.String s)
          Parses a string and returns the keystroke that it represents.
static AWTKeyStroke getAWTKeyStrokeForEvent(java.awt.event.KeyEvent event)
          Returns a keystroke representing what caused the key event.
 char getKeyChar()
          Returns the character of this keystroke, if it was typed.
 int getKeyCode()
          Returns the virtual key code of this keystroke, if it was pressed or released.
 int getKeyEventType()
          Returns the AWT event type of this keystroke.
 int getModifiers()
          Returns the modifiers for this keystroke.
 int hashCode()
          Returns a hashcode for this key event.
 boolean isOnKeyRelease()
          Tests if this keystroke is a key release.
protected  java.lang.Object readResolve()
          Returns a cached version of the deserialized keystroke, if available.
protected static void registerSubclass(java.lang.Class subclass)
          Registers a new subclass as being the type of keystrokes to generate in the factory methods.
 java.lang.String toString()
          Returns a string representation of this keystroke.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Compatible with JDK 1.4+.

See Also:
Constant Field Values

MODIFIERS_MASK

private static final int MODIFIERS_MASK
The mask for modifiers.

See Also:
Constant Field Values

cache

private static final java.util.LinkedHashMap cache
The cache of recently created keystrokes. This maps KeyStrokes to KeyStrokes in a cache which removes the least recently accessed entry, under the assumption that garbage collection of a new keystroke is easy when we find the old one that it matches in the cache.


recent

private static AWTKeyStroke recent
The most recently generated keystroke, or null.


ctor

private static java.lang.reflect.Constructor ctor
The no-arg constructor of a subclass, or null to use AWTKeyStroke. Note that this will be left accessible, to get around private access; but it should not be a security risk as it is highly unlikely that creating protected instances of the subclass via reflection will do much damage.


vktable

static final java.util.HashMap vktable
A table of keyCode names to values. This is package-private to avoid an accessor method.

See Also:
getAWTKeyStroke(String) 55

keyChar

private char keyChar
The typed character, or CHAR_UNDEFINED for key presses and releases.


keyCode

int keyCode
The virtual key code, or VK_UNDEFINED for key typed. Package visible for use by Component.


modifiers

private int modifiers
The modifiers in effect. To match Sun, this stores the old style masks for shift, control, alt, meta, and alt-graph (but not button1); as well as the new style of extended modifiers for all modifiers.


onKeyRelease

private boolean onKeyRelease
True if this is a key release; should only be true if keyChar is CHAR_UNDEFINED.

Constructor Detail

AWTKeyStroke

protected AWTKeyStroke()
Construct a keystroke with default values: it will be interpreted as a key typed event with an invalid character and no modifiers. Client code should use the factory methods instead.


AWTKeyStroke

protected AWTKeyStroke(char keyChar,
                       int keyCode,
                       int modifiers,
                       boolean onKeyRelease)
Construct a keystroke with the given values. Client code should use the factory methods instead.

Method Detail

registerSubclass

protected static void registerSubclass(java.lang.Class subclass)
Registers a new subclass as being the type of keystrokes to generate in the factory methods. This operation flushes the cache of stored keystrokes if the class differs from the current one. The new class must be AWTKeyStroke or a subclass, and must have a no-arg constructor (which may be private).


getAWTKeyStroke

public static AWTKeyStroke getAWTKeyStroke(char keyChar)
Returns a keystroke representing a typed character.


getAWTKeyStroke

public static AWTKeyStroke getAWTKeyStroke(java.lang.Character keyChar,
                                           int modifiers)
Returns a keystroke representing a typed character with the given modifiers. Note that keyChar is a Character instead of a char to avoid accidental ambiguity with getAWTKeyStroke(int, int). The modifiers are the bitwise or of the masks found in java.awt.event.InputEvent; the new style (*_DOWN_MASK) is preferred, but the old style will work.


getAWTKeyStroke

public static AWTKeyStroke getAWTKeyStroke(int keyCode,
                                           int modifiers,
                                           boolean release)
Returns a keystroke representing a pressed or released key event, with the given modifiers. The "virtual key" should be one of the VK_* constants in java.awt.event.KeyEvent. The modifiers are the bitwise or of the masks found in java.awt.event.InputEvent; the new style (*_DOWN_MASK) is preferred, but the old style will work.


getAWTKeyStroke

public static AWTKeyStroke getAWTKeyStroke(int keyCode,
                                           int modifiers)
Returns a keystroke representing a pressed key event, with the given modifiers. The "virtual key" should be one of the VK_* constants in java.awt.event.KeyEvent. The modifiers are the bitwise or of the masks found in java.awt.event.InputEvent; the new style (*_DOWN_MASK) is preferred, but the old style will work.


getAWTKeyStrokeForEvent

public static AWTKeyStroke getAWTKeyStrokeForEvent(java.awt.event.KeyEvent event)
Returns a keystroke representing what caused the key event.


getAWTKeyStroke

public static AWTKeyStroke getAWTKeyStroke(java.lang.String s)
Parses a string and returns the keystroke that it represents. The syntax for keystrokes is listed below, with tokens separated by an arbitrary number of spaces:
 keyStroke := <modifiers>* ( <typedID> | <codeID> )
 modifiers := ( shift | control | ctrl | meta | alt
                | button1 | button2 | button3 )
 typedID := typed <single Unicode character>
 codeID := ( pressed | released )? <name>
 name := <the KeyEvent field name less the leading "VK_">
 

Note that the grammar is rather weak, and not all valid keystrokes can be generated in this manner (for example, a typed space, or anything with the alt-graph modifier!). The output of AWTKeyStroke.toString() will not meet the grammar. If pressed or released is not specified, pressed is assumed. Examples:
"INSERT" => getAWTKeyStroke(KeyEvent.VK_INSERT, 0);
"control DELETE" => getAWTKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
"alt shift X" => getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
"alt shift released X" => getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
"typed a" => getAWTKeyStroke('a');


getKeyChar

public final char getKeyChar()
Returns the character of this keystroke, if it was typed.


getKeyCode

public final int getKeyCode()
Returns the virtual key code of this keystroke, if it was pressed or released. This will be a VK_* constant from KeyEvent.


getModifiers

public final int getModifiers()
Returns the modifiers for this keystroke. This will be a bitwise or of constants from InputEvent; it includes the old style masks for shift, control, alt, meta, and alt-graph (but not button1); as well as the new style of extended modifiers for all modifiers.


isOnKeyRelease

public final boolean isOnKeyRelease()
Tests if this keystroke is a key release.


getKeyEventType

public final int getKeyEventType()
Returns the AWT event type of this keystroke. This is one of KeyEvent.KEY_TYPED>KeyEvent.KEY_TYPED 55 , KeyEvent.KEY_PRESSED>KeyEvent.KEY_PRESSED 55 , or KeyEvent.KEY_RELEASED>KeyEvent.KEY_RELEASED 55 .


hashCode

public int hashCode()
Returns a hashcode for this key event. It is not documented, but appears to be: (getKeyChar() + 1) * (getKeyCode() + 1) * (getModifiers() + 1) * 2 + (isOnKeyRelease() ? 1 : 2).


equals

public final boolean equals(java.lang.Object o)
Tests two keystrokes for equality.


toString

public java.lang.String toString()
Returns a string representation of this keystroke. For typed keystrokes, this is "keyChar " + KeyEvent.getKeyModifiersText(getModifiers()) + getKeyChar(); for pressed and released keystrokes, this is "keyCode " + KeyEvent.getKeyModifiersText(getModifiers()) + KeyEvent.getKeyText(getKeyCode()) + (isOnKeyRelease() ? "-R" : "-P").


readResolve

protected java.lang.Object readResolve()
                                throws java.io.ObjectStreamException
Returns a cached version of the deserialized keystroke, if available.


getAWTKeyStroke

private static AWTKeyStroke getAWTKeyStroke(char keyChar,
                                            int keyCode,
                                            int modifiers,
                                            boolean release)
Gets the appropriate keystroke, creating one if necessary.


extend

private static int extend(int mod)
Converts the modifiers to the appropriate format.