awtkeystroke.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 743 行 · 第 1/2 页
JAVA
743 行
* <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 * @return an <code>AWTKeyStroke</code> object for that key * * @see java.awt.event.KeyEvent * @see java.awt.event.InputEvent */ public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers) { return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers, false); } /** * Returns an <code>AWTKeyStroke</code> which represents the * stroke which generated a given <code>KeyEvent</code>. * <p> * This method obtains the key char from a <code>KeyTyped</code> * event, and the key code from a <code>KeyPressed</code> or * <code>KeyReleased</code> event. The <code>KeyEvent</code> modifiers are * obtained for all three types of <code>KeyEvent</code>. * * @param anEvent the <code>KeyEvent</code> from which to * obtain the <code>AWTKeyStroke</code> * @return the <code>AWTKeyStroke</code> that precipitated the event */ public static AWTKeyStroke getAWTKeyStrokeForEvent(KeyEvent anEvent) { int id = anEvent.getID(); switch (id) { case KeyEvent.KEY_PRESSED: case KeyEvent.KEY_RELEASED: return getCachedStroke(KeyEvent.CHAR_UNDEFINED, anEvent.getKeyCode(), anEvent.getModifiers(), (id == KeyEvent.KEY_RELEASED)); case KeyEvent.KEY_TYPED: return getCachedStroke(anEvent.getKeyChar(), KeyEvent.VK_UNDEFINED, anEvent.getModifiers(), false); default: // Invalid ID for this KeyEvent return null; } } /** * Parses a string and returns an <code>AWTKeyStroke</code>. * The string must have the following syntax: * <pre> * <modifiers>* (<typedID> | <pressedReleasedID>) * * modifiers := shift | control | ctrl | meta | alt | button1 | button2 | button3 * typedID := typed <typedKey> * typedKey := string of length 1 giving Unicode character. * pressedReleasedID := (pressed | released) key * key := KeyEvent key code name, i.e. the name following "VK_". * </pre> * If typed, pressed or released is not specified, pressed is assumed. Here * are some examples: * <pre> * "INSERT" => getAWTKeyStroke(KeyEvent.VK_INSERT, 0); * "control DELETE" => getAWTKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK); * "alt shift X" => getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK); * "alt shift released X" => getAWTKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true); * "typed a" => getAWTKeyStroke('a'); * </pre> * * @param s a String formatted as described above * @return an <code>AWTKeyStroke</code> object for that String * @throws IllegalArgumentException if <code>s</code> is <code>null</code>, * or is formatted incorrectly */ public static AWTKeyStroke getAWTKeyStroke(String s) { if (s == null) { throw new IllegalArgumentException("String cannot be null"); } final String errmsg = "String formatted incorrectly"; StringTokenizer st = new StringTokenizer(s, " "); int mask = 0; boolean released = false; boolean typed = false; boolean pressed = false; if (modifierKeywords == null) { synchronized (AWTKeyStroke.class) { if (modifierKeywords == null) { Map uninitializedMap = new HashMap(8, 1.0f); uninitializedMap.put("shift", new Integer(InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)); uninitializedMap.put("control", new Integer(InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)); uninitializedMap.put("ctrl", new Integer(InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)); uninitializedMap.put("meta", new Integer(InputEvent.META_DOWN_MASK | InputEvent.META_MASK)); uninitializedMap.put("alt", new Integer(InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)); uninitializedMap.put("altGraph", new Integer(InputEvent.ALT_GRAPH_DOWN_MASK | InputEvent.ALT_GRAPH_MASK)); uninitializedMap.put("button1", new Integer(InputEvent.BUTTON1_DOWN_MASK)); uninitializedMap.put("button2", new Integer(InputEvent.BUTTON2_DOWN_MASK)); uninitializedMap.put("button3", new Integer(InputEvent.BUTTON3_DOWN_MASK)); modifierKeywords = Collections.synchronizedMap(uninitializedMap); } } } int count = st.countTokens(); for (int i = 1; i <= count; i++) { String token = st.nextToken(); if (typed) { if (token.length() != 1 || i != count) { throw new IllegalArgumentException(errmsg); } return getCachedStroke(token.charAt(0), KeyEvent.VK_UNDEFINED, mask, false); } if (pressed || released || i == count) { if (i != count) { throw new IllegalArgumentException(errmsg); } String keyCodeName = "VK_" + token; int keyCode = getVKValue(keyCodeName); return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode, mask, released); } if (token.equals("released")) { released = true; continue; } if (token.equals("pressed")) { pressed = true; continue; } if (token.equals("typed")) { typed = true; continue; } Integer tokenMask = (Integer) modifierKeywords.get(token); if (tokenMask != null) { mask |= tokenMask.intValue(); } else { throw new IllegalArgumentException(errmsg); } } throw new IllegalArgumentException(errmsg); } /** * Returns the integer constant for the KeyEvent.VK field named * <code>key</code>. This will throw an * <code>IllegalArgumentException</code> if <code>key</code> is * not a valid constant. */ private static int getVKValue(String key) { if (vkMap == null) { vkMap = Collections.synchronizedMap(new HashMap()); } Integer value = (Integer) vkMap.get(key); if (value == null) { int keyCode = 0; final String errmsg = "String formatted incorrectly"; try { keyCode = KeyEvent.class.getField(key).getInt(KeyEvent.class); } catch (NoSuchFieldException nsfe) { throw new IllegalArgumentException(errmsg); } catch (IllegalAccessException iae) { throw new IllegalArgumentException(errmsg); } value = new Integer(keyCode); vkMap.put(key, value); } return value.intValue(); } /** * Returns the character for this <code>AWTKeyStroke</code>. * * @return a char value * @see #getAWTKeyStroke(char) */ public final char getKeyChar() { return keyChar; } /** * Returns the numeric key code for this <code>AWTKeyStroke</code>. * * @return an int containing the key code value * @see #getAWTKeyStroke(int,int) */ public final int getKeyCode() { return keyCode; } /** * Returns the modifier keys for this <code>AWTKeyStroke</code>. * * @return an int containing the modifiers * @see #getAWTKeyStroke(int,int) */ public final int getModifiers() { return modifiers; } /** * Returns whether this <code>AWTKeyStroke</code> represents a key release. * * @return <code>true</code> if this <code>AWTKeyStroke</code> * represents a key release; <code>false</code> otherwise * @see #getAWTKeyStroke(int,int,boolean) */ public final boolean isOnKeyRelease() { return onKeyRelease; } /** * Returns the type of <code>KeyEvent</code> which corresponds to * this <code>AWTKeyStroke</code>. * * @return <code>KeyEvent.KEY_PRESSED</code>, * <code>KeyEvent.KEY_TYPED</code>, * or <code>KeyEvent.KEY_RELEASED</code> * @see java.awt.event.KeyEvent */ public final int getKeyEventType() { if (keyCode == KeyEvent.VK_UNDEFINED) { return KeyEvent.KEY_TYPED; } else { return (onKeyRelease) ? KeyEvent.KEY_RELEASED : KeyEvent.KEY_PRESSED; } } /** * Returns a numeric value for this object that is likely to be unique, * making it a good choice as the index value in a hash table. * * @return an int that represents this object */ public int hashCode() { return (((int) keyChar) + 1) * (2 * (keyCode + 1)) * (modifiers + 1) + (onKeyRelease ? 1 : 2); } /** * Returns true if this object is identical to the specified object. * * @param anObject the Object to compare this object to * @return true if the objects are identical */ public final boolean equals(Object anObject) { if (anObject instanceof AWTKeyStroke) { AWTKeyStroke ks = (AWTKeyStroke) anObject; return (ks.keyChar == keyChar && ks.keyCode == keyCode && ks.onKeyRelease == onKeyRelease && ks.modifiers == modifiers); } return false; } /** * Returns a string that displays and identifies this object's properties. * * @return a String representation of this object */ public String toString() { if (keyCode == KeyEvent.VK_UNDEFINED) { return "keyChar " + KeyEvent.getKeyModifiersText(modifiers) + keyChar; } else { return "keyCode " + KeyEvent.getKeyModifiersText(modifiers) + KeyEvent.getKeyText(keyCode) + (onKeyRelease ? "-R" : "-P"); } } /** * Returns a cached instance of <code>AWTKeyStroke</code> * which is equal to this instance. * * @return a cached instance which is equal to this instance */ // PBP/PP 6218482 // Make readResolve() private, like java.net.InetAddress. // protected Object readResolve() throws java.io.ObjectStreamException { private Object readResolve() throws java.io.ObjectStreamException { synchronized (AWTKeyStroke.class) { /* Class newClass = getClass(); if (!newClass.equals(subclass)) { registerSubclass(newClass); } */ return getCachedStroke(keyChar, keyCode, modifiers, onKeyRelease); } } private static int mapOldModifiers(int modifiers) { if ((modifiers & InputEvent.SHIFT_MASK) != 0) { modifiers |= InputEvent.SHIFT_DOWN_MASK; } if ((modifiers & InputEvent.ALT_MASK) != 0) { modifiers |= InputEvent.ALT_DOWN_MASK; } if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) { modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK; } if ((modifiers & InputEvent.CTRL_MASK) != 0) { modifiers |= InputEvent.CTRL_DOWN_MASK; } if ((modifiers & InputEvent.META_MASK) != 0) { modifiers |= InputEvent.META_DOWN_MASK; } if ((modifiers & InputEvent.BUTTON1_MASK) != 0) { modifiers |= InputEvent.BUTTON1_DOWN_MASK; } modifiers &= InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK | InputEvent.ALT_GRAPH_DOWN_MASK | InputEvent.CTRL_DOWN_MASK | InputEvent.META_DOWN_MASK | InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON3_DOWN_MASK; return modifiers; } private static int mapNewModifiers(int modifiers) { if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) { modifiers |= InputEvent.SHIFT_MASK; } if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) { modifiers |= InputEvent.ALT_MASK; } if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) { modifiers |= InputEvent.ALT_GRAPH_MASK; } if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) { modifiers |= InputEvent.CTRL_MASK; } if ((modifiers & InputEvent.META_DOWN_MASK) != 0) { modifiers |= InputEvent.META_MASK; } return modifiers; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?