< prev index next >

src/java.desktop/share/classes/java/awt/event/KeyEvent.java

Print this page




1152      *        {@code KEY_LOCATION_RIGHT}, and {@code KEY_LOCATION_NUMPAD}.
1153      * @throws IllegalArgumentException
1154      *     if {@code id} is {@code KEY_TYPED} and
1155      *       {@code keyChar} is {@code CHAR_UNDEFINED};
1156      *     or if {@code id} is {@code KEY_TYPED} and
1157      *       {@code keyCode} is not {@code VK_UNDEFINED};
1158      *     or if {@code id} is {@code KEY_TYPED} and
1159      *       {@code keyLocation} is not {@code KEY_LOCATION_UNKNOWN};
1160      *     or if {@code keyLocation} is not one of the legal
1161      *       values enumerated above.
1162      * @throws IllegalArgumentException if {@code source} is null
1163      * @see #getSource()
1164      * @see #getID()
1165      * @see #getWhen()
1166      * @see #getModifiers()
1167      * @see #getKeyCode()
1168      * @see #getKeyChar()
1169      * @see #getKeyLocation()
1170      * @since 1.4
1171      */

1172     public KeyEvent(Component source, int id, long when, int modifiers,
1173                     int keyCode, char keyChar, int keyLocation) {
1174         super(source, id, when, modifiers);
1175         if (id == KEY_TYPED) {
1176             if (keyChar == CHAR_UNDEFINED) {
1177                 throw new IllegalArgumentException("invalid keyChar");
1178             }
1179             if (keyCode != VK_UNDEFINED) {
1180                 throw new IllegalArgumentException("invalid keyCode");
1181             }
1182             if (keyLocation != KEY_LOCATION_UNKNOWN) {
1183                 throw new IllegalArgumentException("invalid keyLocation");
1184             }
1185         }
1186 
1187         this.keyCode = keyCode;
1188         this.keyChar = keyChar;
1189 
1190         if ((keyLocation < KEY_LOCATION_UNKNOWN) ||
1191             (keyLocation > KEY_LOCATION_NUMPAD)) {


1544         String unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
1545         return unknown + " keyCode: 0x" + Integer.toString(keyCode, 16);
1546     }
1547 
1548     /**
1549      * Returns a {@code String} describing the modifier key(s),
1550      * such as "Shift", or "Ctrl+Shift".  These strings can be
1551      * localized by changing the {@code awt.properties} file.
1552      * <p>
1553      * Note that {@code InputEvent.ALT_MASK} and
1554      * {@code InputEvent.BUTTON2_MASK} have the same value,
1555      * so the string "Alt" is returned for both modifiers.  Likewise,
1556      * {@code InputEvent.META_MASK} and
1557      * {@code InputEvent.BUTTON3_MASK} have the same value,
1558      * so the string "Meta" is returned for both modifiers.
1559      *
1560      * @param modifiers the modifier mask to be processed
1561      * @return string a text description of the combination of modifier
1562      *                keys that were held down during the event
1563      * @see InputEvent#getModifiersExText(int)


1564      */

1565     public static String getKeyModifiersText(int modifiers) {
1566         StringBuilder buf = new StringBuilder();
1567         if ((modifiers & InputEvent.META_MASK) != 0) {
1568             buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
1569             buf.append("+");
1570         }
1571         if ((modifiers & InputEvent.CTRL_MASK) != 0) {
1572             buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
1573             buf.append("+");
1574         }
1575         if ((modifiers & InputEvent.ALT_MASK) != 0) {
1576             buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
1577             buf.append("+");
1578         }
1579         if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
1580             buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
1581             buf.append("+");
1582         }
1583         if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
1584             buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));


1679           case VK_PASTE:
1680           case VK_CUT:
1681           case VK_FIND:
1682           case VK_PROPS:
1683           case VK_STOP:
1684 
1685           case VK_HELP:
1686           case VK_WINDOWS:
1687           case VK_CONTEXT_MENU:
1688               return true;
1689         }
1690         return false;
1691     }
1692 
1693     /**
1694      * Returns a parameter string identifying this event.
1695      * This method is useful for event logging and for debugging.
1696      *
1697      * @return a string identifying the event and its attributes
1698      */

1699     public String paramString() {
1700         StringBuilder str = new StringBuilder(100);
1701 
1702         switch (id) {
1703           case KEY_PRESSED:
1704             str.append("KEY_PRESSED");
1705             break;
1706           case KEY_RELEASED:
1707             str.append("KEY_RELEASED");
1708             break;
1709           case KEY_TYPED:
1710             str.append("KEY_TYPED");
1711             break;
1712           default:
1713             str.append("unknown type");
1714             break;
1715         }
1716 
1717         str.append(",keyCode=").append(keyCode);
1718         str.append(",keyText=").append(getKeyText(keyCode));


1804      * Returns an extended key code for a unicode character.
1805      *
1806      * @param c the unicode character to be processed
1807      * @return for a unicode character with a corresponding {@code VK_} constant -- this
1808      *   {@code VK_} constant; for a character appearing on the primary
1809      *   level of a known keyboard layout -- a unique integer.
1810      *   If a character does not appear on the primary level of a known keyboard,
1811      *   {@code VK_UNDEFINED} is returned.
1812      *
1813      * @since 1.7
1814      */
1815     public static int getExtendedKeyCodeForChar(int c) {
1816         // Return a keycode (if any) associated with a character.
1817         return sun.awt.ExtendedKeyCodes.getExtendedKeyCodeForChar(c);
1818     }
1819 
1820     /**
1821      * Sets new modifiers by the old ones. The key modifiers
1822      * override overlapping mouse modifiers.
1823      */

1824     private void setNewModifiers() {
1825         if ((modifiers & SHIFT_MASK) != 0) {
1826             modifiers |= SHIFT_DOWN_MASK;
1827         }
1828         if ((modifiers & ALT_MASK) != 0) {
1829             modifiers |= ALT_DOWN_MASK;
1830         }
1831         if ((modifiers & CTRL_MASK) != 0) {
1832             modifiers |= CTRL_DOWN_MASK;
1833         }
1834         if ((modifiers & META_MASK) != 0) {
1835             modifiers |= META_DOWN_MASK;
1836         }
1837         if ((modifiers & ALT_GRAPH_MASK) != 0) {
1838             modifiers |= ALT_GRAPH_DOWN_MASK;
1839         }
1840         if ((modifiers & BUTTON1_MASK) != 0) {
1841             modifiers |= BUTTON1_DOWN_MASK;
1842         }
1843     }
1844 
1845     /**
1846      * Sets old modifiers by the new ones.
1847      */

1848     private void setOldModifiers() {
1849         if ((modifiers & SHIFT_DOWN_MASK) != 0) {
1850             modifiers |= SHIFT_MASK;
1851         }
1852         if ((modifiers & ALT_DOWN_MASK) != 0) {
1853             modifiers |= ALT_MASK;
1854         }
1855         if ((modifiers & CTRL_DOWN_MASK) != 0) {
1856             modifiers |= CTRL_MASK;
1857         }
1858         if ((modifiers & META_DOWN_MASK) != 0) {
1859             modifiers |= META_MASK;
1860         }
1861         if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
1862             modifiers |= ALT_GRAPH_MASK;
1863         }
1864         if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
1865             modifiers |= BUTTON1_MASK;
1866         }
1867     }
1868 
1869     /**
1870      * Sets new modifiers by the old ones. The key modifiers
1871      * override overlapping mouse modifiers.
1872      * @serial
1873      */

1874     private void readObject(ObjectInputStream s)
1875       throws IOException, ClassNotFoundException {
1876         s.defaultReadObject();
1877         if (getModifiers() != 0 && getModifiersEx() == 0) {
1878             setNewModifiers();
1879         }
1880     }
1881 }


1152      *        {@code KEY_LOCATION_RIGHT}, and {@code KEY_LOCATION_NUMPAD}.
1153      * @throws IllegalArgumentException
1154      *     if {@code id} is {@code KEY_TYPED} and
1155      *       {@code keyChar} is {@code CHAR_UNDEFINED};
1156      *     or if {@code id} is {@code KEY_TYPED} and
1157      *       {@code keyCode} is not {@code VK_UNDEFINED};
1158      *     or if {@code id} is {@code KEY_TYPED} and
1159      *       {@code keyLocation} is not {@code KEY_LOCATION_UNKNOWN};
1160      *     or if {@code keyLocation} is not one of the legal
1161      *       values enumerated above.
1162      * @throws IllegalArgumentException if {@code source} is null
1163      * @see #getSource()
1164      * @see #getID()
1165      * @see #getWhen()
1166      * @see #getModifiers()
1167      * @see #getKeyCode()
1168      * @see #getKeyChar()
1169      * @see #getKeyLocation()
1170      * @since 1.4
1171      */
1172     @SuppressWarnings("deprecation")
1173     public KeyEvent(Component source, int id, long when, int modifiers,
1174                     int keyCode, char keyChar, int keyLocation) {
1175         super(source, id, when, modifiers);
1176         if (id == KEY_TYPED) {
1177             if (keyChar == CHAR_UNDEFINED) {
1178                 throw new IllegalArgumentException("invalid keyChar");
1179             }
1180             if (keyCode != VK_UNDEFINED) {
1181                 throw new IllegalArgumentException("invalid keyCode");
1182             }
1183             if (keyLocation != KEY_LOCATION_UNKNOWN) {
1184                 throw new IllegalArgumentException("invalid keyLocation");
1185             }
1186         }
1187 
1188         this.keyCode = keyCode;
1189         this.keyChar = keyChar;
1190 
1191         if ((keyLocation < KEY_LOCATION_UNKNOWN) ||
1192             (keyLocation > KEY_LOCATION_NUMPAD)) {


1545         String unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
1546         return unknown + " keyCode: 0x" + Integer.toString(keyCode, 16);
1547     }
1548 
1549     /**
1550      * Returns a {@code String} describing the modifier key(s),
1551      * such as "Shift", or "Ctrl+Shift".  These strings can be
1552      * localized by changing the {@code awt.properties} file.
1553      * <p>
1554      * Note that {@code InputEvent.ALT_MASK} and
1555      * {@code InputEvent.BUTTON2_MASK} have the same value,
1556      * so the string "Alt" is returned for both modifiers.  Likewise,
1557      * {@code InputEvent.META_MASK} and
1558      * {@code InputEvent.BUTTON3_MASK} have the same value,
1559      * so the string "Meta" is returned for both modifiers.
1560      *
1561      * @param modifiers the modifier mask to be processed
1562      * @return string a text description of the combination of modifier
1563      *                keys that were held down during the event
1564      * @see InputEvent#getModifiersExText(int)
1565      * @deprecated It is recommended that extended modifier keys and
1566      *             {@link InputEvent#getModifiersExText(int)} be used instead
1567      */
1568     @Deprecated(since = "9")
1569     public static String getKeyModifiersText(int modifiers) {
1570         StringBuilder buf = new StringBuilder();
1571         if ((modifiers & InputEvent.META_MASK) != 0) {
1572             buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
1573             buf.append("+");
1574         }
1575         if ((modifiers & InputEvent.CTRL_MASK) != 0) {
1576             buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
1577             buf.append("+");
1578         }
1579         if ((modifiers & InputEvent.ALT_MASK) != 0) {
1580             buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
1581             buf.append("+");
1582         }
1583         if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
1584             buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
1585             buf.append("+");
1586         }
1587         if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
1588             buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));


1683           case VK_PASTE:
1684           case VK_CUT:
1685           case VK_FIND:
1686           case VK_PROPS:
1687           case VK_STOP:
1688 
1689           case VK_HELP:
1690           case VK_WINDOWS:
1691           case VK_CONTEXT_MENU:
1692               return true;
1693         }
1694         return false;
1695     }
1696 
1697     /**
1698      * Returns a parameter string identifying this event.
1699      * This method is useful for event logging and for debugging.
1700      *
1701      * @return a string identifying the event and its attributes
1702      */
1703     @SuppressWarnings("deprecation")
1704     public String paramString() {
1705         StringBuilder str = new StringBuilder(100);
1706 
1707         switch (id) {
1708           case KEY_PRESSED:
1709             str.append("KEY_PRESSED");
1710             break;
1711           case KEY_RELEASED:
1712             str.append("KEY_RELEASED");
1713             break;
1714           case KEY_TYPED:
1715             str.append("KEY_TYPED");
1716             break;
1717           default:
1718             str.append("unknown type");
1719             break;
1720         }
1721 
1722         str.append(",keyCode=").append(keyCode);
1723         str.append(",keyText=").append(getKeyText(keyCode));


1809      * Returns an extended key code for a unicode character.
1810      *
1811      * @param c the unicode character to be processed
1812      * @return for a unicode character with a corresponding {@code VK_} constant -- this
1813      *   {@code VK_} constant; for a character appearing on the primary
1814      *   level of a known keyboard layout -- a unique integer.
1815      *   If a character does not appear on the primary level of a known keyboard,
1816      *   {@code VK_UNDEFINED} is returned.
1817      *
1818      * @since 1.7
1819      */
1820     public static int getExtendedKeyCodeForChar(int c) {
1821         // Return a keycode (if any) associated with a character.
1822         return sun.awt.ExtendedKeyCodes.getExtendedKeyCodeForChar(c);
1823     }
1824 
1825     /**
1826      * Sets new modifiers by the old ones. The key modifiers
1827      * override overlapping mouse modifiers.
1828      */
1829     @SuppressWarnings("deprecation")
1830     private void setNewModifiers() {
1831         if ((modifiers & SHIFT_MASK) != 0) {
1832             modifiers |= SHIFT_DOWN_MASK;
1833         }
1834         if ((modifiers & ALT_MASK) != 0) {
1835             modifiers |= ALT_DOWN_MASK;
1836         }
1837         if ((modifiers & CTRL_MASK) != 0) {
1838             modifiers |= CTRL_DOWN_MASK;
1839         }
1840         if ((modifiers & META_MASK) != 0) {
1841             modifiers |= META_DOWN_MASK;
1842         }
1843         if ((modifiers & ALT_GRAPH_MASK) != 0) {
1844             modifiers |= ALT_GRAPH_DOWN_MASK;
1845         }
1846         if ((modifiers & BUTTON1_MASK) != 0) {
1847             modifiers |= BUTTON1_DOWN_MASK;
1848         }
1849     }
1850 
1851     /**
1852      * Sets old modifiers by the new ones.
1853      */
1854     @SuppressWarnings("deprecation")
1855     private void setOldModifiers() {
1856         if ((modifiers & SHIFT_DOWN_MASK) != 0) {
1857             modifiers |= SHIFT_MASK;
1858         }
1859         if ((modifiers & ALT_DOWN_MASK) != 0) {
1860             modifiers |= ALT_MASK;
1861         }
1862         if ((modifiers & CTRL_DOWN_MASK) != 0) {
1863             modifiers |= CTRL_MASK;
1864         }
1865         if ((modifiers & META_DOWN_MASK) != 0) {
1866             modifiers |= META_MASK;
1867         }
1868         if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
1869             modifiers |= ALT_GRAPH_MASK;
1870         }
1871         if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
1872             modifiers |= BUTTON1_MASK;
1873         }
1874     }
1875 
1876     /**
1877      * Sets new modifiers by the old ones. The key modifiers
1878      * override overlapping mouse modifiers.
1879      * @serial
1880      */
1881     @SuppressWarnings("deprecation")
1882     private void readObject(ObjectInputStream s)
1883       throws IOException, ClassNotFoundException {
1884         s.defaultReadObject();
1885         if (getModifiers() != 0 && getModifiersEx() == 0) {
1886             setNewModifiers();
1887         }
1888     }
1889 }
< prev index next >