📄 awtkeystroke.java
字号:
* 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 * @return the specified keystroke */ public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers) { return getAWTKeyStroke(KeyEvent.CHAR_UNDEFINED, keyCode, extend(modifiers), false); } /** * Returns a keystroke representing what caused the key event. * * @param event the key event to convert * @return the specified keystroke, or null if the event is invalid * @throws NullPointerException if event is null */ public static AWTKeyStroke getAWTKeyStrokeForEvent(KeyEvent event) { switch (event.id) { case KeyEvent.KEY_TYPED: return getAWTKeyStroke(event.getKeyChar(), KeyEvent.VK_UNDEFINED, extend(event.getModifiersEx()), false); case KeyEvent.KEY_PRESSED: return getAWTKeyStroke(KeyEvent.CHAR_UNDEFINED, event.getKeyCode(), extend(event.getModifiersEx()), false); case KeyEvent.KEY_RELEASED: return getAWTKeyStroke(KeyEvent.CHAR_UNDEFINED, event.getKeyCode(), extend(event.getModifiersEx()), true); default: return null; } } /** * Parses a string and returns the keystroke that it represents. The syntax * for keystrokes is listed below, with tokens separated by an arbitrary * number of spaces: * <pre> * keyStroke := <modifiers>* ( <typedID> | <codeID> ) * modifiers := ( shift | control | ctrl | meta | alt * | button1 | button2 | button3 ) * typedID := typed <single Unicode character> * codeID := ( pressed | released )? <name> * name := <the KeyEvent field name less the leading "VK_"> * </pre> * * <p>Note that the grammar is rather weak, and not all valid keystrokes * can be generated in this manner (for example, a typed space, or anything * with the alt-graph modifier!). The output of AWTKeyStroke.toString() * will not meet the grammar. If pressed or released is not specified, * pressed is assumed. Examples:<br> * <code> * "INSERT" => getAWTKeyStroke(KeyEvent.VK_INSERT, 0);<br> * "control DELETE" => * getAWTKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);<br> * "alt shift X" => getAWTKeyStroke(KeyEvent.VK_X, * InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);<br> * "alt shift released X" => getAWTKeyStroke(KeyEvent.VK_X, * InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);<br> * "typed a" => getAWTKeyStroke('a'); * </code> * * @param s the string to parse * @return the specified keystroke * @throws NullPointerException if s is null * @throws IllegalArgumentException if s cannot be parsed */ public static AWTKeyStroke getAWTKeyStroke(String s) { StringTokenizer t = new StringTokenizer(s, " "); if (! t.hasMoreTokens()) throw new IllegalArgumentException(); int modifiers = 0; boolean released = false; String token = null; do { token = t.nextToken(); if ("shift".equals(token)) modifiers |= KeyEvent.SHIFT_MASK | KeyEvent.SHIFT_DOWN_MASK; else if ("ctrl".equals(token) || "control".equals(token)) modifiers |= KeyEvent.CTRL_MASK | KeyEvent.CTRL_DOWN_MASK; else if ("meta".equals(token)) modifiers |= KeyEvent.META_MASK | KeyEvent.META_DOWN_MASK; else if ("alt".equals(token)) modifiers |= KeyEvent.ALT_MASK | KeyEvent.ALT_DOWN_MASK; else if ("button1".equals(token)) modifiers |= KeyEvent.BUTTON1_DOWN_MASK; else if ("button2".equals(token)) modifiers |= KeyEvent.BUTTON2_DOWN_MASK; else if ("button3".equals(token)) modifiers |= KeyEvent.BUTTON3_DOWN_MASK; else if ("typed".equals(token)) { if (t.hasMoreTokens()) { token = t.nextToken(); if (! t.hasMoreTokens() && token.length() == 1) return getAWTKeyStroke(token.charAt(0), KeyEvent.VK_UNDEFINED, modifiers, false); } throw new IllegalArgumentException(); } else if ("pressed".equals(token)) { if (t.hasMoreTokens()) token = t.nextToken(); break; } else if ("released".equals(token)) { released = true; if (t.hasMoreTokens()) token = t.nextToken(); break; } else break; } while (t.hasMoreTokens()); // Now token contains the VK name we must parse. Integer code = (Integer) vktable.get(token); if (code == null || t.hasMoreTokens()) throw new IllegalArgumentException(); return getAWTKeyStroke(KeyEvent.CHAR_UNDEFINED, code.intValue(), modifiers, released); } /** * Returns the character of this keystroke, if it was typed. * * @return the character value, or CHAR_UNDEFINED * @see #getAWTKeyStroke(char) */ public final char getKeyChar() { return keyChar; } /** * Returns the virtual key code of this keystroke, if it was pressed or * released. This will be a VK_* constant from KeyEvent. * * @return the virtual key code value, or VK_UNDEFINED * @see #getAWTKeyStroke(int, int) */ public final int getKeyCode() { return keyCode; } /** * Returns the modifiers for this keystroke. This will be a bitwise or of * constants from InputEvent; it includes 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. * * @return the modifiers * @see #getAWTKeyStroke(Character, int) * @see #getAWTKeyStroke(int, int) */ public final int getModifiers() { return modifiers; } /** * Tests if this keystroke is a key release. * * @return true if this is a key release * @see #getAWTKeyStroke(int, int, boolean) */ public final boolean isOnKeyRelease() { return onKeyRelease; } /** * Returns the AWT event type of this keystroke. This is one of * {@link KeyEvent#KEY_TYPED}, {@link KeyEvent#KEY_PRESSED}, or * {@link KeyEvent#KEY_RELEASED}. * * @return the key event type */ public final int getKeyEventType() { return keyCode == KeyEvent.VK_UNDEFINED ? KeyEvent.KEY_TYPED : onKeyRelease ? KeyEvent.KEY_RELEASED : KeyEvent.KEY_PRESSED; } /** * Returns a hashcode for this key event. It is not documented, but appears * to be: <code>(getKeyChar() + 1) * (getKeyCode() + 1) * * (getModifiers() + 1) * 2 + (isOnKeyRelease() ? 1 : 2)</code>. * * @return the hashcode */ public int hashCode() { return (keyChar + 1) * (keyCode + 1) * (modifiers + 1) * 2 + (onKeyRelease ? 1 : 2); } /** * Tests two keystrokes for equality. * * @param o the object to test * @return true if it is equal */ public final boolean equals(Object o) { if (! (o instanceof AWTKeyStroke)) return false; AWTKeyStroke s = (AWTKeyStroke) o; return this == o || (keyChar == s.keyChar && keyCode == s.keyCode && modifiers == s.modifiers && onKeyRelease == s.onKeyRelease); } /** * Returns a string representation of this keystroke. For typed keystrokes, * this is <code>"keyChar " + KeyEvent.getKeyModifiersText(getModifiers()) + getKeyChar()</code>; for pressed and released keystrokes, this is * <code>"keyCode " + KeyEvent.getKeyModifiersText(getModifiers()) * + KeyEvent.getKeyText(getKeyCode()) * + (isOnKeyRelease() ? "-R" : "-P")</code>. * * @return a string representation */ public String toString() { if (keyCode == KeyEvent.VK_UNDEFINED) return "keyChar " + KeyEvent.getKeyModifiersText(modifiers) + keyChar; return "keyCode " + KeyEvent.getKeyModifiersText(modifiers) + KeyEvent.getKeyText(keyCode) + (onKeyRelease ? "-R" : "-P"); } /** * Returns a cached version of the deserialized keystroke, if available. * * @return a cached replacement * @throws ObjectStreamException if something goes wrong */ protected Object readResolve() throws ObjectStreamException { AWTKeyStroke s = (AWTKeyStroke) cache.get(this); if (s != null) return s; cache.put(this, this); return this; } /** * Gets the appropriate keystroke, creating one if necessary. * * @param keyChar the keyChar * @param keyCode the keyCode * @param modifiers the modifiers * @param release true for key release * @return the specified keystroke */ private static AWTKeyStroke getAWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean release) { // Check level 0 cache. AWTKeyStroke stroke = recent; // Avoid thread races. if (stroke != null && stroke.keyChar == keyChar && stroke.keyCode == keyCode && stroke.modifiers == modifiers && stroke.onKeyRelease == release) return stroke; // Create a new object, on the assumption that if it has a match in the // cache, the VM can easily garbage collect it as it is temporary. Constructor c = ctor; // Avoid thread races. if (c == null) stroke = new AWTKeyStroke(keyChar, keyCode, modifiers, release); else try { stroke = (AWTKeyStroke) c.newInstance(null); stroke.keyChar = keyChar; stroke.keyCode = keyCode; stroke.modifiers = modifiers; stroke.onKeyRelease = release; } catch (Exception e) { throw (Error) new InternalError().initCause(e); } // Check level 1 cache. AWTKeyStroke cached = (AWTKeyStroke) cache.get(stroke); if (cached == null) cache.put(stroke, stroke); else stroke = cached; return recent = stroke; } /** * Converts the modifiers to the appropriate format. * * @param mod the modifiers to convert * @return the adjusted modifiers */ private static int extend(int mod) { if ((mod & (KeyEvent.SHIFT_MASK | KeyEvent.SHIFT_DOWN_MASK)) != 0) mod |= KeyEvent.SHIFT_MASK | KeyEvent.SHIFT_DOWN_MASK; if ((mod & (KeyEvent.CTRL_MASK | KeyEvent.CTRL_DOWN_MASK)) != 0) mod |= KeyEvent.CTRL_MASK | KeyEvent.CTRL_DOWN_MASK; if ((mod & (KeyEvent.META_MASK | KeyEvent.META_DOWN_MASK)) != 0) mod |= KeyEvent.META_MASK | KeyEvent.META_DOWN_MASK; if ((mod & (KeyEvent.ALT_MASK | KeyEvent.ALT_DOWN_MASK)) != 0) mod |= KeyEvent.ALT_MASK | KeyEvent.ALT_DOWN_MASK; if ((mod & (KeyEvent.ALT_GRAPH_MASK | KeyEvent.ALT_GRAPH_DOWN_MASK)) != 0) mod |= KeyEvent.ALT_GRAPH_MASK | KeyEvent.ALT_GRAPH_DOWN_MASK; if ((mod & KeyEvent.BUTTON1_MASK) != 0) mod |= KeyEvent.BUTTON1_DOWN_MASK; return mod & MODIFIERS_MASK; }} // class AWTKeyStroke
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -