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

Print this page




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.event;
  27 
  28 import java.awt.Component;
  29 import java.awt.GraphicsEnvironment;
  30 import java.awt.Toolkit;
  31 import java.io.IOException;
  32 import java.io.ObjectInputStream;


  33 import sun.awt.AWTAccessor;
  34 
  35 /**
  36  * An event which indicates that a keystroke occurred in a component.
  37  * <p>
  38  * This low-level event is generated by a component object (such as a text
  39  * field) when a key is pressed, released, or typed.
  40  * The event is passed to every <code>KeyListener</code>
  41  * or <code>KeyAdapter</code> object which registered to receive such
  42  * events using the component's <code>addKeyListener</code> method.
  43  * (<code>KeyAdapter</code> objects implement the
  44  * <code>KeyListener</code> interface.)  Each such listener object
  45  * gets this <code>KeyEvent</code> when the event occurs.
  46  * <p>
  47  * <em>"Key typed" events</em> are higher-level and generally do not depend on
  48  * the platform or keyboard layout.  They are generated when a Unicode character
  49  * is entered, and are the preferred way to find out about character input.
  50  * In the simplest case, a key typed event is produced by a single key press
  51  * (e.g., 'a').  Often, however, characters are produced by series of key
  52  * presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to


1541     }
1542 
1543     /**
1544      * Returns a <code>String</code> describing the modifier key(s),
1545      * such as "Shift", or "Ctrl+Shift".  These strings can be
1546      * localized by changing the <code>awt.properties</code> file.
1547      * <p>
1548      * Note that <code>InputEvent.ALT_MASK</code> and
1549      * <code>InputEvent.BUTTON2_MASK</code> have the same value,
1550      * so the string "Alt" is returned for both modifiers.  Likewise,
1551      * <code>InputEvent.META_MASK</code> and
1552      * <code>InputEvent.BUTTON3_MASK</code> have the same value,
1553      * so the string "Meta" is returned for both modifiers.
1554      *
1555      * @param modifiers the modifier mask to be processed
1556      * @return string a text description of the combination of modifier
1557      *                keys that were held down during the event
1558      * @see InputEvent#getModifiersExText(int)
1559      */
1560     public static String getKeyModifiersText(int modifiers) {
1561         StringBuilder buf = new StringBuilder();
1562         if ((modifiers & InputEvent.META_MASK) != 0) {
1563             buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
1564             buf.append("+");
1565         }
1566         if ((modifiers & InputEvent.CTRL_MASK) != 0) {
1567             buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
1568             buf.append("+");
1569         }
1570         if ((modifiers & InputEvent.ALT_MASK) != 0) {
1571             buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
1572             buf.append("+");
1573         }
1574         if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
1575             buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
1576             buf.append("+");
1577         }
1578         if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
1579             buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
1580             buf.append("+");
1581         }
1582         if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
1583             buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
1584             buf.append("+");
1585         }
1586         if (buf.length() > 0) {
1587             buf.setLength(buf.length()-1); // remove trailing '+'
1588         }
1589         return buf.toString();
1590     }
1591 
1592 
1593     /**
1594      * Returns whether the key in this event is an "action" key.
1595      * Typically an action key does not fire a unicode character and is
1596      * not a modifier key.
1597      *
1598      * @return <code>true</code> if the key is an "action" key,
1599      *         <code>false</code> otherwise
1600      */
1601     public boolean isActionKey() {
1602         switch (keyCode) {
1603           case VK_HOME:
1604           case VK_END:
1605           case VK_PAGE_UP:
1606           case VK_PAGE_DOWN:
1607           case VK_UP:




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.event;
  27 
  28 import java.awt.Component;
  29 import java.awt.GraphicsEnvironment;
  30 import java.awt.Toolkit;
  31 import java.io.IOException;
  32 import java.io.ObjectInputStream;
  33 import java.util.StringJoiner;
  34 
  35 import sun.awt.AWTAccessor;
  36 
  37 /**
  38  * An event which indicates that a keystroke occurred in a component.
  39  * <p>
  40  * This low-level event is generated by a component object (such as a text
  41  * field) when a key is pressed, released, or typed.
  42  * The event is passed to every <code>KeyListener</code>
  43  * or <code>KeyAdapter</code> object which registered to receive such
  44  * events using the component's <code>addKeyListener</code> method.
  45  * (<code>KeyAdapter</code> objects implement the
  46  * <code>KeyListener</code> interface.)  Each such listener object
  47  * gets this <code>KeyEvent</code> when the event occurs.
  48  * <p>
  49  * <em>"Key typed" events</em> are higher-level and generally do not depend on
  50  * the platform or keyboard layout.  They are generated when a Unicode character
  51  * is entered, and are the preferred way to find out about character input.
  52  * In the simplest case, a key typed event is produced by a single key press
  53  * (e.g., 'a').  Often, however, characters are produced by series of key
  54  * presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to


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

1566         }
1567         if ((modifiers & InputEvent.CTRL_MASK) != 0) {
1568             buf.add(Toolkit.getProperty("AWT.control", "Ctrl"));

1569         }
1570         if ((modifiers & InputEvent.ALT_MASK) != 0) {
1571             buf.add(Toolkit.getProperty("AWT.alt", "Alt"));

1572         }
1573         if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
1574             buf.add(Toolkit.getProperty("AWT.shift", "Shift"));

1575         }
1576         if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
1577             buf.add(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));

1578         }
1579         if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
1580             buf.add(Toolkit.getProperty("AWT.button1", "Button1"));




1581         }
1582         return buf.toString();
1583     }
1584 
1585 
1586     /**
1587      * Returns whether the key in this event is an "action" key.
1588      * Typically an action key does not fire a unicode character and is
1589      * not a modifier key.
1590      *
1591      * @return <code>true</code> if the key is an "action" key,
1592      *         <code>false</code> otherwise
1593      */
1594     public boolean isActionKey() {
1595         switch (keyCode) {
1596           case VK_HOME:
1597           case VK_END:
1598           case VK_PAGE_UP:
1599           case VK_PAGE_DOWN:
1600           case VK_UP: