jdk/src/share/classes/javax/swing/KeyStroke.java
author dmarkov
Wed, 16 Apr 2014 12:51:25 +0400
changeset 24184 4da2f6ec4dab
parent 22574 7f8ce0c8c20a
child 25201 4adc75e0c4e5
permissions -rw-r--r--
8032874: ArrayIndexOutOfBoundsException in JTable while clearing data in JTable Reviewed-by: alexp, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2473
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2473
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2473
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2473
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2473
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.AWTKeyStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A KeyStroke represents a key action on the keyboard, or equivalent input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * device. KeyStrokes can correspond to only a press or release of a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * key, just as KEY_PRESSED and KEY_RELEASED KeyEvents do; alternately, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * can correspond to typing a specific Java character, just as KEY_TYPED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * KeyEvents do. In all cases, KeyStrokes can specify modifiers (alt, shift,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * control, meta, altGraph, or a combination thereof) which must be present during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * action for an exact match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * KeyStrokes are used to define high-level (semantic) action events. Instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * of trapping every keystroke and throwing away the ones you are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * interested in, those keystrokes you care about automatically initiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * actions on the Components with which they are registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * KeyStrokes are immutable, and are intended to be unique. Client code cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * create a KeyStroke; a variant of <code>getKeyStroke</code> must be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * instead. These factory methods allow the KeyStroke implementation to cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * and share instances efficiently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20157
diff changeset
    54
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see javax.swing.text.Keymap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see #getKeyStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    64
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class KeyStroke extends AWTKeyStroke {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Serial Version ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final long serialVersionUID = -9060180771037902530L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private KeyStroke() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private KeyStroke(char keyChar, int keyCode, int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                      boolean onKeyRelease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        super(keyChar, keyCode, modifiers, onKeyRelease);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Returns a shared instance of a <code>KeyStroke</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * that represents a <code>KEY_TYPED</code> event for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * specified character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param keyChar the character value for a keyboard key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @return a KeyStroke object for that key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static KeyStroke getKeyStroke(char keyChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        synchronized (AWTKeyStroke.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            registerSubclass(KeyStroke.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return (KeyStroke)getAWTKeyStroke(keyChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Returns an instance of a KeyStroke, specifying whether the key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * considered to be activated when it is pressed or released. Unlike all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * other factory methods in this class, the instances returned by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * method are not necessarily cached or shared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param keyChar the character value for a keyboard key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param onKeyRelease <code>true</code> if this KeyStroke corresponds to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *        key release; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return a KeyStroke object for that key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @deprecated use getKeyStroke(char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static KeyStroke getKeyStroke(char keyChar, boolean onKeyRelease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return new KeyStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, onKeyRelease);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Returns a shared instance of a {@code KeyStroke}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * that represents a {@code KEY_TYPED} event for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * specified Character object and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      * set of modifiers. Note that the first parameter is of type Character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * rather than char. This is to avoid inadvertent clashes with calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <code>getKeyStroke(int keyCode, int modifiers)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The modifiers consist of any combination of following:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <li>java.awt.event.InputEvent.META_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <li>java.awt.event.InputEvent.ALT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * The old modifiers listed below also can be used, but they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * mapped to _DOWN_ modifiers. <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <li>java.awt.event.InputEvent.SHIFT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <li>java.awt.event.InputEvent.CTRL_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <li>java.awt.event.InputEvent.META_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <li>java.awt.event.InputEvent.ALT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <li>java.awt.event.InputEvent.ALT_GRAPH_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * also can be used, but they are mapped to _DOWN_ modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Since these numbers are all different powers of two, any combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * them is an integer in which each bit represents a different modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * key. Use 0 to specify no modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param keyChar the Character object for a keyboard character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param modifiers a bitwise-ored combination of any modifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return an KeyStroke object for that key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws IllegalArgumentException if keyChar is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see java.awt.event.InputEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static KeyStroke getKeyStroke(Character keyChar, int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        synchronized (AWTKeyStroke.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            registerSubclass(KeyStroke.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return (KeyStroke)getAWTKeyStroke(keyChar, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Returns a shared instance of a KeyStroke, given a numeric key code and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * set of modifiers, specifying whether the key is activated when it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * pressed or released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * The "virtual key" constants defined in java.awt.event.KeyEvent can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * used to specify the key code. For example:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <li>java.awt.event.KeyEvent.VK_ENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <li>java.awt.event.KeyEvent.VK_TAB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <li>java.awt.event.KeyEvent.VK_SPACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * </ul>
2473
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
   166
     * Alternatively, the key code may be obtained by calling
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
   167
     * <code>java.awt.event.KeyEvent.getExtendedKeyCodeForChar</code>.
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
   168
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * The modifiers consist of any combination of:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <li>java.awt.event.InputEvent.META_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <li>java.awt.event.InputEvent.ALT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * The old modifiers <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <li>java.awt.event.InputEvent.SHIFT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <li>java.awt.event.InputEvent.CTRL_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <li>java.awt.event.InputEvent.META_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <li>java.awt.event.InputEvent.ALT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <li>java.awt.event.InputEvent.ALT_GRAPH_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * also can be used, but they are mapped to _DOWN_ modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Since these numbers are all different powers of two, any combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * them is an integer in which each bit represents a different modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * key. Use 0 to specify no modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param keyCode an int specifying the numeric code for a keyboard key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param modifiers a bitwise-ored combination of any modifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param onKeyRelease <code>true</code> if the KeyStroke should represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *        a key release; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return a KeyStroke object for that key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @see java.awt.event.InputEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public static KeyStroke getKeyStroke(int keyCode, int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                         boolean onKeyRelease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        synchronized (AWTKeyStroke.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            registerSubclass(KeyStroke.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return (KeyStroke)getAWTKeyStroke(keyCode, modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                              onKeyRelease);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Returns a shared instance of a KeyStroke, given a numeric key code and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * set of modifiers. The returned KeyStroke will correspond to a key press.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * The "virtual key" constants defined in java.awt.event.KeyEvent can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * used to specify the key code. For example:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <li>java.awt.event.KeyEvent.VK_ENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <li>java.awt.event.KeyEvent.VK_TAB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <li>java.awt.event.KeyEvent.VK_SPACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * </ul>
2473
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
   217
     * Alternatively, the key code may be obtained by calling
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
   218
     * <code>java.awt.event.KeyEvent.getExtendedKeyCodeForChar</code>.
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
   219
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * The modifiers consist of any combination of:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <li>java.awt.event.InputEvent.META_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <li>java.awt.event.InputEvent.ALT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * The old modifiers <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <li>java.awt.event.InputEvent.SHIFT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <li>java.awt.event.InputEvent.CTRL_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <li>java.awt.event.InputEvent.META_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <li>java.awt.event.InputEvent.ALT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <li>java.awt.event.InputEvent.ALT_GRAPH_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * also can be used, but they are mapped to _DOWN_ modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Since these numbers are all different powers of two, any combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * them is an integer in which each bit represents a different modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * key. Use 0 to specify no modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param keyCode an int specifying the numeric code for a keyboard key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param modifiers a bitwise-ored combination of any modifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return a KeyStroke object for that key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @see java.awt.event.InputEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public static KeyStroke getKeyStroke(int keyCode, int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        synchronized (AWTKeyStroke.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            registerSubclass(KeyStroke.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return (KeyStroke)getAWTKeyStroke(keyCode, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns a KeyStroke which represents the stroke which generated a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * KeyEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * This method obtains the keyChar from a KeyTyped event, and the keyCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * from a KeyPressed or KeyReleased event. The KeyEvent modifiers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * obtained for all three types of KeyEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param anEvent the KeyEvent from which to obtain the KeyStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws NullPointerException if <code>anEvent</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return the KeyStroke that precipitated the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static KeyStroke getKeyStrokeForEvent(KeyEvent anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        synchronized (AWTKeyStroke.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            registerSubclass(KeyStroke.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return (KeyStroke)getAWTKeyStrokeForEvent(anEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Parses a string and returns a <code>KeyStroke</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * The string must have the following syntax:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *    &lt;modifiers&gt;* (&lt;typedID&gt; | &lt;pressedReleasedID&gt;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *    modifiers := shift | control | ctrl | meta | alt | altGraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *    typedID := typed &lt;typedKey&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *    typedKey := string of length 1 giving Unicode character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *    pressedReleasedID := (pressed | released) key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *    key := KeyEvent key code name, i.e. the name following "VK_".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * If typed, pressed or released is not specified, pressed is assumed. Here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * are some examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <pre>
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   288
     *     "INSERT" =&gt; getKeyStroke(KeyEvent.VK_INSERT, 0);
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   289
     *     "control DELETE" =&gt; getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   290
     *     "alt shift X" =&gt; getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   291
     *     "alt shift released X" =&gt; getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   292
     *     "typed a" =&gt; getKeyStroke('a');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * In order to maintain backward-compatibility, specifying a null String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * or a String which is formatted incorrectly, returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param s a String formatted as described above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return a KeyStroke object for that String, or null if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *         String is null, or is formatted incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public static KeyStroke getKeyStroke(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (s == null || s.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        synchronized (AWTKeyStroke.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            registerSubclass(KeyStroke.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                return (KeyStroke)getAWTKeyStroke(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
}