public class AWTKeyStroke extends Object implements Serializable
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).
getAWTKeyStroke(char)
,
Serialized FormModifier | Constructor and Description |
---|---|
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.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o)
Tests two keystrokes for equality.
|
static AWTKeyStroke |
getAWTKeyStroke(char keyChar)
Returns a keystroke representing a typed character.
|
static AWTKeyStroke |
getAWTKeyStroke(Character keyChar,
int modifiers)
Returns a keystroke representing a typed character with the given
modifiers.
|
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(String s)
Parses a string and returns the keystroke that it represents.
|
static AWTKeyStroke |
getAWTKeyStrokeForEvent(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 Object |
readResolve()
Returns a cached version of the deserialized keystroke, if available.
|
protected static void |
registerSubclass(Class<?> subclass)
Registers a new subclass as being the type of keystrokes to generate in
the factory methods.
|
String |
toString()
Returns a string representation of this keystroke.
|
protected AWTKeyStroke()
protected AWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean onKeyRelease)
keyChar
- the character entered, if this is a key typedkeyCode
- the key pressed or released, or VK_UNDEFINED for key typedmodifiers
- the modifier keys for the keystroke, in old or new styleonKeyRelease
- true if this is a key release instead of a pressgetAWTKeyStroke(char)
,
getAWTKeyStroke(Character, int)
,
getAWTKeyStroke(int, int, boolean)
,
getAWTKeyStroke(int, int)
,
getAWTKeyStrokeForEvent(KeyEvent)
,
getAWTKeyStroke(String)
protected static void registerSubclass(Class<?> subclass)
subclass
- the new runtime type of generated keystrokesIllegalArgumentException
- subclass doesn't have no-arg constructorClassCastException
- subclass doesn't extend AWTKeyStrokepublic static AWTKeyStroke getAWTKeyStroke(char keyChar)
keyChar
- the typed characterpublic static AWTKeyStroke getAWTKeyStroke(Character keyChar, int modifiers)
Character
instead of a
char
to avoid accidental ambiguity with
getAWTKeyStroke(int, int)
. The modifiers are the bitwise
or of the masks found in InputEvent
; the new style (*_DOWN_MASK)
is preferred, but the old style will work.keyChar
- the typed charactermodifiers
- the modifiers, or 0IllegalArgumentException
- if keyChar is nullpublic static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers, boolean release)
KeyEvent
. The modifiers are the bitwise or of the
masks found in InputEvent
; the new style (*_DOWN_MASK) is
preferred, but the old style will work.keyCode
- the virtual keymodifiers
- the modifiers, or 0release
- true if this is a key release instead of a key presspublic static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers)
KeyEvent
. The modifiers are the bitwise or of the masks found
in InputEvent
; the new style (*_DOWN_MASK) is preferred, but the
old style will work.keyCode
- the virtual keymodifiers
- the modifiers, or 0public static AWTKeyStroke getAWTKeyStrokeForEvent(KeyEvent event)
event
- the key event to convertNullPointerException
- if event is nullpublic static AWTKeyStroke getAWTKeyStroke(String s)
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');
s
- the string to parseIllegalArgumentException
- if s is null or cannot be parsedpublic final char getKeyChar()
getAWTKeyStroke(char)
public final int getKeyCode()
getAWTKeyStroke(int, int)
public final int getModifiers()
getAWTKeyStroke(Character, int)
,
getAWTKeyStroke(int, int)
public final boolean isOnKeyRelease()
getAWTKeyStroke(int, int, boolean)
public final int getKeyEventType()
KeyEvent.KEY_TYPED
, KeyEvent.KEY_PRESSED
, or
KeyEvent.KEY_RELEASED
.public int hashCode()
(getKeyChar() + 1) * (getKeyCode() + 1)
* (getModifiers() + 1) * 2 + (isOnKeyRelease() ? 1 : 2)
.hashCode
in class Object
Object.equals(Object)
,
System.identityHashCode(Object)
public final boolean equals(Object o)
equals
in class Object
o
- the object to testObject.hashCode()
public String toString()
"keyChar " + KeyEvent.getKeyModifiersText(getModifiers())
+ getKeyChar()
; for pressed and released keystrokes, this is
"keyCode " + KeyEvent.getKeyModifiersText(getModifiers())
+ KeyEvent.getKeyText(getKeyCode())
+ (isOnKeyRelease() ? "-R" : "-P")
.toString
in class Object
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)
protected Object readResolve() throws ObjectStreamException
ObjectStreamException
- if something goes wrong