awtkeystroke.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 743 行 · 第 1/2 页
JAVA
743 行
/* * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package java.awt;import java.awt.event.KeyEvent;import java.awt.event.InputEvent;import java.util.Collections;import java.util.HashMap;import java.util.Map;import java.util.StringTokenizer;import java.io.Serializable;import java.lang.reflect.*;/** * An <code>AWTKeyStroke</code> represents a key action on the * keyboard, or equivalent input device. <code>AWTKeyStroke</code>s * can correspond to only a press or release of a * particular key, just as <code>KEY_PRESSED</code> and * <code>KEY_RELEASED</code> <code>KeyEvent</code>s do; * alternately, they can correspond to typing a specific Java character, just * as <code>KEY_TYPED</code> <code>KeyEvent</code>s do. * In all cases, <code>AWTKeyStroke</code>s can specify modifiers * (alt, shift, control, meta, or a combination thereof) which must be present * during the action for an exact match. * <p> * <code>AWTKeyStrokes</code> are immutable, and are intended * to be unique. Client code should never create an * <code>AWTKeyStroke</code> on its own, but should instead use * a variant of <code>getAWTKeyStroke</code>. Client use of these factory * methods allows the <code>AWTKeyStroke</code> implementation * to cache and share instances efficiently. * * @see #getAWTKeyStroke * * @version 1.14, 01/23/03 * @author Arnaud Weber * @author David Mendenhall * @since 1.4 */public class AWTKeyStroke implements Serializable { private static Map cache; private static AWTKeyStroke cacheKey; private static Class subclass = AWTKeyStroke.class; private static Map modifierKeywords; /** * Maps from VK_XXX (as a String) to an Integer. This is done to * avoid the overhead of the reflective call to find the constant. */ private static Map vkMap; // Fix 6234000. static final long serialVersionUID = -6430539691155161871L; private char keyChar = KeyEvent.CHAR_UNDEFINED; private int keyCode = KeyEvent.VK_UNDEFINED; private int modifiers; private boolean onKeyRelease; /** * Constructs an <code>AWTKeyStroke</code> with default values. * The default values used are: * <table border="1" summary="AWTKeyStroke default values"> * <tr><th>Property</th><th>Default Value</th></tr> * <tr> * <td>Key Char</td> * <td><code>KeyEvent.CHAR_UNDEFINED</code></td> * </tr> * <tr> * <td>Key Code</td> * <td><code>KeyEvent.VK_UNDEFINED</code></td> * </tr> * <tr> * <td>Modifiers</td> * <td>none</td> * </tr> * <tr> * <td>On key release?</td> * <td><code>false</code></td> * </tr> * </table> * * <code>AWTKeyStroke</code>s should not be constructed * by client code. Use a variant of <code>getAWTKeyStroke</code> * instead. * * @see #getAWTKeyStroke */ // PBP/PP 6218482 // Make package private // protected AWTKeyStroke() { } AWTKeyStroke() {} /** * Constructs an <code>AWTKeyStroke</code> with the specified * values. <code>AWTKeyStroke</code>s should not be constructed * by client code. Use a variant of <code>getAWTKeyStroke</code> * instead. * * @param keyChar the character value for a keyboard key * @param keyCode the key code for this <code>AWTKeyStroke</code> * @param modifiers a bitwise-ored combination of any modifiers * @param onKeyRelease <code>true</code> if this * <code>AWTKeyStroke</code> corresponds * to a key release; <code>false</code> otherwise * @see #getAWTKeyStroke */ // PBP/PP 6218482 // Make package private // protected AWTKeyStroke(char keyChar, int keyCode, int modifiers, // boolean onKeyRelease) { AWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean onKeyRelease) { this.keyChar = keyChar; this.keyCode = keyCode; this.modifiers = modifiers; this.onKeyRelease = onKeyRelease; } private void copy(AWTKeyStroke rhs) { this.keyChar = rhs.keyChar; this.keyCode = rhs.keyCode; this.modifiers = rhs.modifiers; this.onKeyRelease = rhs.onKeyRelease; } /** * Registers a new class which the factory methods in * <code>AWTKeyStroke</code> will use when generating new * instances of <code>AWTKeyStroke</code>s. After invoking this * method, the factory methods will return instances of the specified * Class. The specified Class must be either <code>AWTKeyStroke</code> * or derived from <code>AWTKeyStroke</code>, and it must have a * no-arg constructor. The constructor can be of any accessibility, * including <code>private</code>. This operation * flushes the current <code>AWTKeyStroke</code> cache. * * @param subclass the new Class of which the factory methods should create * instances * @throws IllegalArgumentException if subclass is <code>null</code>, * or if subclass does not have a no-arg constructor * @throws ClassCastException if subclass is not * <code>AWTKeyStroke</code>, or a class derived from * <code>AWTKeyStroke</code> */ // PBP/PP 6218482 // Remove registerSubClass(). /* protected static void registerSubclass(Class subclass) { if (subclass == null) { throw new IllegalArgumentException("subclass cannot be null"); } if (AWTKeyStroke.subclass.equals(subclass)) { // Already registered return; } if (!AWTKeyStroke.class.isAssignableFrom(subclass)) { throw new ClassCastException("subclass is not derived from AWTKeyStroke"); } String couldNotInstantiate = "subclass could not be instantiated"; try { AWTKeyStroke stroke = allocateNewInstance(subclass); if (stroke == null) { throw new IllegalArgumentException(couldNotInstantiate); } } catch (NoSuchMethodError e) { throw new IllegalArgumentException(couldNotInstantiate); } catch (ExceptionInInitializerError e) { throw new IllegalArgumentException(couldNotInstantiate); } catch (InstantiationException e) { throw new IllegalArgumentException(couldNotInstantiate); } synchronized (AWTKeyStroke.class) { AWTKeyStroke.subclass = subclass; cache = null; cacheKey = null; } } */ // Reflection allows us to bypass all security and access restrictions. // This allows us to define javax.swing.KeyStroke with only a private // no-arg constructor, yet still instantiate it from AWT without // special knowledge of Swing. private static AWTKeyStroke allocateNewInstance(Class clazz) throws InstantiationException { Class[] parameterTypes = new Class[0]; AWTKeyStroke newInstance = null; try { final Constructor constructor = clazz.getDeclaredConstructor(parameterTypes); java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Object run() { constructor.setAccessible(true); return null; } }); Object[] args = new Object[0]; newInstance = (AWTKeyStroke) constructor.newInstance(args); } catch (InstantiationException ie) { throw ie; } catch (Exception e) { throw new InstantiationException("Couldn't instantiate class " + clazz.getName()); } return newInstance; } private static synchronized AWTKeyStroke getCachedStroke (char keyChar, int keyCode, int modifiers, boolean onKeyRelease) { if (cache == null) { cache = new HashMap(); } if (cacheKey == null) { try { cacheKey = allocateNewInstance(subclass); } catch (InstantiationException e) {} } cacheKey.keyChar = keyChar; cacheKey.keyCode = keyCode; cacheKey.modifiers = mapNewModifiers(mapOldModifiers(modifiers)); cacheKey.onKeyRelease = onKeyRelease; AWTKeyStroke stroke = (AWTKeyStroke) cache.get(cacheKey); if (stroke == null) { stroke = cacheKey; cache.put(stroke, stroke); cacheKey = null; } return stroke; } /** * Returns a shared instance of an <code>AWTKeyStroke</code> * that represents a <code>KEY_TYPED</code> event for the * specified character. * * @param keyChar the character value for a keyboard key * @return an <code>AWTKeyStroke</code> object for that key */ public static AWTKeyStroke getAWTKeyStroke(char keyChar) { return getCachedStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, false); } /** * Returns a shared instance of an <code>AWTKeyStroke</code>, * given a Character object and a set of modifiers. Note * that the first parameter is of type Character rather than * char. This is to avoid inadvertent clashes with * calls to <code>getAWTKeyStroke(int keyCode, int modifiers)</code>. * * The modifiers consist of any combination of:<ul> * <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK * <li>java.awt.event.InputEvent.CTRL_DOWN_MASK * <li>java.awt.event.InputEvent.META_DOWN_MASK * <li>java.awt.event.InputEvent.ALT_DOWN_MASK * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK * <li>java.awt.event.InputEvent.BUTTON1_DOWN_MASK * <li>java.awt.event.InputEvent.BUTTON2_DOWN_MASK * <li>java.awt.event.InputEvent.BUTTON3_DOWN_MASK * </ul> * The old modifiers <ul> * <li>java.awt.event.InputEvent.SHIFT_MASK * <li>java.awt.event.InputEvent.CTRL_MASK * <li>java.awt.event.InputEvent.META_MASK * <li>java.awt.event.InputEvent.ALT_MASK * <li>java.awt.event.InputEvent.ALT_GRAPH_MASK * </ul> * also can be used, but they are mapped to _DOWN_ modifiers. * * Since these numbers are all different powers of two, any combination of * them is an integer in which each bit represents a different modifier * key. Use 0 to specify no modifiers. * * @param keyChar the Character object for a keyboard character * @param modifiers a bitwise-ored combination of any modifiers * @return an <code>AWTKeyStroke</code> object for that key * @throws IllegalArgumentException if <code>keyChar</code> is * <code>null</code> * * @see java.awt.event.InputEvent */ public static AWTKeyStroke getAWTKeyStroke(Character keyChar, int modifiers) { if (keyChar == null) { throw new IllegalArgumentException("keyChar cannot be null"); } return getCachedStroke(keyChar.charValue(), KeyEvent.VK_UNDEFINED, modifiers, false); } /** * Returns a shared instance of an <code>AWTKeyStroke</code>, * given a numeric key code and a set of modifiers, specifying * whether the key is activated when it is pressed or released. * <p> * The "virtual key" constants defined in * <code>java.awt.event.KeyEvent</code> can be * used to specify the key code. For example:<ul> * <li><code>java.awt.event.KeyEvent.VK_ENTER</code> * <li><code>java.awt.event.KeyEvent.VK_TAB</code> * <li><code>java.awt.event.KeyEvent.VK_SPACE</code> * </ul> * The modifiers consist of any combination of:<ul> * <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK * <li>java.awt.event.InputEvent.CTRL_DOWN_MASK * <li>java.awt.event.InputEvent.META_DOWN_MASK * <li>java.awt.event.InputEvent.ALT_DOWN_MASK * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK * <li>java.awt.event.InputEvent.BUTTON1_DOWN_MASK * <li>java.awt.event.InputEvent.BUTTON2_DOWN_MASK * <li>java.awt.event.InputEvent.BUTTON3_DOWN_MASK * </ul> * The old modifiers <ul> * <li>java.awt.event.InputEvent.SHIFT_MASK * <li>java.awt.event.InputEvent.CTRL_MASK * <li>java.awt.event.InputEvent.META_MASK * <li>java.awt.event.InputEvent.ALT_MASK * <li>java.awt.event.InputEvent.ALT_GRAPH_MASK * </ul> * also can be used, but they are mapped to _DOWN_ modifiers. * * Since these numbers are all different powers of two, any combination of * them is an integer in which each bit represents a different modifier * key. Use 0 to specify no modifiers. * * @param keyCode an int specifying the numeric code for a keyboard key * @param modifiers a bitwise-ored combination of any modifiers * @param onKeyRelease <code>true</code> if the <code>AWTKeyStroke</code> * should represent a key release; <code>false</code> otherwise * @return an AWTKeyStroke object for that key * * @see java.awt.event.KeyEvent * @see java.awt.event.InputEvent */ public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers, boolean onKeyRelease) { return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers, onKeyRelease); } /** * Returns a shared instance of an <code>AWTKeyStroke</code>, * given a numeric key code and a set of modifiers. The returned * <code>AWTKeyStroke</code> will correspond to a key press. * <p> * The "virtual key" constants defined in * <code>java.awt.event.KeyEvent</code> can be * used to specify the key code. For example:<ul> * <li><code>java.awt.event.KeyEvent.VK_ENTER</code> * <li><code>java.awt.event.KeyEvent.VK_TAB</code>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?