1. /*
  2. * @(#)KeyStroke.java 1.49 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.AWTKeyStroke;
  9. import java.awt.event.KeyEvent;
  10. /**
  11. * A KeyStroke represents a key action on the keyboard, or equivalent input
  12. * device. KeyStrokes can correspond to only a press or release of a particular
  13. * key, just as KEY_PRESSED and KEY_RELEASED KeyEvents do; alternately, they
  14. * can correspond to typing a specific Java character, just as KEY_TYPED
  15. * KeyEvents do. In all cases, KeyStrokes can specify modifiers (alt, shift,
  16. * control, meta, or a combination thereof) which must be present during the
  17. * action for an exact match.
  18. * <p>
  19. * KeyStrokes are used to define high-level (semantic) action events. Instead
  20. * of trapping every keystroke and throwing away the ones you are not
  21. * interested in, those keystrokes you care about automatically initiate
  22. * actions on the Components with which they are registered.
  23. * <p>
  24. * KeyStrokes are immutable, and are intended to be unique. Client code cannot
  25. * create a KeyStroke; a variant of <code>getKeyStroke</code> must be used
  26. * instead. These factory methods allow the KeyStroke implementation to cache
  27. * and share instances efficiently.
  28. * <p>
  29. * <strong>Warning:</strong>
  30. * Serialized objects of this class will not be compatible with
  31. * future Swing releases. The current serialization support is
  32. * appropriate for short term storage or RMI between applications running
  33. * the same version of Swing. As of 1.4, support for long term storage
  34. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  35. * has been added to the <code>java.beans</code> package.
  36. * Please see {@link java.beans.XMLEncoder}.
  37. *
  38. * @see javax.swing.text.Keymap
  39. * @see #getKeyStroke
  40. *
  41. * @version 1.49, 05/18/04
  42. * @author Arnaud Weber
  43. * @author David Mendenhall
  44. */
  45. public class KeyStroke extends AWTKeyStroke {
  46. /**
  47. * Serial Version ID.
  48. */
  49. private static final long serialVersionUID = -9060180771037902530L;
  50. private KeyStroke() {
  51. }
  52. private KeyStroke(char keyChar, int keyCode, int modifiers,
  53. boolean onKeyRelease) {
  54. super(keyChar, keyCode, modifiers, onKeyRelease);
  55. }
  56. /**
  57. * Returns a shared instance of a <code>KeyStroke</code>
  58. * that represents a <code>KEY_TYPED</code> event for the
  59. * specified character.
  60. *
  61. * @param keyChar the character value for a keyboard key
  62. * @return a KeyStroke object for that key
  63. */
  64. public static KeyStroke getKeyStroke(char keyChar) {
  65. synchronized (AWTKeyStroke.class) {
  66. registerSubclass(KeyStroke.class);
  67. return (KeyStroke)getAWTKeyStroke(keyChar);
  68. }
  69. }
  70. /**
  71. * Returns an instance of a KeyStroke, specifying whether the key is
  72. * considered to be activated when it is pressed or released. Unlike all
  73. * other factory methods in this class, the instances returned by this
  74. * method are not necessarily cached or shared.
  75. *
  76. * @param keyChar the character value for a keyboard key
  77. * @param onKeyRelease <code>true</code> if this KeyStroke corresponds to a
  78. * key release; <code>false</code> otherwise.
  79. * @return a KeyStroke object for that key
  80. * @deprecated use getKeyStroke(char)
  81. */
  82. @Deprecated
  83. public static KeyStroke getKeyStroke(char keyChar, boolean onKeyRelease) {
  84. return new KeyStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, onKeyRelease);
  85. }
  86. /**
  87. * Returns a shared instance of a KeyStroke, given a Character object and a
  88. * set of modifiers. Note that the first parameter is of type Character
  89. * rather than char. This is to avoid inadvertent clashes with calls to
  90. * <code>getKeyStroke(int keyCode, int modifiers)</code>.
  91. *
  92. * The modifiers consist of any combination of:<ul>
  93. * <li>java.awt.event.InputEvent.SHIFT_MASK (1)
  94. * <li>java.awt.event.InputEvent.CTRL_MASK (2)
  95. * <li>java.awt.event.InputEvent.META_MASK (4)
  96. * <li>java.awt.event.InputEvent.ALT_MASK (8)
  97. * </ul>
  98. * Since these numbers are all different powers of two, any combination of
  99. * them is an integer in which each bit represents a different modifier
  100. * key. Use 0 to specify no modifiers.
  101. *
  102. * @param keyChar the Character object for a keyboard character
  103. * @param modifiers a bitwise-ored combination of any modifiers
  104. * @return an KeyStroke object for that key
  105. * @throws IllegalArgumentException if keyChar is null
  106. *
  107. * @see java.awt.event.InputEvent
  108. * @since 1.3
  109. */
  110. public static KeyStroke getKeyStroke(Character keyChar, int modifiers) {
  111. synchronized (AWTKeyStroke.class) {
  112. registerSubclass(KeyStroke.class);
  113. return (KeyStroke)getAWTKeyStroke(keyChar, modifiers);
  114. }
  115. }
  116. /**
  117. * Returns a shared instance of a KeyStroke, given a numeric key code and a
  118. * set of modifiers, specifying whether the key is activated when it is
  119. * pressed or released.
  120. * <p>
  121. * The "virtual key" constants defined in java.awt.event.KeyEvent can be
  122. * used to specify the key code. For example:<ul>
  123. * <li>java.awt.event.KeyEvent.VK_ENTER
  124. * <li>java.awt.event.KeyEvent.VK_TAB
  125. * <li>java.awt.event.KeyEvent.VK_SPACE
  126. * </ul>
  127. * The modifiers consist of any combination of:<ul>
  128. * <li>java.awt.event.InputEvent.SHIFT_MASK (1)
  129. * <li>java.awt.event.InputEvent.CTRL_MASK (2)
  130. * <li>java.awt.event.InputEvent.META_MASK (4)
  131. * <li>java.awt.event.InputEvent.ALT_MASK (8)
  132. * </ul>
  133. * Since these numbers are all different powers of two, any combination of
  134. * them is an integer in which each bit represents a different modifier
  135. * key. Use 0 to specify no modifiers.
  136. *
  137. * @param keyCode an int specifying the numeric code for a keyboard key
  138. * @param modifiers a bitwise-ored combination of any modifiers
  139. * @param onKeyRelease <code>true</code> if the KeyStroke should represent
  140. * a key release; <code>false</code> otherwise.
  141. * @return a KeyStroke object for that key
  142. *
  143. * @see java.awt.event.KeyEvent
  144. * @see java.awt.event.InputEvent
  145. */
  146. public static KeyStroke getKeyStroke(int keyCode, int modifiers,
  147. boolean onKeyRelease) {
  148. synchronized (AWTKeyStroke.class) {
  149. registerSubclass(KeyStroke.class);
  150. return (KeyStroke)getAWTKeyStroke(keyCode, modifiers,
  151. onKeyRelease);
  152. }
  153. }
  154. /**
  155. * Returns a shared instance of a KeyStroke, given a numeric key code and a
  156. * set of modifiers. The returned KeyStroke will correspond to a key press.
  157. * <p>
  158. * The "virtual key" constants defined in java.awt.event.KeyEvent can be
  159. * used to specify the key code. For example:<ul>
  160. * <li>java.awt.event.KeyEvent.VK_ENTER
  161. * <li>java.awt.event.KeyEvent.VK_TAB
  162. * <li>java.awt.event.KeyEvent.VK_SPACE
  163. * </ul>
  164. * The modifiers consist of any combination of:<ul>
  165. * <li>java.awt.event.InputEvent.SHIFT_MASK (1)
  166. * <li>java.awt.event.InputEvent.CTRL_MASK (2)
  167. * <li>java.awt.event.InputEvent.META_MASK (4)
  168. * <li>java.awt.event.InputEvent.ALT_MASK (8)
  169. * </ul>
  170. * Since these numbers are all different powers of two, any combination of
  171. * them is an integer in which each bit represents a different modifier
  172. * key. Use 0 to specify no modifiers.
  173. *
  174. * @param keyCode an int specifying the numeric code for a keyboard key
  175. * @param modifiers a bitwise-ored combination of any modifiers
  176. * @return a KeyStroke object for that key
  177. *
  178. * @see java.awt.event.KeyEvent
  179. * @see java.awt.event.InputEvent
  180. */
  181. public static KeyStroke getKeyStroke(int keyCode, int modifiers) {
  182. synchronized (AWTKeyStroke.class) {
  183. registerSubclass(KeyStroke.class);
  184. return (KeyStroke)getAWTKeyStroke(keyCode, modifiers);
  185. }
  186. }
  187. /**
  188. * Returns a KeyStroke which represents the stroke which generated a given
  189. * KeyEvent.
  190. * <p>
  191. * This method obtains the keyChar from a KeyTyped event, and the keyCode
  192. * from a KeyPressed or KeyReleased event. The KeyEvent modifiers are
  193. * obtained for all three types of KeyEvent.
  194. *
  195. * @param anEvent the KeyEvent from which to obtain the KeyStroke
  196. * @throws NullPointerException if <code>anEvent</code> is null
  197. * @return the KeyStroke that precipitated the event
  198. */
  199. public static KeyStroke getKeyStrokeForEvent(KeyEvent anEvent) {
  200. synchronized (AWTKeyStroke.class) {
  201. registerSubclass(KeyStroke.class);
  202. return (KeyStroke)getAWTKeyStrokeForEvent(anEvent);
  203. }
  204. }
  205. /**
  206. * Parses a string and returns a <code>KeyStroke</code>.
  207. * The string must have the following syntax:
  208. * <pre>
  209. * <modifiers>* (<typedID> | <pressedReleasedID>)
  210. *
  211. * modifiers := shift | control | ctrl | meta | alt | altGraph
  212. * typedID := typed <typedKey>
  213. * typedKey := string of length 1 giving Unicode character.
  214. * pressedReleasedID := (pressed | released) key
  215. * key := KeyEvent key code name, i.e. the name following "VK_".
  216. * </pre>
  217. * If typed, pressed or released is not specified, pressed is assumed. Here
  218. * are some examples:
  219. * <pre>
  220. * "INSERT" => getKeyStroke(KeyEvent.VK_INSERT, 0);
  221. * "control DELETE" => getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
  222. * "alt shift X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
  223. * "alt shift released X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
  224. * "typed a" => getKeyStroke('a');
  225. * </pre>
  226. *
  227. * In order to maintain backward-compatibility, specifying a null String,
  228. * or a String which is formatted incorrectly, returns null.
  229. *
  230. * @param s a String formatted as described above
  231. * @return a KeyStroke object for that String, or null if the specified
  232. * String is null, or is formatted incorrectly
  233. */
  234. public static KeyStroke getKeyStroke(String s) {
  235. if (s == null || s.length() == 0) {
  236. return null;
  237. }
  238. synchronized (AWTKeyStroke.class) {
  239. registerSubclass(KeyStroke.class);
  240. try {
  241. return (KeyStroke)getAWTKeyStroke(s);
  242. } catch (IllegalArgumentException e) {
  243. return null;
  244. }
  245. }
  246. }
  247. }