1. /*
  2. * @(#)KeyStroke.java 1.44 03/01/23
  3. *
  4. * Copyright 2003 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.44, 01/23/03
  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. public static KeyStroke getKeyStroke(char keyChar, boolean onKeyRelease) {
  83. return new KeyStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, onKeyRelease);
  84. }
  85. /**
  86. * Returns a shared instance of a KeyStroke, given a Character object and a
  87. * set of modifiers. Note that the first parameter is of type Character
  88. * rather than char. This is to avoid inadvertent clashes with calls to
  89. * <code>getKeyStroke(int keyCode, int modifiers)</code>.
  90. *
  91. * The modifiers consist of any combination of:<ul>
  92. * <li>java.awt.event.InputEvent.SHIFT_MASK (1)
  93. * <li>java.awt.event.InputEvent.CTRL_MASK (2)
  94. * <li>java.awt.event.InputEvent.META_MASK (4)
  95. * <li>java.awt.event.InputEvent.ALT_MASK (8)
  96. * </ul>
  97. * Since these numbers are all different powers of two, any combination of
  98. * them is an integer in which each bit represents a different modifier
  99. * key. Use 0 to specify no modifiers.
  100. *
  101. * @param keyChar the Character object for a keyboard character
  102. * @param modifiers a bitwise-ored combination of any modifiers
  103. * @return an KeyStroke object for that key
  104. * @throws IllegalArgumentException if keyChar is null
  105. *
  106. * @see java.awt.event.InputEvent
  107. * @since 1.3
  108. */
  109. public static KeyStroke getKeyStroke(Character keyChar, int modifiers) {
  110. synchronized (AWTKeyStroke.class) {
  111. registerSubclass(KeyStroke.class);
  112. return (KeyStroke)getAWTKeyStroke(keyChar, modifiers);
  113. }
  114. }
  115. /**
  116. * Returns a shared instance of a KeyStroke, given a numeric key code and a
  117. * set of modifiers, specifying whether the key is activated when it is
  118. * pressed or released.
  119. * <p>
  120. * The "virtual key" constants defined in java.awt.event.KeyEvent can be
  121. * used to specify the key code. For example:<ul>
  122. * <li>java.awt.event.KeyEvent.VK_ENTER
  123. * <li>java.awt.event.KeyEvent.VK_TAB
  124. * <li>java.awt.event.KeyEvent.VK_SPACE
  125. * </ul>
  126. * The modifiers consist of any combination of:<ul>
  127. * <li>java.awt.event.InputEvent.SHIFT_MASK (1)
  128. * <li>java.awt.event.InputEvent.CTRL_MASK (2)
  129. * <li>java.awt.event.InputEvent.META_MASK (4)
  130. * <li>java.awt.event.InputEvent.ALT_MASK (8)
  131. * </ul>
  132. * Since these numbers are all different powers of two, any combination of
  133. * them is an integer in which each bit represents a different modifier
  134. * key. Use 0 to specify no modifiers.
  135. *
  136. * @param keyCode an int specifying the numeric code for a keyboard key
  137. * @param modifiers a bitwise-ored combination of any modifiers
  138. * @param onKeyRelease <code>true</code> if the KeyStroke should represent
  139. * a key release; <code>false</code> otherwise.
  140. * @return a KeyStroke object for that key
  141. *
  142. * @see java.awt.event.KeyEvent
  143. * @see java.awt.event.InputEvent
  144. */
  145. public static KeyStroke getKeyStroke(int keyCode, int modifiers,
  146. boolean onKeyRelease) {
  147. synchronized (AWTKeyStroke.class) {
  148. registerSubclass(KeyStroke.class);
  149. return (KeyStroke)getAWTKeyStroke(keyCode, modifiers,
  150. onKeyRelease);
  151. }
  152. }
  153. /**
  154. * Returns a shared instance of a KeyStroke, given a numeric key code and a
  155. * set of modifiers. The returned KeyStroke will correspond to a key press.
  156. * <p>
  157. * The "virtual key" constants defined in java.awt.event.KeyEvent can be
  158. * used to specify the key code. For example:<ul>
  159. * <li>java.awt.event.KeyEvent.VK_ENTER
  160. * <li>java.awt.event.KeyEvent.VK_TAB
  161. * <li>java.awt.event.KeyEvent.VK_SPACE
  162. * </ul>
  163. * The modifiers consist of any combination of:<ul>
  164. * <li>java.awt.event.InputEvent.SHIFT_MASK (1)
  165. * <li>java.awt.event.InputEvent.CTRL_MASK (2)
  166. * <li>java.awt.event.InputEvent.META_MASK (4)
  167. * <li>java.awt.event.InputEvent.ALT_MASK (8)
  168. * </ul>
  169. * Since these numbers are all different powers of two, any combination of
  170. * them is an integer in which each bit represents a different modifier
  171. * key. Use 0 to specify no modifiers.
  172. *
  173. * @param keyCode an int specifying the numeric code for a keyboard key
  174. * @param modifiers a bitwise-ored combination of any modifiers
  175. * @return a KeyStroke object for that key
  176. *
  177. * @see java.awt.event.KeyEvent
  178. * @see java.awt.event.InputEvent
  179. */
  180. public static KeyStroke getKeyStroke(int keyCode, int modifiers) {
  181. synchronized (AWTKeyStroke.class) {
  182. registerSubclass(KeyStroke.class);
  183. return (KeyStroke)getAWTKeyStroke(keyCode, modifiers);
  184. }
  185. }
  186. /**
  187. * Returns a KeyStroke which represents the stroke which generated a given
  188. * KeyEvent.
  189. * <p>
  190. * This method obtains the keyChar from a KeyTyped event, and the key code
  191. * from a KeyPressed or KeyReleased event. The KeyEvent modifiers are
  192. * obtained for all three types of KeyEvent.
  193. *
  194. * @param anEvent the KeyEvent from which to obtain the KeyStroke
  195. * @return the KeyStroke that precipitated the event
  196. */
  197. public static KeyStroke getKeyStrokeForEvent(KeyEvent anEvent) {
  198. synchronized (AWTKeyStroke.class) {
  199. registerSubclass(KeyStroke.class);
  200. return (KeyStroke)getAWTKeyStrokeForEvent(anEvent);
  201. }
  202. }
  203. /**
  204. * Parses a string and returns a <code>KeyStroke</code>.
  205. * The string must have the following syntax:
  206. * <pre>
  207. * <modifiers>* (<typedID> | <pressedReleasedID>)
  208. *
  209. * modifiers := shift | control | ctrl | meta | alt | button1 | button2 | button3
  210. * typedID := typed <typedKey>
  211. * typedKey := string of length 1 giving Unicode character.
  212. * pressedReleasedID := (pressed | released) key
  213. * key := KeyEvent key code name, i.e. the name following "VK_".
  214. * </pre>
  215. * If typed, pressed or released is not specified, pressed is assumed. Here
  216. * are some examples:
  217. * <pre>
  218. * "INSERT" => getKeyStroke(KeyEvent.VK_INSERT, 0);
  219. * "control DELETE" => getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
  220. * "alt shift X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
  221. * "alt shift released X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
  222. * "typed a" => getKeyStroke('a');
  223. * </pre>
  224. *
  225. * In order to maintain backward-compatibility, specifying a null String,
  226. * or a String which is formatted incorrectly, returns null.
  227. *
  228. * @param s a String formatted as described above
  229. * @return a KeyStroke object for that String, or null if the specified
  230. * String is null, or is formatted incorrectly
  231. */
  232. public static KeyStroke getKeyStroke(String s) {
  233. if (s == null || s.length() == 0) {
  234. return null;
  235. }
  236. synchronized (AWTKeyStroke.class) {
  237. registerSubclass(KeyStroke.class);
  238. try {
  239. return (KeyStroke)getAWTKeyStroke(s);
  240. } catch (IllegalArgumentException e) {
  241. return null;
  242. }
  243. }
  244. }
  245. }