Overview | Package | Class | Tree | Deprecated | Index | Help Java Platform
1.1.7
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD

Class com.sun.java.swing.KeyStroke

java.lang.Object
  |
  +--com.sun.java.swing.KeyStroke

public class KeyStroke
extends java.lang.Object
implements java.io.Serializable
A KeyStroke instance represents a key being typed on the keyboard -- it contains both a char code for the key and a modifier (alt, shift, ctrl, meta, or a combination).

KeyStroke objects are used to define high-level (semantic) action events. Instead of trapping every keystroke and throwing away the ones you are not interested in, those keystrokes you care about automatically initiate actions on the components they are registered with.

KeyStroke objects handle both character-code generating keystrokes you would trap with a KeyTyped event handler and key-code generating keystrokes (like Enter or F1) that you would trap with a KeyPressed event handler.

KeyStroke objects are immutable and unique.

All KeyStroke objects are cached. To get one, use getKeyStroke.

Warning: serialized objects of this class will not be compatible with future swing releases. The current serialization support is appropriate for short term storage or RMI between Swing1.0 applications. It will not be possible to load serialized Swing1.0 objects with future releases of Swing. The JDK1.2 release of Swing will be the compatibility baseline for the serialized form of Swing objects.

See Also:
registerKeyboardAction, getKeyStroke, Serialized Form

Method Summary
boolean equals(java.lang.Object anObject)
          Returns true if this object is identical to the specified object.
char getKeyChar()
           
int getKeyCode()
           
static KeyStroke getKeyStrokeForEvent(java.awt.event.KeyEvent anEvent)
          Return a keystroke from an event.
static KeyStroke getKeyStroke(char keyChar)
          Return a shared instance of a key stroke that is activated when the key is pressed.
static KeyStroke getKeyStroke(char keyChar, boolean onKeyRelease)
          Return a shared instance of a key stroke, specifying whether the key is considered to be activated when it is pressed or when it is released.
static KeyStroke getKeyStroke(int keyCode, int modifiers, boolean onKeyRelease)
          Return a shared instance of a key stroke given a char code and a set of modifiers, specifying whether the key is activated when it is pressed or released.
static KeyStroke getKeyStroke(int keyCode, int modifiers)
          Return a shared instance of a key stroke given a char code and a set of modifiers -- the key is activated when it is pressed.
static KeyStroke getKeyStroke(java.lang.String representation)
          Return a shared instance of a key stroke matching a string representation.
int getModifiers()
           
int hashCode()
          Returns a numeric value for this object that is likely to be reasonably unique, so it can be used as the index value in a Hashtable.
boolean isOnKeyRelease()
           
java.lang.String toString()
          Returns a string that displays and identifies this object's properties.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notifyAll, notify, toString, wait, wait, wait
 

Method Detail

getKeyStroke

public static KeyStroke getKeyStroke(char keyChar)
Return a shared instance of a key stroke that is activated when the key is pressed.
Parameters:
keyChar - the character value for a keyboard key
Returns:
a KeyStroke object for that key

getKeyStroke

public static KeyStroke getKeyStroke(char keyChar,
                                     boolean onKeyRelease)
Return a shared instance of a key stroke, specifying whether the key is considered to be activated when it is pressed or when it is released.
Parameters:
keyChar - the character value for a keyboard key
onKeyRelease - a boolean value. When true, specifies that the key is active when it is released.
Returns:
a KeyStroke object for that key

getKeyStroke

public static KeyStroke getKeyStroke(int keyCode,
                                     int modifiers,
                                     boolean onKeyRelease)
Return a shared instance of a key stroke given a char code and a set of modifiers, specifying whether the key is activated when it is pressed or released.

The "virtual key" constants defined in java.awt.event.KeyEvent can be used to specify the key code. For example:

The modifiers consist of any combination of: Since these numbers are all different powers of two, any combination of them is an integer in which each bit represents a different modifier key.
Parameters:
keyCode - an int specifying the numeric code for a keyboard key
modifiers - an int specifying any combination of the key modifiers.
onKeyRelease - a boolean value. When true, specifies that the key is active when it is released.
Returns:
a KeyStroke object for that key
See Also:
java.awt.event.KeyEvent, java.awt.Event

getKeyStroke

public static KeyStroke getKeyStroke(int keyCode,
                                     int modifiers)
Return a shared instance of a key stroke given a char code and a set of modifiers -- the key is activated when it is pressed.

The modifiers consist of any combination of:

Since these numbers are all different powers of two, any combination of them is an integer in which each bit represents a different modifier key.
Parameters:
keyChar - the character value for a keyboard key
modifiers - an int specifying any combination of the key modifiers.
Returns:
a KeyStroke object for that key

getKeyStrokeForEvent

public static KeyStroke getKeyStrokeForEvent(java.awt.event.KeyEvent anEvent)
Return a keystroke from an event.

This method obtains the keyChar from a KeyTyped event, and the keyCode from a KeyPressed or KeyReleased event, so you don't have to.

Parameters:
anEvent - the KeyEvent to obtain the KeyStroke from
Returns:
the KeyStroke that precipitated the event

getKeyStroke

public static KeyStroke getKeyStroke(java.lang.String representation)
Return a shared instance of a key stroke matching a string representation.
Parameters:
representation - a String specifying a KeyStroke
Returns:
a KeyStroke object matching the specification.

getKeyChar

public char getKeyChar()

getKeyCode

public int getKeyCode()

getModifiers

public int getModifiers()

isOnKeyRelease

public boolean isOnKeyRelease()

hashCode

public int hashCode()
Returns a numeric value for this object that is likely to be reasonably unique, so it can be used as the index value in a Hashtable.
Returns:
an int that "represents" this object
Overrides:
hashCode in class java.lang.Object
See Also:
java.util.Hashtable

equals

public boolean equals(java.lang.Object anObject)
Returns true if this object is identical to the specified object.
Parameters:
anObject - the Object to compare this object to
Returns:
true if the objects are identical
Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Returns a string that displays and identifies this object's properties.
Returns:
a String representation of this object
Overrides:
toString in class java.lang.Object

Overview | Package | Class | Tree | Deprecated | Index | Help Java Platform
1.1.7
PREV CLASS | NEXT CLASS FRAMES  | NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD DETAIL:  FIELD | CONSTR | METHOD

Submit a bug or feature
Submit comments/suggestions about javadoc
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.