📄 awtkeystroke.java
字号:
/* AWTKeyStroke.java -- an immutable key stroke Copyright (C) 2002 Free Software FoundationThis file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.awt;import java.awt.event.KeyEvent;import java.io.ObjectStreamException;import java.io.Serializable;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.security.AccessController;import java.security.PrivilegedAction;import java.security.PrivilegedActionException;import java.security.PrivilegedExceptionAction;import java.util.Map;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.StringTokenizer;/** * This class mirrors KeyEvents, representing both low-level key presses and * key releases, and high level key typed inputs. However, this class forms * immutable strokes, and can be efficiently reused via the factory methods * for creating them. * * <p>For backwards compatibility with Swing, this supports a way to build * instances of a subclass, using reflection, provided the subclass has a * no-arg constructor (of any accessibility). * * @author Eric Blake <ebb9@email.byu.edu> * @see #getAWTKeyStroke(char) * @since 1.4 * @status updated to 1.4 */public class AWTKeyStroke implements Serializable{ /** * Compatible with JDK 1.4+. */ private static final long serialVersionUID = -6430539691155161871L; /** The mask for modifiers. */ private static final int MODIFIERS_MASK = 0x3fef; /** * The cache of recently created keystrokes. This maps KeyStrokes to * KeyStrokes in a cache which removes the least recently accessed entry, * under the assumption that garbage collection of a new keystroke is * easy when we find the old one that it matches in the cache. */ private static final LinkedHashMap cache = new LinkedHashMap(11, 0.75f, true) { /** The largest the keystroke cache can grow. */ private static final int MAX_CACHE_SIZE = 2048; /** Prune stale entries. */ protected boolean removeEldestEntry(Map.Entry eldest) { // XXX - FIXME Use Map.Entry, not just Entry as gcj 3.1 workaround. return size() > MAX_CACHE_SIZE; } }; /** The most recently generated keystroke, or null. */ private static AWTKeyStroke recent; /** * The no-arg constructor of a subclass, or null to use AWTKeyStroke. Note * that this will be left accessible, to get around private access; but * it should not be a security risk as it is highly unlikely that creating * protected instances of the subclass via reflection will do much damage. */ private static Constructor ctor; /** * A table of keyCode names to values. * * @see #getAWTKeyStroke(String) */ private static final HashMap vktable = new HashMap(); static { // Using reflection saves the hassle of keeping this in sync with KeyEvent, // at the price of an expensive initialization. AccessController.doPrivileged(new PrivilegedAction() { public Object run() { Field[] fields = KeyEvent.class.getFields(); int i = fields.length; try { while (--i >= 0) { Field f = fields[i]; String name = f.getName(); if (name.startsWith("VK_")) vktable.put(name.substring(3), f.get(null)); } } catch (Exception e) { throw (Error) new InternalError().initCause(e); } return null; } }); } /** * The typed character, or CHAR_UNDEFINED for key presses and releases. * * @serial the keyChar */ private char keyChar; /** * The virtual key code, or VK_UNDEFINED for key typed. Package visible for * use by Component. * * @serial the keyCode */ int keyCode; /** * The modifiers in effect. To match Sun, this stores the old style masks * for shift, control, alt, meta, and alt-graph (but not button1); as well * as the new style of extended modifiers for all modifiers. * * @serial bitwise or of the *_DOWN_MASK modifiers */ private int modifiers; /** * True if this is a key release; should only be true if keyChar is * CHAR_UNDEFINED. * * @serial true to distinguish key pressed from key released */ private boolean onKeyRelease; /** * Construct a keystroke with default values: it will be interpreted as a * key typed event with an invalid character and no modifiers. Client code * should use the factory methods instead. * * @see #getAWTKeyStroke(char) * @see #getAWTKeyStroke(Character, int) * @see #getAWTKeyStroke(int, int, boolean) * @see #getAWTKeyStroke(int, int) * @see #getAWTKeyStrokeForEvent(KeyEvent) * @see #getAWTKeyStroke(String) */ protected AWTKeyStroke() { keyChar = KeyEvent.CHAR_UNDEFINED; } /** * Construct a keystroke with the given values. Client code should use the * factory methods instead. * * @param keyChar the character entered, if this is a key typed * @param keyCode the key pressed or released, or VK_UNDEFINED for key typed * @param modifiers the modifier keys for the keystroke, in old or new style * @param onKeyRelease true if this is a key release instead of a press * @see #getAWTKeyStroke(char) * @see #getAWTKeyStroke(Character, int) * @see #getAWTKeyStroke(int, int, boolean) * @see #getAWTKeyStroke(int, int) * @see #getAWTKeyStrokeForEvent(KeyEvent) * @see #getAWTKeyStroke(String) */ protected AWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean onKeyRelease) { this.keyChar = keyChar; this.keyCode = keyCode; // No need to call extend(), as only trusted code calls this constructor. this.modifiers = modifiers; this.onKeyRelease = onKeyRelease; } /** * Registers a new subclass as being the type of keystrokes to generate in * the factory methods. This operation flushes the cache of stored keystrokes * if the class differs from the current one. The new class must be * AWTKeyStroke or a subclass, and must have a no-arg constructor (which may * be private). * * @param subclass the new runtime type of generated keystrokes * @throws IllegalArgumentException subclass doesn't have no-arg constructor * @throws ClassCastException subclass doesn't extend AWTKeyStroke */ protected static void registerSubclass(final Class subclass) { if (subclass == null) throw new IllegalArgumentException(); if (subclass.equals(ctor == null ? AWTKeyStroke.class : ctor.getDeclaringClass())) return; if (subclass.equals(AWTKeyStroke.class)) { cache.clear(); recent = null; ctor = null; return; } try { ctor = (Constructor) AccessController.doPrivileged (new PrivilegedExceptionAction() { public Object run() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { Constructor c = subclass.getDeclaredConstructor(null); c.setAccessible(true); // Create a new instance, to make sure that we can, and // to cause any ClassCastException. AWTKeyStroke dummy = (AWTKeyStroke) c.newInstance(null); return c; } }); } catch (PrivilegedActionException e) { // e.getCause() will not ever be ClassCastException; that should // escape on its own. throw (RuntimeException) new IllegalArgumentException().initCause(e.getCause()); } cache.clear(); recent = null; } /** * Returns a keystroke representing a typed character. * * @param keyChar the typed character * @return the specified keystroke */ public static AWTKeyStroke getAWTKeyStroke(char keyChar) { return getAWTKeyStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, false); } /** * Returns a keystroke representing a typed character with the given * modifiers. Note that keyChar is a <code>Character</code> instead of a * <code>char</code> to avoid accidental ambiguity with * <code>getAWTKeyStroke(int, int)</code>. The modifiers are the bitwise * or of the masks found in {@link InputEvent}; the new style (*_DOWN_MASK) * is preferred, but the old style will work. * * @param keyChar the typed character * @param modifiers the modifiers, or 0 * @return the specified keystroke * @throws IllegalArgumentException if keyChar is null */ public static AWTKeyStroke getAWTKeyStroke(Character keyChar, int modifiers) { if (keyChar == null) throw new IllegalArgumentException(); return getAWTKeyStroke(keyChar.charValue(), KeyEvent.VK_UNDEFINED, extend(modifiers), false); } /** * Returns a keystroke representing a pressed or released key event, with * the given modifiers. The "virtual key" should be one of the VK_* * constants in {@link KeyEvent}. The modifiers are the bitwise or of the * masks found in {@link InputEvent}; the new style (*_DOWN_MASK) is * preferred, but the old style will work. * * @param keyCode the virtual key * @param modifiers the modifiers, or 0 * @param release true if this is a key release instead of a key press * @return the specified keystroke */ public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers, boolean release) { return getAWTKeyStroke(KeyEvent.CHAR_UNDEFINED, keyCode, extend(modifiers), release); } /** * Returns a keystroke representing a pressed key event, with the given * modifiers. The "virtual key" should be one of the VK_* constants in * {@link KeyEvent}. The modifiers are the bitwise or of the masks found
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -