jdk/src/java.desktop/share/classes/java/awt/AWTKeyStroke.java
author alexsch
Wed, 24 Aug 2016 00:23:49 +0400
changeset 40719 4ae72a69bd3b
parent 35667 ed476aba94de
child 43722 25ba19c20260
permissions -rw-r--r--
8129854: Remove reflection from AWT/Swing classes Reviewed-by: serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20172
diff changeset
     2
 * Copyright (c) 2000, 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 java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.event.KeyEvent;
10911
6816ffc67934 7019773: AWTKeyStroke.ctor is a mutable static
denis
parents: 5506
diff changeset
    28
import sun.awt.AppContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.InputEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Field;
32867
90051b1c4e3d 8133453: Deprecate AWTKeyStroke.registerSubclass(Class) method
alexsch
parents: 26749
diff changeset
    37
import sun.swing.SwingAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    40
 * An {@code AWTKeyStroke} represents a key action on the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    41
 * keyboard, or equivalent input device. {@code AWTKeyStroke}s
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * can correspond to only a press or release of a
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    43
 * particular key, just as {@code KEY_PRESSED} and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    44
 * {@code KEY_RELEASED KeyEvent}s do;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * alternately, they can correspond to typing a specific Java character, just
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    46
 * as {@code KEY_TYPED KeyEvent}s do.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    47
 * In all cases, {@code AWTKeyStroke}s can specify modifiers
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * (alt, shift, control, meta, altGraph, or a combination thereof) which must be present
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * during the action for an exact match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    51
 * {@code AWTKeyStrokes} are immutable, and are intended
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * to be unique. Client code should never create an
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    53
 * {@code AWTKeyStroke} on its own, but should instead use
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    54
 * a variant of {@code getAWTKeyStroke}. Client use of these factory
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    55
 * methods allows the {@code AWTKeyStroke} implementation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * to cache and share instances efficiently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see #getAWTKeyStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class AWTKeyStroke implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final long serialVersionUID = -6430539691155161871L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 11502
diff changeset
    67
    private static Map<String, Integer> modifierKeywords;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Associates VK_XXX (as a String) with code (as Integer). This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * done to avoid the overhead of the reflective call to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static VKCollection vks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
10911
6816ffc67934 7019773: AWTKeyStroke.ctor is a mutable static
denis
parents: 5506
diff changeset
    75
    //A key for the collection of AWTKeyStrokes within AppContext.
6816ffc67934 7019773: AWTKeyStroke.ctor is a mutable static
denis
parents: 5506
diff changeset
    76
    private static Object APP_CONTEXT_CACHE_KEY = new Object();
6816ffc67934 7019773: AWTKeyStroke.ctor is a mutable static
denis
parents: 5506
diff changeset
    77
    //A key withing the cache
6816ffc67934 7019773: AWTKeyStroke.ctor is a mutable static
denis
parents: 5506
diff changeset
    78
    private static AWTKeyStroke APP_CONTEXT_KEYSTROKE_KEY = new AWTKeyStroke();
6816ffc67934 7019773: AWTKeyStroke.ctor is a mutable static
denis
parents: 5506
diff changeset
    79
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private char keyChar = KeyEvent.CHAR_UNDEFINED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private int keyCode = KeyEvent.VK_UNDEFINED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private int modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private boolean onKeyRelease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    91
     * Constructs an {@code AWTKeyStroke} with default values.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The default values used are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <table border="1" summary="AWTKeyStroke default values">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <tr><th>Property</th><th>Default Value</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *    <td>Key Char</td>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
    97
     *    <td>{@code KeyEvent.CHAR_UNDEFINED}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *    <td>Key Code</td>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   101
     *    <td>{@code KeyEvent.VK_UNDEFINED}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *    <td>Modifiers</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *    <td>none</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *    <td>On key release?</td>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   109
     *    <td>{@code false}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   113
     * {@code AWTKeyStroke}s should not be constructed
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   114
     * by client code. Use a variant of {@code getAWTKeyStroke}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @see #getAWTKeyStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    protected AWTKeyStroke() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   123
     * Constructs an {@code AWTKeyStroke} with the specified
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   124
     * values. {@code AWTKeyStroke}s should not be constructed
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   125
     * by client code. Use a variant of {@code getAWTKeyStroke}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param keyChar the character value for a keyboard key
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   129
     * @param keyCode the key code for this {@code AWTKeyStroke}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param modifiers a bitwise-ored combination of any modifiers
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   131
     * @param onKeyRelease {@code true} if this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   132
     *        {@code AWTKeyStroke} corresponds
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   133
     *        to a key release; {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see #getAWTKeyStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected AWTKeyStroke(char keyChar, int keyCode, int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                           boolean onKeyRelease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.keyChar = keyChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.keyCode = keyCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.modifiers = modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this.onKeyRelease = onKeyRelease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
32867
90051b1c4e3d 8133453: Deprecate AWTKeyStroke.registerSubclass(Class) method
alexsch
parents: 26749
diff changeset
   145
     * The method has no effect and is only left present to avoid introducing
90051b1c4e3d 8133453: Deprecate AWTKeyStroke.registerSubclass(Class) method
alexsch
parents: 26749
diff changeset
   146
     * a binary incompatibility.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param subclass the new Class of which the factory methods should create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *        instances
32867
90051b1c4e3d 8133453: Deprecate AWTKeyStroke.registerSubclass(Class) method
alexsch
parents: 26749
diff changeset
   150
     * @deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
32867
90051b1c4e3d 8133453: Deprecate AWTKeyStroke.registerSubclass(Class) method
alexsch
parents: 26749
diff changeset
   152
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    protected static void registerSubclass(Class<?> subclass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static synchronized AWTKeyStroke getCachedStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        (char keyChar, int keyCode, int modifiers, boolean onKeyRelease)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    {
24549
147a5c8b7793 8039109: Fix unchecked and raw lint warnings in java.awt
darcy
parents: 22584
diff changeset
   159
        @SuppressWarnings("unchecked")
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 11502
diff changeset
   160
        Map<AWTKeyStroke, AWTKeyStroke> cache = (Map)AppContext.getAppContext().get(APP_CONTEXT_CACHE_KEY);
10911
6816ffc67934 7019773: AWTKeyStroke.ctor is a mutable static
denis
parents: 5506
diff changeset
   161
        AWTKeyStroke cacheKey = (AWTKeyStroke)AppContext.getAppContext().get(APP_CONTEXT_KEYSTROKE_KEY);
6816ffc67934 7019773: AWTKeyStroke.ctor is a mutable static
denis
parents: 5506
diff changeset
   162
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (cache == null) {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 11502
diff changeset
   164
            cache = new HashMap<>();
10911
6816ffc67934 7019773: AWTKeyStroke.ctor is a mutable static
denis
parents: 5506
diff changeset
   165
            AppContext.getAppContext().put(APP_CONTEXT_CACHE_KEY, cache);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (cacheKey == null) {
32867
90051b1c4e3d 8133453: Deprecate AWTKeyStroke.registerSubclass(Class) method
alexsch
parents: 26749
diff changeset
   169
            cacheKey = SwingAccessor.getKeyStrokeAccessor().create();
90051b1c4e3d 8133453: Deprecate AWTKeyStroke.registerSubclass(Class) method
alexsch
parents: 26749
diff changeset
   170
            AppContext.getAppContext().put(APP_CONTEXT_KEYSTROKE_KEY, cacheKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
32867
90051b1c4e3d 8133453: Deprecate AWTKeyStroke.registerSubclass(Class) method
alexsch
parents: 26749
diff changeset
   172
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        cacheKey.keyChar = keyChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        cacheKey.keyCode = keyCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        cacheKey.modifiers = mapNewModifiers(mapOldModifiers(modifiers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        cacheKey.onKeyRelease = onKeyRelease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20172
diff changeset
   178
        AWTKeyStroke stroke = cache.get(cacheKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (stroke == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            stroke = cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            cache.put(stroke, stroke);
10911
6816ffc67934 7019773: AWTKeyStroke.ctor is a mutable static
denis
parents: 5506
diff changeset
   182
            AppContext.getAppContext().remove(APP_CONTEXT_KEYSTROKE_KEY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return stroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   188
     * Returns a shared instance of an {@code AWTKeyStroke}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   189
     * that represents a {@code KEY_TYPED} event for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * specified character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param keyChar the character value for a keyboard key
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   193
     * @return an {@code AWTKeyStroke} object for that key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public static AWTKeyStroke getAWTKeyStroke(char keyChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return getCachedStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Returns a shared instance of an {@code AWTKeyStroke}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * that represents a {@code KEY_TYPED} event for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * specified Character object and a set of modifiers. Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * that the first parameter is of type Character rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * char. This is to avoid inadvertent clashes with
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   205
     * calls to {@code getAWTKeyStroke(int keyCode, int modifiers)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * The modifiers consist of any combination of following:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <li>java.awt.event.InputEvent.META_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <li>java.awt.event.InputEvent.ALT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * The old modifiers listed below also can be used, but they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * mapped to _DOWN_ modifiers. <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <li>java.awt.event.InputEvent.SHIFT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <li>java.awt.event.InputEvent.CTRL_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <li>java.awt.event.InputEvent.META_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <li>java.awt.event.InputEvent.ALT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <li>java.awt.event.InputEvent.ALT_GRAPH_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * also can be used, but they are mapped to _DOWN_ modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Since these numbers are all different powers of two, any combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * them is an integer in which each bit represents a different modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * key. Use 0 to specify no modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param keyChar the Character object for a keyboard character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param modifiers a bitwise-ored combination of any modifiers
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   230
     * @return an {@code AWTKeyStroke} object for that key
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   231
     * @throws IllegalArgumentException if {@code keyChar} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   232
     *       {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @see java.awt.event.InputEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public static AWTKeyStroke getAWTKeyStroke(Character keyChar, int modifiers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (keyChar == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throw new IllegalArgumentException("keyChar cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return getCachedStroke(keyChar.charValue(), KeyEvent.VK_UNDEFINED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                               modifiers, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   246
     * Returns a shared instance of an {@code AWTKeyStroke},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * given a numeric key code and a set of modifiers, specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * whether the key is activated when it is pressed or released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * The "virtual key" constants defined in
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   251
     * {@code java.awt.event.KeyEvent} can be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * used to specify the key code. For example:<ul>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   253
     * <li>{@code java.awt.event.KeyEvent.VK_ENTER}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   254
     * <li>{@code java.awt.event.KeyEvent.VK_TAB}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   255
     * <li>{@code java.awt.event.KeyEvent.VK_SPACE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * </ul>
2473
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
   257
     * Alternatively, the key code may be obtained by calling
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   258
     * {@code java.awt.event.KeyEvent.getExtendedKeyCodeForChar}.
2473
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 2
diff changeset
   259
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * The modifiers consist of any combination of:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <li>java.awt.event.InputEvent.META_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <li>java.awt.event.InputEvent.ALT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * The old modifiers <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <li>java.awt.event.InputEvent.SHIFT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <li>java.awt.event.InputEvent.CTRL_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <li>java.awt.event.InputEvent.META_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <li>java.awt.event.InputEvent.ALT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <li>java.awt.event.InputEvent.ALT_GRAPH_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * also can be used, but they are mapped to _DOWN_ modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Since these numbers are all different powers of two, any combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * them is an integer in which each bit represents a different modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * key. Use 0 to specify no modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param keyCode an int specifying the numeric code for a keyboard key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param modifiers a bitwise-ored combination of any modifiers
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   282
     * @param onKeyRelease {@code true} if the {@code AWTKeyStroke}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   283
     *        should represent a key release; {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return an AWTKeyStroke object for that key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @see java.awt.event.InputEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                               boolean onKeyRelease) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                               onKeyRelease);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   296
     * Returns a shared instance of an {@code AWTKeyStroke},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * given a numeric key code and a set of modifiers. The returned
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   298
     * {@code AWTKeyStroke} will correspond to a key press.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * The "virtual key" constants defined in
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   301
     * {@code java.awt.event.KeyEvent} can be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * used to specify the key code. For example:<ul>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   303
     * <li>{@code java.awt.event.KeyEvent.VK_ENTER}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   304
     * <li>{@code java.awt.event.KeyEvent.VK_TAB}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   305
     * <li>{@code java.awt.event.KeyEvent.VK_SPACE}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * The modifiers consist of any combination of:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <li>java.awt.event.InputEvent.CTRL_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <li>java.awt.event.InputEvent.META_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <li>java.awt.event.InputEvent.ALT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * The old modifiers <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <li>java.awt.event.InputEvent.SHIFT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <li>java.awt.event.InputEvent.CTRL_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <li>java.awt.event.InputEvent.META_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <li>java.awt.event.InputEvent.ALT_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <li>java.awt.event.InputEvent.ALT_GRAPH_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * also can be used, but they are mapped to _DOWN_ modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Since these numbers are all different powers of two, any combination of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * them is an integer in which each bit represents a different modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * key. Use 0 to specify no modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param keyCode an int specifying the numeric code for a keyboard key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param modifiers a bitwise-ored combination of any modifiers
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   329
     * @return an {@code AWTKeyStroke} object for that key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @see java.awt.event.InputEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                               false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   340
     * Returns an {@code AWTKeyStroke} which represents the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   341
     * stroke which generated a given {@code KeyEvent}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   343
     * This method obtains the keyChar from a {@code KeyTyped}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   344
     * event, and the keyCode from a {@code KeyPressed} or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   345
     * {@code KeyReleased} event. The {@code KeyEvent} modifiers are
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   346
     * obtained for all three types of {@code KeyEvent}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   348
     * @param anEvent the {@code KeyEvent} from which to
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   349
     *      obtain the {@code AWTKeyStroke}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   350
     * @throws NullPointerException if {@code anEvent} is null
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   351
     * @return the {@code AWTKeyStroke} that precipitated the event
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public static AWTKeyStroke getAWTKeyStrokeForEvent(KeyEvent anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int id = anEvent.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        switch(id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
          case KeyEvent.KEY_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
          case KeyEvent.KEY_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return getCachedStroke(KeyEvent.CHAR_UNDEFINED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                   anEvent.getKeyCode(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                   anEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                   (id == KeyEvent.KEY_RELEASED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
          case KeyEvent.KEY_TYPED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return getCachedStroke(anEvent.getKeyChar(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                   KeyEvent.VK_UNDEFINED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                   anEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                   false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // Invalid ID for this KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   374
     * Parses a string and returns an {@code AWTKeyStroke}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * The string must have the following syntax:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *    &lt;modifiers&gt;* (&lt;typedID&gt; | &lt;pressedReleasedID&gt;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *    modifiers := shift | control | ctrl | meta | alt | altGraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *    typedID := typed &lt;typedKey&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *    typedKey := string of length 1 giving Unicode character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *    pressedReleasedID := (pressed | released) key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *    key := KeyEvent key code name, i.e. the name following "VK_".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * If typed, pressed or released is not specified, pressed is assumed. Here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * are some examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <pre>
20172
f48935a247ec 8025218: [javadoc] some errors in java/awt classes
yan
parents: 20107
diff changeset
   388
     *     "INSERT" =&gt; getAWTKeyStroke(KeyEvent.VK_INSERT, 0);
f48935a247ec 8025218: [javadoc] some errors in java/awt classes
yan
parents: 20107
diff changeset
   389
     *     "control DELETE" =&gt; getAWTKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
f48935a247ec 8025218: [javadoc] some errors in java/awt classes
yan
parents: 20107
diff changeset
   390
     *     "alt shift X" =&gt; getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
f48935a247ec 8025218: [javadoc] some errors in java/awt classes
yan
parents: 20107
diff changeset
   391
     *     "alt shift released X" =&gt; getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
f48935a247ec 8025218: [javadoc] some errors in java/awt classes
yan
parents: 20107
diff changeset
   392
     *     "typed a" =&gt; getAWTKeyStroke('a');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param s a String formatted as described above
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   396
     * @return an {@code AWTKeyStroke} object for that String
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   397
     * @throws IllegalArgumentException if {@code s} is {@code null},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *        or is formatted incorrectly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public static AWTKeyStroke getAWTKeyStroke(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            throw new IllegalArgumentException("String cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        final String errmsg = "String formatted incorrectly";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        StringTokenizer st = new StringTokenizer(s, " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        int mask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        boolean released = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        boolean typed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        boolean pressed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        synchronized (AWTKeyStroke.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (modifierKeywords == null) {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 11502
diff changeset
   416
                Map<String, Integer> uninitializedMap = new HashMap<>(8, 1.0f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                uninitializedMap.put("shift",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                     Integer.valueOf(InputEvent.SHIFT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                                     |InputEvent.SHIFT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                uninitializedMap.put("control",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                     Integer.valueOf(InputEvent.CTRL_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                                                     |InputEvent.CTRL_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                uninitializedMap.put("ctrl",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                     Integer.valueOf(InputEvent.CTRL_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                                     |InputEvent.CTRL_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                uninitializedMap.put("meta",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                                     Integer.valueOf(InputEvent.META_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                                                     |InputEvent.META_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                uninitializedMap.put("alt",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                                     Integer.valueOf(InputEvent.ALT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                                     |InputEvent.ALT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                uninitializedMap.put("altGraph",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                     Integer.valueOf(InputEvent.ALT_GRAPH_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                                                     |InputEvent.ALT_GRAPH_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                uninitializedMap.put("button1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                                     Integer.valueOf(InputEvent.BUTTON1_DOWN_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                uninitializedMap.put("button2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                                     Integer.valueOf(InputEvent.BUTTON2_DOWN_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                uninitializedMap.put("button3",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                                     Integer.valueOf(InputEvent.BUTTON3_DOWN_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                modifierKeywords =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    Collections.synchronizedMap(uninitializedMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        int count = st.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        for (int i = 1; i <= count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            String token = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            if (typed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (token.length() != 1 || i != count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    throw new IllegalArgumentException(errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                return getCachedStroke(token.charAt(0), KeyEvent.VK_UNDEFINED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                       mask, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (pressed || released || i == count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                if (i != count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    throw new IllegalArgumentException(errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                String keyCodeName = "VK_" + token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                int keyCode = getVKValue(keyCodeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                       mask, released);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (token.equals("released")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                released = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (token.equals("pressed")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                pressed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (token.equals("typed")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                typed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20172
diff changeset
   484
            Integer tokenMask = modifierKeywords.get(token);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (tokenMask != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                mask |= tokenMask.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                throw new IllegalArgumentException(errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        throw new IllegalArgumentException(errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    private static VKCollection getVKCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (vks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            vks = new VKCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        return vks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Returns the integer constant for the KeyEvent.VK field named
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   503
     * {@code key}. This will throw an
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   504
     * {@code IllegalArgumentException} if {@code key} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * not a valid constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    private static int getVKValue(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        VKCollection vkCollect = getVKCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        Integer value = vkCollect.findCode(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            int keyCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            final String errmsg = "String formatted incorrectly";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                keyCode = KeyEvent.class.getField(key).getInt(KeyEvent.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            } catch (NoSuchFieldException nsfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                throw new IllegalArgumentException(errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                throw new IllegalArgumentException(errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            value = Integer.valueOf(keyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            vkCollect.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return value.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   530
     * Returns the character for this {@code AWTKeyStroke}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @return a char value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @see #getAWTKeyStroke(char)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @see KeyEvent#getKeyChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    public final char getKeyChar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        return keyChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   541
     * Returns the numeric key code for this {@code AWTKeyStroke}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @return an int containing the key code value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @see #getAWTKeyStroke(int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @see KeyEvent#getKeyCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public final int getKeyCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return keyCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   552
     * Returns the modifier keys for this {@code AWTKeyStroke}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @return an int containing the modifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @see #getAWTKeyStroke(int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    public final int getModifiers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        return modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   562
     * Returns whether this {@code AWTKeyStroke} represents a key release.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   564
     * @return {@code true} if this {@code AWTKeyStroke}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   565
     *          represents a key release; {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @see #getAWTKeyStroke(int,int,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public final boolean isOnKeyRelease() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return onKeyRelease;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   573
     * Returns the type of {@code KeyEvent} which corresponds to
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   574
     * this {@code AWTKeyStroke}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   576
     * @return {@code KeyEvent.KEY_PRESSED},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   577
     *         {@code KeyEvent.KEY_TYPED},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   578
     *         or {@code KeyEvent.KEY_RELEASED}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    public final int getKeyEventType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (keyCode == KeyEvent.VK_UNDEFINED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            return KeyEvent.KEY_TYPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return (onKeyRelease)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                ? KeyEvent.KEY_RELEASED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                : KeyEvent.KEY_PRESSED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Returns a numeric value for this object that is likely to be unique,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * making it a good choice as the index value in a hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @return an int that represents this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        return (((int)keyChar) + 1) * (2 * (keyCode + 1)) * (modifiers + 1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            (onKeyRelease ? 1 : 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * Returns true if this object is identical to the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @param anObject the Object to compare this object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @return true if the objects are identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public final boolean equals(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if (anObject instanceof AWTKeyStroke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            AWTKeyStroke ks = (AWTKeyStroke)anObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            return (ks.keyChar == keyChar && ks.keyCode == keyCode &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    ks.onKeyRelease == onKeyRelease &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    ks.modifiers == modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * Returns a string that displays and identifies this object's properties.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   620
     * The {@code String} returned by this method can be passed
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   621
     * as a parameter to {@code getAWTKeyStroke(String)} to produce
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * a key stroke equal to this key stroke.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @return a String representation of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @see #getAWTKeyStroke(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (keyCode == KeyEvent.VK_UNDEFINED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            return getModifiersText(modifiers) + "typed " + keyChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            return getModifiersText(modifiers) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                (onKeyRelease ? "released" : "pressed") + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                getVKText(keyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    static String getModifiersText(int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            buf.append("shift ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            buf.append("ctrl ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if ((modifiers & InputEvent.META_DOWN_MASK) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            buf.append("meta ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            buf.append("alt ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            buf.append("altGraph ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if ((modifiers & InputEvent.BUTTON1_DOWN_MASK) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            buf.append("button1 ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if ((modifiers & InputEvent.BUTTON2_DOWN_MASK) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            buf.append("button2 ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if ((modifiers & InputEvent.BUTTON3_DOWN_MASK) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            buf.append("button3 ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    static String getVKText(int keyCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        VKCollection vkCollect = getVKCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        Integer key = Integer.valueOf(keyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        String name = vkCollect.findName(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return name.substring(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        int expected_modifiers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        Field[] fields = KeyEvent.class.getDeclaredFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        for (int i = 0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                if (fields[i].getModifiers() == expected_modifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    && fields[i].getType() == Integer.TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    && fields[i].getName().startsWith("VK_")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    && fields[i].getInt(KeyEvent.class) == keyCode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    name = fields[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    vkCollect.put(name, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    return name.substring(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        return "UNKNOWN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   698
     * Returns a cached instance of {@code AWTKeyStroke} (or a subclass of
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32867
diff changeset
   699
     * {@code AWTKeyStroke}) which is equal to this instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @return a cached instance which is equal to this instance
25162
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 24549
diff changeset
   702
     * @throws java.io.ObjectStreamException if a serialization problem occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    protected Object readResolve() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        synchronized (AWTKeyStroke.class) {
32867
90051b1c4e3d 8133453: Deprecate AWTKeyStroke.registerSubclass(Class) method
alexsch
parents: 26749
diff changeset
   706
90051b1c4e3d 8133453: Deprecate AWTKeyStroke.registerSubclass(Class) method
alexsch
parents: 26749
diff changeset
   707
            return getCachedStroke(keyChar, keyCode, modifiers, onKeyRelease);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    private static int mapOldModifiers(int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            modifiers |= InputEvent.SHIFT_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        if ((modifiers & InputEvent.ALT_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            modifiers |= InputEvent.ALT_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if ((modifiers & InputEvent.CTRL_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            modifiers |= InputEvent.CTRL_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if ((modifiers & InputEvent.META_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            modifiers |= InputEvent.META_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        modifiers &= InputEvent.SHIFT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            | InputEvent.ALT_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            | InputEvent.ALT_GRAPH_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            | InputEvent.CTRL_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            | InputEvent.META_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            | InputEvent.BUTTON1_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            | InputEvent.BUTTON2_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            | InputEvent.BUTTON3_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        return modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    private static int mapNewModifiers(int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            modifiers |= InputEvent.SHIFT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            modifiers |= InputEvent.ALT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            modifiers |= InputEvent.ALT_GRAPH_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            modifiers |= InputEvent.CTRL_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        if ((modifiers & InputEvent.META_DOWN_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            modifiers |= InputEvent.META_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        return modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
class VKCollection {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 11502
diff changeset
   763
    Map<Integer, String> code2name;
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 11502
diff changeset
   764
    Map<String, Integer> name2code;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    public VKCollection() {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 11502
diff changeset
   767
        code2name = new HashMap<>();
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 11502
diff changeset
   768
        name2code = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public synchronized void put(String name, Integer code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        assert((name != null) && (code != null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        assert(findName(code) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        assert(findCode(name) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        code2name.put(code, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        name2code.put(name, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    public synchronized Integer findCode(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        assert(name != null);
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20172
diff changeset
   781
        return name2code.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    public synchronized String findName(Integer code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        assert(code != null);
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20172
diff changeset
   786
        return code2name.get(code);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
}