< prev index next >
src/java.desktop/share/classes/java/awt/AWTKeyStroke.java
Print this page
*** 35,60 ****
import java.lang.reflect.Modifier;
import java.lang.reflect.Field;
import sun.swing.SwingAccessor;
/**
! * An <code>AWTKeyStroke</code> represents a key action on the
! * keyboard, or equivalent input device. <code>AWTKeyStroke</code>s
* can correspond to only a press or release of a
! * particular key, just as <code>KEY_PRESSED</code> and
! * <code>KEY_RELEASED</code> <code>KeyEvent</code>s do;
* alternately, they can correspond to typing a specific Java character, just
! * as <code>KEY_TYPED</code> <code>KeyEvent</code>s do.
! * In all cases, <code>AWTKeyStroke</code>s can specify modifiers
* (alt, shift, control, meta, altGraph, or a combination thereof) which must be present
* during the action for an exact match.
* <p>
! * <code>AWTKeyStrokes</code> are immutable, and are intended
* to be unique. Client code should never create an
! * <code>AWTKeyStroke</code> on its own, but should instead use
! * a variant of <code>getAWTKeyStroke</code>. Client use of these factory
! * methods allows the <code>AWTKeyStroke</code> implementation
* to cache and share instances efficiently.
*
* @see #getAWTKeyStroke
*
* @author Arnaud Weber
--- 35,60 ----
import java.lang.reflect.Modifier;
import java.lang.reflect.Field;
import sun.swing.SwingAccessor;
/**
! * An {@code AWTKeyStroke} represents a key action on the
! * keyboard, or equivalent input device. {@code AWTKeyStroke}s
* can correspond to only a press or release of a
! * particular key, just as {@code KEY_PRESSED} and
! * {@code KEY_RELEASED KeyEvent}s do;
* alternately, they can correspond to typing a specific Java character, just
! * as {@code KEY_TYPED KeyEvent}s do.
! * In all cases, {@code AWTKeyStroke}s can specify modifiers
* (alt, shift, control, meta, altGraph, or a combination thereof) which must be present
* during the action for an exact match.
* <p>
! * {@code AWTKeyStrokes} are immutable, and are intended
* to be unique. Client code should never create an
! * {@code AWTKeyStroke} on its own, but should instead use
! * a variant of {@code getAWTKeyStroke}. Client use of these factory
! * methods allows the {@code AWTKeyStroke} implementation
* to cache and share instances efficiently.
*
* @see #getAWTKeyStroke
*
* @author Arnaud Weber
*** 86,138 ****
/* ensure that the necessary native libraries are loaded */
Toolkit.loadLibraries();
}
/**
! * Constructs an <code>AWTKeyStroke</code> with default values.
* The default values used are:
* <table border="1" summary="AWTKeyStroke default values">
* <tr><th>Property</th><th>Default Value</th></tr>
* <tr>
* <td>Key Char</td>
! * <td><code>KeyEvent.CHAR_UNDEFINED</code></td>
* </tr>
* <tr>
* <td>Key Code</td>
! * <td><code>KeyEvent.VK_UNDEFINED</code></td>
* </tr>
* <tr>
* <td>Modifiers</td>
* <td>none</td>
* </tr>
* <tr>
* <td>On key release?</td>
! * <td><code>false</code></td>
* </tr>
* </table>
*
! * <code>AWTKeyStroke</code>s should not be constructed
! * by client code. Use a variant of <code>getAWTKeyStroke</code>
* instead.
*
* @see #getAWTKeyStroke
*/
protected AWTKeyStroke() {
}
/**
! * Constructs an <code>AWTKeyStroke</code> with the specified
! * values. <code>AWTKeyStroke</code>s should not be constructed
! * by client code. Use a variant of <code>getAWTKeyStroke</code>
* instead.
*
* @param keyChar the character value for a keyboard key
! * @param keyCode the key code for this <code>AWTKeyStroke</code>
* @param modifiers a bitwise-ored combination of any modifiers
! * @param onKeyRelease <code>true</code> if this
! * <code>AWTKeyStroke</code> corresponds
! * to a key release; <code>false</code> otherwise
* @see #getAWTKeyStroke
*/
protected AWTKeyStroke(char keyChar, int keyCode, int modifiers,
boolean onKeyRelease) {
this.keyChar = keyChar;
--- 86,138 ----
/* ensure that the necessary native libraries are loaded */
Toolkit.loadLibraries();
}
/**
! * Constructs an {@code AWTKeyStroke} with default values.
* The default values used are:
* <table border="1" summary="AWTKeyStroke default values">
* <tr><th>Property</th><th>Default Value</th></tr>
* <tr>
* <td>Key Char</td>
! * <td>{@code KeyEvent.CHAR_UNDEFINED}</td>
* </tr>
* <tr>
* <td>Key Code</td>
! * <td>{@code KeyEvent.VK_UNDEFINED}</td>
* </tr>
* <tr>
* <td>Modifiers</td>
* <td>none</td>
* </tr>
* <tr>
* <td>On key release?</td>
! * <td>{@code false}</td>
* </tr>
* </table>
*
! * {@code AWTKeyStroke}s should not be constructed
! * by client code. Use a variant of {@code getAWTKeyStroke}
* instead.
*
* @see #getAWTKeyStroke
*/
protected AWTKeyStroke() {
}
/**
! * Constructs an {@code AWTKeyStroke} with the specified
! * values. {@code AWTKeyStroke}s should not be constructed
! * by client code. Use a variant of {@code getAWTKeyStroke}
* instead.
*
* @param keyChar the character value for a keyboard key
! * @param keyCode the key code for this {@code AWTKeyStroke}
* @param modifiers a bitwise-ored combination of any modifiers
! * @param onKeyRelease {@code true} if this
! * {@code AWTKeyStroke} corresponds
! * to a key release; {@code false} otherwise
* @see #getAWTKeyStroke
*/
protected AWTKeyStroke(char keyChar, int keyCode, int modifiers,
boolean onKeyRelease) {
this.keyChar = keyChar;
*** 183,198 ****
}
return stroke;
}
/**
! * Returns a shared instance of an <code>AWTKeyStroke</code>
! * that represents a <code>KEY_TYPED</code> event for the
* specified character.
*
* @param keyChar the character value for a keyboard key
! * @return an <code>AWTKeyStroke</code> object for that key
*/
public static AWTKeyStroke getAWTKeyStroke(char keyChar) {
return getCachedStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, false);
}
--- 183,198 ----
}
return stroke;
}
/**
! * Returns a shared instance of an {@code AWTKeyStroke}
! * that represents a {@code KEY_TYPED} event for the
* specified character.
*
* @param keyChar the character value for a keyboard key
! * @return an {@code AWTKeyStroke} object for that key
*/
public static AWTKeyStroke getAWTKeyStroke(char keyChar) {
return getCachedStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, false);
}
*** 200,210 ****
* Returns a shared instance of an {@code AWTKeyStroke}
* that represents a {@code KEY_TYPED} event for the
* specified Character object and a set of modifiers. Note
* that the first parameter is of type Character rather than
* char. This is to avoid inadvertent clashes with
! * calls to <code>getAWTKeyStroke(int keyCode, int modifiers)</code>.
*
* The modifiers consist of any combination of following:<ul>
* <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
* <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
* <li>java.awt.event.InputEvent.META_DOWN_MASK
--- 200,210 ----
* Returns a shared instance of an {@code AWTKeyStroke}
* that represents a {@code KEY_TYPED} event for the
* specified Character object and a set of modifiers. Note
* that the first parameter is of type Character rather than
* char. This is to avoid inadvertent clashes with
! * calls to {@code getAWTKeyStroke(int keyCode, int modifiers)}.
*
* The modifiers consist of any combination of following:<ul>
* <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
* <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
* <li>java.awt.event.InputEvent.META_DOWN_MASK
*** 225,237 ****
* them is an integer in which each bit represents a different modifier
* key. Use 0 to specify no modifiers.
*
* @param keyChar the Character object for a keyboard character
* @param modifiers a bitwise-ored combination of any modifiers
! * @return an <code>AWTKeyStroke</code> object for that key
! * @throws IllegalArgumentException if <code>keyChar</code> is
! * <code>null</code>
*
* @see java.awt.event.InputEvent
*/
public static AWTKeyStroke getAWTKeyStroke(Character keyChar, int modifiers)
{
--- 225,237 ----
* them is an integer in which each bit represents a different modifier
* key. Use 0 to specify no modifiers.
*
* @param keyChar the Character object for a keyboard character
* @param modifiers a bitwise-ored combination of any modifiers
! * @return an {@code AWTKeyStroke} object for that key
! * @throws IllegalArgumentException if {@code keyChar} is
! * {@code null}
*
* @see java.awt.event.InputEvent
*/
public static AWTKeyStroke getAWTKeyStroke(Character keyChar, int modifiers)
{
*** 241,263 ****
return getCachedStroke(keyChar.charValue(), KeyEvent.VK_UNDEFINED,
modifiers, false);
}
/**
! * Returns a shared instance of an <code>AWTKeyStroke</code>,
* given a numeric key code and a set of modifiers, specifying
* whether the key is activated when it is pressed or released.
* <p>
* The "virtual key" constants defined in
! * <code>java.awt.event.KeyEvent</code> can be
* used to specify the key code. For example:<ul>
! * <li><code>java.awt.event.KeyEvent.VK_ENTER</code>
! * <li><code>java.awt.event.KeyEvent.VK_TAB</code>
! * <li><code>java.awt.event.KeyEvent.VK_SPACE</code>
* </ul>
* Alternatively, the key code may be obtained by calling
! * <code>java.awt.event.KeyEvent.getExtendedKeyCodeForChar</code>.
*
* The modifiers consist of any combination of:<ul>
* <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
* <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
* <li>java.awt.event.InputEvent.META_DOWN_MASK
--- 241,263 ----
return getCachedStroke(keyChar.charValue(), KeyEvent.VK_UNDEFINED,
modifiers, false);
}
/**
! * Returns a shared instance of an {@code AWTKeyStroke},
* given a numeric key code and a set of modifiers, specifying
* whether the key is activated when it is pressed or released.
* <p>
* The "virtual key" constants defined in
! * {@code java.awt.event.KeyEvent} can be
* used to specify the key code. For example:<ul>
! * <li>{@code java.awt.event.KeyEvent.VK_ENTER}
! * <li>{@code java.awt.event.KeyEvent.VK_TAB}
! * <li>{@code java.awt.event.KeyEvent.VK_SPACE}
* </ul>
* Alternatively, the key code may be obtained by calling
! * {@code java.awt.event.KeyEvent.getExtendedKeyCodeForChar}.
*
* The modifiers consist of any combination of:<ul>
* <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
* <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
* <li>java.awt.event.InputEvent.META_DOWN_MASK
*** 277,288 ****
* them is an integer in which each bit represents a different modifier
* key. Use 0 to specify no modifiers.
*
* @param keyCode an int specifying the numeric code for a keyboard key
* @param modifiers a bitwise-ored combination of any modifiers
! * @param onKeyRelease <code>true</code> if the <code>AWTKeyStroke</code>
! * should represent a key release; <code>false</code> otherwise
* @return an AWTKeyStroke object for that key
*
* @see java.awt.event.KeyEvent
* @see java.awt.event.InputEvent
*/
--- 277,288 ----
* them is an integer in which each bit represents a different modifier
* key. Use 0 to specify no modifiers.
*
* @param keyCode an int specifying the numeric code for a keyboard key
* @param modifiers a bitwise-ored combination of any modifiers
! * @param onKeyRelease {@code true} if the {@code AWTKeyStroke}
! * should represent a key release; {@code false} otherwise
* @return an AWTKeyStroke object for that key
*
* @see java.awt.event.KeyEvent
* @see java.awt.event.InputEvent
*/
*** 291,310 ****
return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers,
onKeyRelease);
}
/**
! * Returns a shared instance of an <code>AWTKeyStroke</code>,
* given a numeric key code and a set of modifiers. The returned
! * <code>AWTKeyStroke</code> will correspond to a key press.
* <p>
* The "virtual key" constants defined in
! * <code>java.awt.event.KeyEvent</code> can be
* used to specify the key code. For example:<ul>
! * <li><code>java.awt.event.KeyEvent.VK_ENTER</code>
! * <li><code>java.awt.event.KeyEvent.VK_TAB</code>
! * <li><code>java.awt.event.KeyEvent.VK_SPACE</code>
* </ul>
* The modifiers consist of any combination of:<ul>
* <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
* <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
* <li>java.awt.event.InputEvent.META_DOWN_MASK
--- 291,310 ----
return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers,
onKeyRelease);
}
/**
! * Returns a shared instance of an {@code AWTKeyStroke},
* given a numeric key code and a set of modifiers. The returned
! * {@code AWTKeyStroke} will correspond to a key press.
* <p>
* The "virtual key" constants defined in
! * {@code java.awt.event.KeyEvent} can be
* used to specify the key code. For example:<ul>
! * <li>{@code java.awt.event.KeyEvent.VK_ENTER}
! * <li>{@code java.awt.event.KeyEvent.VK_TAB}
! * <li>{@code java.awt.event.KeyEvent.VK_SPACE}
* </ul>
* The modifiers consist of any combination of:<ul>
* <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
* <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
* <li>java.awt.event.InputEvent.META_DOWN_MASK
*** 324,356 ****
* them is an integer in which each bit represents a different modifier
* key. Use 0 to specify no modifiers.
*
* @param keyCode an int specifying the numeric code for a keyboard key
* @param modifiers a bitwise-ored combination of any modifiers
! * @return an <code>AWTKeyStroke</code> object for that key
*
* @see java.awt.event.KeyEvent
* @see java.awt.event.InputEvent
*/
public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers) {
return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers,
false);
}
/**
! * Returns an <code>AWTKeyStroke</code> which represents the
! * stroke which generated a given <code>KeyEvent</code>.
* <p>
! * This method obtains the keyChar from a <code>KeyTyped</code>
! * event, and the keyCode from a <code>KeyPressed</code> or
! * <code>KeyReleased</code> event. The <code>KeyEvent</code> modifiers are
! * obtained for all three types of <code>KeyEvent</code>.
! *
! * @param anEvent the <code>KeyEvent</code> from which to
! * obtain the <code>AWTKeyStroke</code>
! * @throws NullPointerException if <code>anEvent</code> is null
! * @return the <code>AWTKeyStroke</code> that precipitated the event
*/
public static AWTKeyStroke getAWTKeyStrokeForEvent(KeyEvent anEvent) {
int id = anEvent.getID();
switch(id) {
case KeyEvent.KEY_PRESSED:
--- 324,356 ----
* them is an integer in which each bit represents a different modifier
* key. Use 0 to specify no modifiers.
*
* @param keyCode an int specifying the numeric code for a keyboard key
* @param modifiers a bitwise-ored combination of any modifiers
! * @return an {@code AWTKeyStroke} object for that key
*
* @see java.awt.event.KeyEvent
* @see java.awt.event.InputEvent
*/
public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers) {
return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers,
false);
}
/**
! * Returns an {@code AWTKeyStroke} which represents the
! * stroke which generated a given {@code KeyEvent}.
* <p>
! * This method obtains the keyChar from a {@code KeyTyped}
! * event, and the keyCode from a {@code KeyPressed} or
! * {@code KeyReleased} event. The {@code KeyEvent} modifiers are
! * obtained for all three types of {@code KeyEvent}.
! *
! * @param anEvent the {@code KeyEvent} from which to
! * obtain the {@code AWTKeyStroke}
! * @throws NullPointerException if {@code anEvent} is null
! * @return the {@code AWTKeyStroke} that precipitated the event
*/
public static AWTKeyStroke getAWTKeyStrokeForEvent(KeyEvent anEvent) {
int id = anEvent.getID();
switch(id) {
case KeyEvent.KEY_PRESSED:
*** 369,379 ****
return null;
}
}
/**
! * Parses a string and returns an <code>AWTKeyStroke</code>.
* The string must have the following syntax:
* <pre>
* <modifiers>* (<typedID> | <pressedReleasedID>)
*
* modifiers := shift | control | ctrl | meta | alt | altGraph
--- 369,379 ----
return null;
}
}
/**
! * Parses a string and returns an {@code AWTKeyStroke}.
* The string must have the following syntax:
* <pre>
* <modifiers>* (<typedID> | <pressedReleasedID>)
*
* modifiers := shift | control | ctrl | meta | alt | altGraph
*** 391,402 ****
* "alt shift released X" => getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
* "typed a" => getAWTKeyStroke('a');
* </pre>
*
* @param s a String formatted as described above
! * @return an <code>AWTKeyStroke</code> object for that String
! * @throws IllegalArgumentException if <code>s</code> is <code>null</code>,
* or is formatted incorrectly
*/
public static AWTKeyStroke getAWTKeyStroke(String s) {
if (s == null) {
throw new IllegalArgumentException("String cannot be null");
--- 391,402 ----
* "alt shift released X" => getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
* "typed a" => getAWTKeyStroke('a');
* </pre>
*
* @param s a String formatted as described above
! * @return an {@code AWTKeyStroke} object for that String
! * @throws IllegalArgumentException if {@code s} is {@code null},
* or is formatted incorrectly
*/
public static AWTKeyStroke getAWTKeyStroke(String s) {
if (s == null) {
throw new IllegalArgumentException("String cannot be null");
*** 498,509 ****
}
return vks;
}
/**
* Returns the integer constant for the KeyEvent.VK field named
! * <code>key</code>. This will throw an
! * <code>IllegalArgumentException</code> if <code>key</code> is
* not a valid constant.
*/
private static int getVKValue(String key) {
VKCollection vkCollect = getVKCollection();
--- 498,509 ----
}
return vks;
}
/**
* Returns the integer constant for the KeyEvent.VK field named
! * {@code key}. This will throw an
! * {@code IllegalArgumentException} if {@code key} is
* not a valid constant.
*/
private static int getVKValue(String key) {
VKCollection vkCollect = getVKCollection();
*** 525,583 ****
}
return value.intValue();
}
/**
! * Returns the character for this <code>AWTKeyStroke</code>.
*
* @return a char value
* @see #getAWTKeyStroke(char)
* @see KeyEvent#getKeyChar
*/
public final char getKeyChar() {
return keyChar;
}
/**
! * Returns the numeric key code for this <code>AWTKeyStroke</code>.
*
* @return an int containing the key code value
* @see #getAWTKeyStroke(int,int)
* @see KeyEvent#getKeyCode
*/
public final int getKeyCode() {
return keyCode;
}
/**
! * Returns the modifier keys for this <code>AWTKeyStroke</code>.
*
* @return an int containing the modifiers
* @see #getAWTKeyStroke(int,int)
*/
public final int getModifiers() {
return modifiers;
}
/**
! * Returns whether this <code>AWTKeyStroke</code> represents a key release.
*
! * @return <code>true</code> if this <code>AWTKeyStroke</code>
! * represents a key release; <code>false</code> otherwise
* @see #getAWTKeyStroke(int,int,boolean)
*/
public final boolean isOnKeyRelease() {
return onKeyRelease;
}
/**
! * Returns the type of <code>KeyEvent</code> which corresponds to
! * this <code>AWTKeyStroke</code>.
*
! * @return <code>KeyEvent.KEY_PRESSED</code>,
! * <code>KeyEvent.KEY_TYPED</code>,
! * or <code>KeyEvent.KEY_RELEASED</code>
* @see java.awt.event.KeyEvent
*/
public final int getKeyEventType() {
if (keyCode == KeyEvent.VK_UNDEFINED) {
return KeyEvent.KEY_TYPED;
--- 525,583 ----
}
return value.intValue();
}
/**
! * Returns the character for this {@code AWTKeyStroke}.
*
* @return a char value
* @see #getAWTKeyStroke(char)
* @see KeyEvent#getKeyChar
*/
public final char getKeyChar() {
return keyChar;
}
/**
! * Returns the numeric key code for this {@code AWTKeyStroke}.
*
* @return an int containing the key code value
* @see #getAWTKeyStroke(int,int)
* @see KeyEvent#getKeyCode
*/
public final int getKeyCode() {
return keyCode;
}
/**
! * Returns the modifier keys for this {@code AWTKeyStroke}.
*
* @return an int containing the modifiers
* @see #getAWTKeyStroke(int,int)
*/
public final int getModifiers() {
return modifiers;
}
/**
! * Returns whether this {@code AWTKeyStroke} represents a key release.
*
! * @return {@code true} if this {@code AWTKeyStroke}
! * represents a key release; {@code false} otherwise
* @see #getAWTKeyStroke(int,int,boolean)
*/
public final boolean isOnKeyRelease() {
return onKeyRelease;
}
/**
! * Returns the type of {@code KeyEvent} which corresponds to
! * this {@code AWTKeyStroke}.
*
! * @return {@code KeyEvent.KEY_PRESSED},
! * {@code KeyEvent.KEY_TYPED},
! * or {@code KeyEvent.KEY_RELEASED}
* @see java.awt.event.KeyEvent
*/
public final int getKeyEventType() {
if (keyCode == KeyEvent.VK_UNDEFINED) {
return KeyEvent.KEY_TYPED;
*** 615,626 ****
return false;
}
/**
* Returns a string that displays and identifies this object's properties.
! * The <code>String</code> returned by this method can be passed
! * as a parameter to <code>getAWTKeyStroke(String)</code> to produce
* a key stroke equal to this key stroke.
*
* @return a String representation of this object
* @see #getAWTKeyStroke(String)
*/
--- 615,626 ----
return false;
}
/**
* Returns a string that displays and identifies this object's properties.
! * The {@code String} returned by this method can be passed
! * as a parameter to {@code getAWTKeyStroke(String)} to produce
* a key stroke equal to this key stroke.
*
* @return a String representation of this object
* @see #getAWTKeyStroke(String)
*/
*** 693,704 ****
}
return "UNKNOWN";
}
/**
! * Returns a cached instance of <code>AWTKeyStroke</code> (or a subclass of
! * <code>AWTKeyStroke</code>) which is equal to this instance.
*
* @return a cached instance which is equal to this instance
* @throws java.io.ObjectStreamException if a serialization problem occurs
*/
protected Object readResolve() throws java.io.ObjectStreamException {
--- 693,704 ----
}
return "UNKNOWN";
}
/**
! * Returns a cached instance of {@code AWTKeyStroke} (or a subclass of
! * {@code AWTKeyStroke}) which is equal to this instance.
*
* @return a cached instance which is equal to this instance
* @throws java.io.ObjectStreamException if a serialization problem occurs
*/
protected Object readResolve() throws java.io.ObjectStreamException {
< prev index next >