java.awt
Class AWTKeyStroke
- 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).
AWTKeyStroke() - Construct a keystroke with default values: it will be interpreted as a
key typed event with an invalid character and no modifiers.
|
AWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean onKeyRelease) - Construct a keystroke with the given values.
|
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
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.
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 press
getAWTKeyStroke
public static AWTKeyStroke getAWTKeyStroke(char keyChar)
Returns a keystroke representing a typed character.
keyChar
- the typed character
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
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 0
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
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 press
getAWTKeyStroke
public static AWTKeyStroke getAWTKeyStroke(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
InputEvent
; the new style (*_DOWN_MASK)
is preferred, but the old style will work.
keyChar
- the typed charactermodifiers
- the modifiers, or 0
getAWTKeyStroke
public static AWTKeyStroke getAWTKeyStroke(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');
getAWTKeyStrokeForEvent
public static AWTKeyStroke getAWTKeyStrokeForEvent(KeyEvent event)
Returns a keystroke representing what caused the key event.
event
- the key event to convert
- the specified keystroke, or null if the event is invalid
getKeyChar
public final char getKeyChar()
Returns the character of this keystroke, if it was typed.
- the character value, or CHAR_UNDEFINED
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.
- the virtual key code value, or VK_UNDEFINED
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.
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)
.
- hashCode in interface Object
isOnKeyRelease
public final boolean isOnKeyRelease()
Tests if this keystroke is a key release.
- true if this is a key release
registerSubclass
protected static void registerSubclass(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).
subclass
- the new runtime type of generated keystrokes
toString
public 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")
.
- toString in interface Object
AWTKeyStroke.java -- an immutable key stroke
Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.