1. /*
  2. * @(#)InputEvent.java 1.29 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 java.awt.event;
  8. import java.awt.Event;
  9. import java.awt.Component;
  10. import java.awt.GraphicsEnvironment;
  11. import java.awt.Toolkit;
  12. /**
  13. * The root event class for all component-level input events.
  14. *
  15. * Input events are delivered to listeners before they are
  16. * processed normally by the source where they originated.
  17. * This allows listeners and component subclasses to "consume"
  18. * the event so that the source will not process them in their
  19. * default manner. For example, consuming mousePressed events
  20. * on a Button component will prevent the Button from being
  21. * activated.
  22. *
  23. * @author Carl Quinn
  24. * @version 1.29 01/23/03
  25. *
  26. * @see KeyEvent
  27. * @see KeyAdapter
  28. * @see MouseEvent
  29. * @see MouseAdapter
  30. * @see MouseMotionAdapter
  31. *
  32. * @since 1.1
  33. */
  34. public abstract class InputEvent extends ComponentEvent {
  35. /**
  36. * The Shift key modifier constant.
  37. * It is recommended that SHIFT_DOWN_MASK be used instead.
  38. */
  39. public static final int SHIFT_MASK = Event.SHIFT_MASK;
  40. /**
  41. * The Control key modifier constant.
  42. * It is recommended that CTRL_DOWN_MASK be used instead.
  43. */
  44. public static final int CTRL_MASK = Event.CTRL_MASK;
  45. /**
  46. * The Meta key modifier constant.
  47. * It is recommended that META_DOWN_MASK be used instead.
  48. */
  49. public static final int META_MASK = Event.META_MASK;
  50. /**
  51. * The Alt key modifier constant.
  52. * It is recommended that ALT_DOWN_MASK be used instead.
  53. */
  54. public static final int ALT_MASK = Event.ALT_MASK;
  55. /**
  56. * The AltGraph key modifier constant.
  57. */
  58. public static final int ALT_GRAPH_MASK = 1 << 5;
  59. /**
  60. * The Mouse Button1 modifier constant.
  61. * It is recommended that BUTTON1_DOWN_MASK be used instead.
  62. */
  63. public static final int BUTTON1_MASK = 1 << 4;
  64. /**
  65. * The Mouse Button2 modifier constant.
  66. * It is recommended that BUTTON2_DOWN_MASK be used instead.
  67. */
  68. public static final int BUTTON2_MASK = Event.ALT_MASK;
  69. /**
  70. * The Mouse Button3 modifier constant.
  71. * It is recommended that BUTTON3_DOWN_MASK be used instead.
  72. */
  73. public static final int BUTTON3_MASK = Event.META_MASK;
  74. /**
  75. * The Shift key extended modifier constant.
  76. * @since 1.4
  77. */
  78. public static final int SHIFT_DOWN_MASK = 1 << 6;
  79. /**
  80. * The Control key extended modifier constant.
  81. * @since 1.4
  82. */
  83. public static final int CTRL_DOWN_MASK = 1 << 7;
  84. /**
  85. * The Meta key extended modifier constant.
  86. * @since 1.4
  87. */
  88. public static final int META_DOWN_MASK = 1 << 8;
  89. /**
  90. * The Alt key extended modifier constant.
  91. * @since 1.4
  92. */
  93. public static final int ALT_DOWN_MASK = 1 << 9;
  94. /**
  95. * The Mouse Button1 extended modifier constant.
  96. * @since 1.4
  97. */
  98. public static final int BUTTON1_DOWN_MASK = 1 << 10;
  99. /**
  100. * The Mouse Button2 extended modifier constant.
  101. * @since 1.4
  102. */
  103. public static final int BUTTON2_DOWN_MASK = 1 << 11;
  104. /**
  105. * The Mouse Button3 extended modifier constant.
  106. * @since 1.4
  107. */
  108. public static final int BUTTON3_DOWN_MASK = 1 << 12;
  109. /**
  110. * The AltGraph key extended modifier constant.
  111. * @since 1.4
  112. */
  113. public static final int ALT_GRAPH_DOWN_MASK = 1 << 13;
  114. static final int JDK_1_3_MODIFIERS = SHIFT_DOWN_MASK - 1;
  115. /**
  116. * The input event's Time stamp in UTC format. The time stamp
  117. * indicates when the input event was created.
  118. *
  119. * @serial
  120. * @see #getWhen()
  121. */
  122. long when;
  123. /**
  124. * The state of the modifier mask at the time the input
  125. * event was fired.
  126. *
  127. * @serial
  128. * @see #getModifiers()
  129. * @see #getModifiersEx()
  130. * @see java.awt.event.KeyEvent
  131. * @see java.awt.event.MouseEvent
  132. */
  133. int modifiers;
  134. static {
  135. /* ensure that the necessary native libraries are loaded */
  136. NativeLibLoader.loadLibraries();
  137. if (!GraphicsEnvironment.isHeadless()) {
  138. initIDs();
  139. }
  140. }
  141. /**
  142. * Initialize JNI field and method IDs for fields that may be
  143. accessed from C.
  144. */
  145. private static native void initIDs();
  146. /**
  147. * Constructs an InputEvent object with the specified source component,
  148. * modifiers, and type.
  149. *
  150. * @param source the object where the event originated
  151. * @param id the event type
  152. * @param when the time the event occurred
  153. * @param modifiers represents the modifier keys and mouse buttons down
  154. * while the event occurred
  155. */
  156. InputEvent(Component source, int id, long when, int modifiers) {
  157. super(source, id);
  158. this.when = when;
  159. this.modifiers = modifiers;
  160. }
  161. /**
  162. * Returns whether or not the Shift modifier is down on this event.
  163. */
  164. public boolean isShiftDown() {
  165. return (modifiers & SHIFT_MASK) != 0;
  166. }
  167. /**
  168. * Returns whether or not the Control modifier is down on this event.
  169. */
  170. public boolean isControlDown() {
  171. return (modifiers & CTRL_MASK) != 0;
  172. }
  173. /**
  174. * Returns whether or not the Meta modifier is down on this event.
  175. */
  176. public boolean isMetaDown() {
  177. return (modifiers & META_MASK) != 0;
  178. }
  179. /**
  180. * Returns whether or not the Alt modifier is down on this event.
  181. */
  182. public boolean isAltDown() {
  183. return (modifiers & ALT_MASK) != 0;
  184. }
  185. /**
  186. * Returns whether or not the AltGraph modifier is down on this event.
  187. */
  188. public boolean isAltGraphDown() {
  189. return (modifiers & ALT_GRAPH_MASK) != 0;
  190. }
  191. /**
  192. * Returns the timestamp of when this event occurred.
  193. */
  194. public long getWhen() {
  195. return when;
  196. }
  197. /**
  198. * Returns the modifier mask for this event.
  199. */
  200. public int getModifiers() {
  201. return modifiers & JDK_1_3_MODIFIERS;
  202. }
  203. /**
  204. * Returns the extended modifier mask for this event.
  205. * Extended modifiers represent the state of all modal keys,
  206. * such as ALT, CTRL, META, and the mouse buttons just after
  207. * the event occurred
  208. * <P>
  209. * For example, if the user presses <b>button 1</b> followed by
  210. * <b>button 2</b>, and then releases them in the same order,
  211. * the following sequence of events is generated:
  212. * <PRE>
  213. * <code>MOUSE_PRESSED</code>: <code>BUTTON1_DOWN_MASK</code>
  214. * <code>MOUSE_PRESSED</code>: <code>BUTTON1_DOWN_MASK | BUTTON2_DOWN_MASK</code>
  215. * <code>MOUSE_RELEASED</code>: <code>BUTTON2_DOWN_MASK</code>
  216. * <code>MOUSE_CLICKED</code>: <code>BUTTON2_DOWN_MASK</code>
  217. * <code>MOUSE_RELEASED</code>:
  218. * <code>MOUSE_CLICKED</code>:
  219. * </PRE>
  220. * <P>
  221. * It is not recommended to compare the return value of this method
  222. * using <code>==</code> because new modifiers can be added in the future.
  223. * For example, the appropriate way to check that SHIFT and BUTTON1 are
  224. * down, but CTRL is up is demonstrated by the following code:
  225. * <PRE>
  226. * int onmask = SHIFT_DOWN_MASK | BUTTON1_DOWN_MASK;
  227. * int offmask = CTRL_DOWN_MASK;
  228. * if (event.getModifiersEx() & (onmask | offmask) == onmask) {
  229. * ...
  230. * }
  231. * </PRE>
  232. * The above code will work even if new modifiers are added.
  233. *
  234. * @since 1.4
  235. */
  236. public int getModifiersEx() {
  237. return modifiers & ~JDK_1_3_MODIFIERS;
  238. }
  239. /**
  240. * Consumes this event so that it will not be processed
  241. * in the default manner by the source which originated it.
  242. */
  243. public void consume() {
  244. consumed = true;
  245. }
  246. /**
  247. * Returns whether or not this event has been consumed.
  248. * @see #consume
  249. */
  250. public boolean isConsumed() {
  251. return consumed;
  252. }
  253. // state serialization compatibility with JDK 1.1
  254. static final long serialVersionUID = -2482525981698309786L;
  255. /**
  256. * Returns a String describing the extended modifier keys and
  257. * mouse buttons, such as "Shift", "Button1", or "Ctrl+Shift".
  258. * These strings can be localized by changing the
  259. * awt.properties file.
  260. *
  261. * @param modifiers a modifier mask describing the extended
  262. * modifier keys and mouse buttons for the event
  263. * @return a text description of the combination of extended
  264. * modifier keys and mouse buttons that were held down
  265. * during the event.
  266. * @since 1.4
  267. */
  268. public static String getModifiersExText(int modifiers) {
  269. StringBuffer buf = new StringBuffer();
  270. if ((modifiers & InputEvent.META_DOWN_MASK) != 0) {
  271. buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
  272. buf.append("+");
  273. }
  274. if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
  275. buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
  276. buf.append("+");
  277. }
  278. if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) {
  279. buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
  280. buf.append("+");
  281. }
  282. if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
  283. buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
  284. buf.append("+");
  285. }
  286. if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
  287. buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
  288. buf.append("+");
  289. }
  290. if ((modifiers & InputEvent.BUTTON1_DOWN_MASK) != 0) {
  291. buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
  292. buf.append("+");
  293. }
  294. if ((modifiers & InputEvent.BUTTON2_DOWN_MASK) != 0) {
  295. buf.append(Toolkit.getProperty("AWT.button2", "Button2"));
  296. buf.append("+");
  297. }
  298. if ((modifiers & InputEvent.BUTTON3_DOWN_MASK) != 0) {
  299. buf.append(Toolkit.getProperty("AWT.button3", "Button3"));
  300. buf.append("+");
  301. }
  302. if (buf.length() > 0) {
  303. buf.setLength(buf.length()-1); // remove trailing '+'
  304. }
  305. return buf.toString();
  306. }
  307. }