⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyevent.java

📁 JAVA基本类源代码,大家可以学习学习!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     */    public static final int KEY_LOCATION_STANDARD = 1;    /**     * A constant indicating that the key pressed or released is in     * the left key location (there is more than one possible location     * for this key).  Example: the left shift key.     * @since 1.4     */    public static final int KEY_LOCATION_LEFT     = 2;    /**     * A constant indicating that the key pressed or released is in     * the right key location (there is more than one possible location     * for this key).  Example: the right shift key.     * @since 1.4     */    public static final int KEY_LOCATION_RIGHT    = 3;    /**     * A constant indicating that the key event originated on the     * numeric keypad or with a virtual key corresponding to the     * numeric keypad.     * @since 1.4     */    public static final int KEY_LOCATION_NUMPAD   = 4;    /**     * The unique value assigned to each of the keys on the     * keyboard.  There is a common set of key codes that     * can be fired by most keyboards.     * The symbolic name for a key code should be used rather     * than the code value itself.     *     * @serial     * @see #getKeyCode()     * @see #setKeyCode(int)     */    int  keyCode;    /**     * <code>keyChar</code> is a valid unicode character     * that is fired by a key or a key combination on     * a keyboard.     *     * @serial     * @see #getKeyChar()     * @see #setKeyChar(char)     */    char keyChar;    /**     * The location of the key on the keyboard.     *     * Some keys occur more than once on a keyboard, e.g. the left and     * right shift keys.  Additionally, some keys occur on the numeric     * keypad.  This variable is used to distinguish such keys.     *     * The only legal values are <code>KEY_LOCATION_UNKNOWN</code>,      * <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,      * <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.     *     * @serial     * @see #getKeyLocation()     */    int keyLocation;    /*     * JDK 1.1 serialVersionUID      */    private static final long serialVersionUID = -2352130953028126954L;    static {        /* ensure that the necessary native libraries are loaded */	NativeLibLoader.loadLibraries();        if (!GraphicsEnvironment.isHeadless()) {            initIDs();        }    }    /**     * Initialize JNI field and method IDs for fields that may be     * accessed from C.     */    private static native void initIDs();    /**     * Constructs a <code>KeyEvent</code> object.     * <p>Note that passing in an invalid <code>id</code> results in     * unspecified behavior.     *     * @param source    the <code>Component</code> that originated the event     * @param id        an integer identifying the type of event     * @param when      a long integer that specifies the time the event     *                  occurred     * @param modifiers the modifier keys down during event (shift, ctrl,     *                  alt, meta)     *                  Either extended _DOWN_MASK or old _MASK modifiers     *                  should be used, but both models should not be mixed     *                  in one event. Use of the extended modifiers is     *                  preferred.     * @param keyCode   the integer code for an actual key, or VK_UNDEFINED     *                  (for a key-typed event)     * @param keyChar   the Unicode character generated by this event, or     *                  CHAR_UNDEFINED (for key-pressed and key-released     *                  events which do not map to a valid Unicode character)     * @param keyLocation  identifies the key location.  The only legal     *        values are <code>KEY_LOCATION_UNKNOWN</code>,      *        <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,      *        <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.     * @exception IllegalArgumentException       *     if <code>id</code> is <code>KEY_TYPED</code> and      *       <code>keyChar</code> is <code>CHAR_UNDEFINED</code>;      *     or if <code>id</code> is <code>KEY_TYPED</code> and      *       <code>keyCode</code> is not <code>VK_UNDEFINED</code>;      *     or if <code>id</code> is <code>KEY_TYPED</code> and     *       <code>keyLocation</code> is not <code>KEY_LOCATION_UNKNOWN</code>;     *     or if <code>keyLocation</code> is not one of the legal      *       values enumerated above.       * @since 1.4     */    private KeyEvent(Component source, int id, long when, int modifiers,                    int keyCode, char keyChar, int keyLocation, boolean isProxyActive) {        this(source, id, when, modifiers, keyCode, keyChar, keyLocation);        this.isProxyActive = isProxyActive;    }    public KeyEvent(Component source, int id, long when, int modifiers,                    int keyCode, char keyChar, int keyLocation) {        super(source, id, when, modifiers);        if (id == KEY_TYPED) {            if (keyChar == CHAR_UNDEFINED) {                throw new IllegalArgumentException("invalid keyChar");            }            if (keyCode != VK_UNDEFINED) {                throw new IllegalArgumentException("invalid keyCode");            }            if (keyLocation != KEY_LOCATION_UNKNOWN) {                throw new IllegalArgumentException("invalid keyLocation");            }        }        this.keyCode = keyCode;        this.keyChar = keyChar;        if ((keyLocation < KEY_LOCATION_UNKNOWN) ||            (keyLocation > KEY_LOCATION_NUMPAD)) {            throw new IllegalArgumentException("invalid keyLocation");        }        this.keyLocation = keyLocation;        if ((getModifiers() != 0) && (getModifiersEx() == 0)) {	    setNewModifiers();    	} else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {	    setOldModifiers();	}    }    /**     * Constructs a <code>KeyEvent</code> object.     * <p>Note that passing in an invalid <code>id</code> results in     * unspecified behavior.     *     * @param source    the <code>Component</code> that originated the event     * @param id        an integer identifying the type of event     * @param when      a long integer that specifies the time the event     *                  occurred     * @param modifiers the modifier keys down during event (shift, ctrl,     *                  alt, meta)     *                  Either extended _DOWN_MASK or old _MASK modifiers     *                  should be used, but both models should not be mixed     *                  in one event. Use of the extended modifiers is     *                  preferred.     * @param keyCode   the integer code for an actual key, or VK_UNDEFINED      *                  (for a key-typed event)     * @param keyChar   the Unicode character generated by this event, or      *                  CHAR_UNDEFINED (for key-pressed and key-released     *                  events which do not map to a valid Unicode character)     * @exception IllegalArgumentException  if <code>id</code> is     *     <code>KEY_TYPED</code> and <code>keyChar</code> is     *     <code>CHAR_UNDEFINED</code>; or if <code>id</code> is     *     <code>KEY_TYPED</code> and <code>keyCode</code> is not     *     <code>VK_UNDEFINED</code>     */    public KeyEvent(Component source, int id, long when, int modifiers,                    int keyCode, char keyChar) {        this(source, id, when, modifiers, keyCode, keyChar,          KEY_LOCATION_UNKNOWN);    }    /**     * @deprecated as of JDK1.1      */    public KeyEvent(Component source, int id, long when, int modifiers,                    int keyCode) {        this(source, id, when, modifiers, keyCode, (char)keyCode);    }    /**     * Returns the integer keyCode associated with the key in this event.     *      * @return the integer code for an actual key on the keyboard.      *         (For <code>KEY_TYPED</code> events, the keyCode is      *         <code>VK_UNDEFINED</code>.)     */    public int getKeyCode() {        return keyCode;    }    /**     * Set the keyCode value to indicate a physical key.     *     * @param keyCode an integer corresponding to an actual key on the keyboard.     */    public void setKeyCode(int keyCode) {        this.keyCode = keyCode;    }    /**     * Returns the character associated with the key in this event.     * For example, the key-typed event for shift + "a" returns the      * value for "A".     *     * @return the Unicode character defined for this key event.     *         If no valid Unicode character exists for this key event,      *         keyChar is <code>CHAR_UNDEFINED</code>.     */    public char getKeyChar() {        return keyChar;    }    /**     * Set the keyChar value to indicate a logical character.     *     * @param keyChar a char corresponding to to the combination of keystrokes     *                that make up this event.     */    public void setKeyChar(char keyChar) {        this.keyChar = keyChar;    }    /**     * Set the modifiers to indicate additional keys that were held down     * (e.g. shift, ctrl, alt, meta) defined as part of InputEvent.     * <p>     * NOTE:  use of this method is not recommended, because many AWT     * implementations do not recognize modifier changes.  This is     * especially true for <code>KEY_TYPED</code> events where the shift      * modifier is changed.     *     * @param modifiers an integer combination of the modifier constants.     * @see InputEvent     * @deprecated as of JDK1.1.4     */    public void setModifiers(int modifiers) {        this.modifiers = modifiers;        if ((getModifiers() != 0) && (getModifiersEx() == 0)) {	    setNewModifiers();    	} else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {	    setOldModifiers();	}    }    /**     * Returns the location of the key that originated this key event.     *     * Some keys occur more than once on a keyboard, e.g. the left and     * right shift keys.  Additionally, some keys occur on the numeric     * keypad.  This provides a way of distinguishing such keys.     *     * @return the location of the key that was pressed or released.     *         Always returns <code>KEY_LOCATION_UNKNOWN</code> for      *         <code>KEY_TYPED</code> events.     * @since 1.4     */    public int getKeyLocation() {        return keyLocation;    }    /**     * Returns a String describing the keyCode, such as "HOME", "F1" or "A".     * These strings can be localized by changing the awt.properties file.     *     * @return a string containing a text description for a physical key,     *         identified by its keyCode     */    public static String getKeyText(int keyCode) {        if (keyCode >= VK_0 && keyCode <= VK_9 ||             keyCode >= VK_A && keyCode <= VK_Z) {            return String.valueOf((char)keyCode);        }        // Check for other ASCII keyCodes.        int index = ",./;=[\\]".indexOf(keyCode);        if (index >= 0) {            return String.valueOf((char)keyCode);        }	        switch(keyCode) {          case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter");          case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace");          case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab");          case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel");          case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear");          case VK_SHIFT: return Toolkit.getProperty("AWT.shift", "Shift");          case VK_CONTROL: return Toolkit.getProperty("AWT.control", "Control");          case VK_ALT: return Toolkit.getProperty("AWT.alt", "Alt");          case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause");          case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock");          case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape");          case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space");          case VK_PAGE_UP: return Toolkit.getProperty("AWT.pgup", "Page Up");          case VK_PAGE_DOWN: return Toolkit.getProperty("AWT.pgdn", "Page Down");          case VK_END: return Toolkit.getProperty("AWT.end", "End");          case VK_HOME: return Toolkit.getProperty("AWT.home", "Home");          case VK_LEFT: return Toolkit.getProperty("AWT.left", "Left");          case VK_UP: return Toolkit.getProperty("AWT.up", "Up");          case VK_RIGHT: return Toolkit.getProperty("AWT.right", "Right");          case VK_DOWN: return Toolkit.getProperty("AWT.down", "Down");          // handled by ASCII value above:          //   comma, period, slash, 0-9, semicolon, equals, A-Z,          //   open_bracket, back_slash, close_bracket          // numpad numeric keys handled below          case VK_MULTIPLY: return Toolkit.getProperty("AWT.multiply", "NumPad *");          case VK_ADD: return Toolkit.getProperty("AWT.add", "NumPad +");          case VK_SEPARATOR: return Toolkit.getProperty("AWT.separator", "NumPad ,");          case VK_SUBTRACT: return Toolkit.getProperty("AWT.subtract", "NumPad -");          case VK_DECIMAL: return Toolkit.getProperty("AWT.decimal", "NumPad .");          case VK_DIVIDE: return Toolkit.getProperty("AWT.divide", "NumPad /");          case VK_DELETE: return Toolkit.getProperty("AWT.delete", "Delete");          case VK_NUM_LOCK: return Toolkit.getProperty("AWT.numLock", "Num Lock");          case VK_SCROLL_LOCK: return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock");          case VK_F1: return Toolkit.getProperty("AWT.f1", "F1");          case VK_F2: return Toolkit.getProperty("AWT.f2", "F2");          case VK_F3: return Toolkit.getProperty("AWT.f3", "F3");          case VK_F4: return Toolkit.getProperty("AWT.f4", "F4");          case VK_F5: return Toolkit.getProperty("AWT.f5", "F5");          case VK_F6: return Toolkit.getProperty("AWT.f6", "F6");          case VK_F7: return Toolkit.getProperty("AWT.f7", "F7");          case VK_F8: return Toolkit.getProperty("AWT.f8", "F8");          case VK_F9: return Toolkit.getProperty("AWT.f9", "F9");          case VK_F10: return Toolkit.getProperty("AWT.f10", "F10");          case VK_F11: return Toolkit.getProperty("AWT.f11", "F11");          case VK_F12: return Toolkit.getProperty("AWT.f12", "F12");          case VK_F13: return Toolkit.getProperty("AWT.f13", "F13");          case VK_F14: return Toolkit.getProperty("AWT.f14", "F14");          case VK_F15: return Toolkit.getProperty("AWT.f15", "F15");          case VK_F16: return Toolkit.getProperty("AWT.f16", "F16");          case VK_F17: return Toolkit.getProperty("AWT.f17", "F17");          case VK_F18: return Toolkit.getProperty("AWT.f18", "F18");          case VK_F19: return Toolkit.getProperty("AWT.f19", "F19");          case VK_F20: return Toolkit.getProperty("AWT.f20", "F20");          case VK_F21: return Toolkit.getProperty("AWT.f21", "F21");          case VK_F22: return Toolkit.getProperty("AWT.f22", "F22");          case VK_F23: return Toolkit.getProperty("AWT.f23", "F23");          case VK_F24: return Toolkit.getProperty("AWT.f24", "F24");          case VK_PRINTSCREEN: return Toolkit.getProperty("AWT.printScreen", "Print Screen");          case VK_INSERT: return Toolkit.getProperty("AWT.insert", "Insert");          case VK_HELP: return Toolkit.getProperty("AWT.help", "Help");          case VK_META: return Toolkit.getProperty("AWT.meta", "Meta");          case VK_BACK_QUOTE: return Toolkit.getProperty("AWT.backQuote", "Back Quote");          case VK_QUOTE: return Toolkit.getProperty("AWT.quote", "Quote");			           case VK_KP_UP: return Toolkit.getProperty("AWT.up", "Up");          case VK_KP_DOWN: return Toolkit.getProperty("AWT.down", "Down");          case VK_KP_LEFT: return Toolkit.getProperty("AWT.left", "Left");          case VK_KP_RIGHT: return Toolkit.getProperty("AWT.right", "Right");          case VK_DEAD_GRAVE: return Toolkit.getProperty("AWT.deadGrave", "Dead Grave");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -