1. /*
  2. * @(#)InputEvent.java 1.34 04/06/02
  3. *
  4. * Copyright 2004 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.34 06/02/04
  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. * Note that BUTTON2_MASK has the same value as ALT_MASK.
  68. */
  69. public static final int BUTTON2_MASK = Event.ALT_MASK;
  70. /**
  71. * The Mouse Button3 modifier constant.
  72. * It is recommended that BUTTON3_DOWN_MASK be used instead.
  73. * Note that BUTTON3_MASK has the same value as META_MASK.
  74. */
  75. public static final int BUTTON3_MASK = Event.META_MASK;
  76. /**
  77. * The Shift key extended modifier constant.
  78. * @since 1.4
  79. */
  80. public static final int SHIFT_DOWN_MASK = 1 << 6;
  81. /**
  82. * The Control key extended modifier constant.
  83. * @since 1.4
  84. */
  85. public static final int CTRL_DOWN_MASK = 1 << 7;
  86. /**
  87. * The Meta key extended modifier constant.
  88. * @since 1.4
  89. */
  90. public static final int META_DOWN_MASK = 1 << 8;
  91. /**
  92. * The Alt key extended modifier constant.
  93. * @since 1.4
  94. */
  95. public static final int ALT_DOWN_MASK = 1 << 9;
  96. /**
  97. * The Mouse Button1 extended modifier constant.
  98. * @since 1.4
  99. */
  100. public static final int BUTTON1_DOWN_MASK = 1 << 10;
  101. /**
  102. * The Mouse Button2 extended modifier constant.
  103. * @since 1.4
  104. */
  105. public static final int BUTTON2_DOWN_MASK = 1 << 11;
  106. /**
  107. * The Mouse Button3 extended modifier constant.
  108. * @since 1.4
  109. */
  110. public static final int BUTTON3_DOWN_MASK = 1 << 12;
  111. /**
  112. * The AltGraph key extended modifier constant.
  113. * @since 1.4
  114. */
  115. public static final int ALT_GRAPH_DOWN_MASK = 1 << 13;
  116. static final int JDK_1_3_MODIFIERS = SHIFT_DOWN_MASK - 1;
  117. /**
  118. * The input event's Time stamp in UTC format. The time stamp
  119. * indicates when the input event was created.
  120. *
  121. * @serial
  122. * @see #getWhen()
  123. */
  124. long when;
  125. /**
  126. * The state of the modifier mask at the time the input
  127. * event was fired.
  128. *
  129. * @serial
  130. * @see #getModifiers()
  131. * @see #getModifiersEx()
  132. * @see java.awt.event.KeyEvent
  133. * @see java.awt.event.MouseEvent
  134. */
  135. int modifiers;
  136. /*
  137. * A flag that indicates that this instance can be used to access
  138. * the system clipboard.
  139. */
  140. private transient boolean canAccessSystemClipboard;
  141. static {
  142. /* ensure that the necessary native libraries are loaded */
  143. NativeLibLoader.loadLibraries();
  144. if (!GraphicsEnvironment.isHeadless()) {
  145. initIDs();
  146. }
  147. }
  148. /**
  149. * Initialize JNI field and method IDs for fields that may be
  150. accessed from C.
  151. */
  152. private static native void initIDs();
  153. /**
  154. * Constructs an InputEvent object with the specified source component,
  155. * modifiers, and type.
  156. * <p>Note that passing in an invalid <code>id</code> results in
  157. * unspecified behavior. This method throws an
  158. * <code>IllegalArgumentException</code> if <code>source</code>
  159. * is <code>null</code>.
  160. *
  161. * @param source the object where the event originated
  162. * @param id the event type
  163. * @param when the time the event occurred
  164. * @param modifiers represents the modifier keys and mouse buttons down
  165. * while the event occurred
  166. * @throws IllegalArgumentException if <code>source</code> is null
  167. */
  168. InputEvent(Component source, int id, long when, int modifiers) {
  169. super(source, id);
  170. this.when = when;
  171. this.modifiers = modifiers;
  172. canAccessSystemClipboard = canAccessSystemClipboard();
  173. }
  174. private boolean canAccessSystemClipboard() {
  175. boolean b = false;
  176. if (!GraphicsEnvironment.isHeadless()) {
  177. SecurityManager sm = System.getSecurityManager();
  178. if (sm != null) {
  179. try {
  180. sm.checkSystemClipboardAccess();
  181. b = true;
  182. } catch (SecurityException se) {
  183. }
  184. } else {
  185. b = true;
  186. }
  187. }
  188. return b;
  189. }
  190. /**
  191. * Returns whether or not the Shift modifier is down on this event.
  192. */
  193. public boolean isShiftDown() {
  194. return (modifiers & SHIFT_MASK) != 0;
  195. }
  196. /**
  197. * Returns whether or not the Control modifier is down on this event.
  198. */
  199. public boolean isControlDown() {
  200. return (modifiers & CTRL_MASK) != 0;
  201. }
  202. /**
  203. * Returns whether or not the Meta modifier is down on this event.
  204. */
  205. public boolean isMetaDown() {
  206. return (modifiers & META_MASK) != 0;
  207. }
  208. /**
  209. * Returns whether or not the Alt modifier is down on this event.
  210. */
  211. public boolean isAltDown() {
  212. return (modifiers & ALT_MASK) != 0;
  213. }
  214. /**
  215. * Returns whether or not the AltGraph modifier is down on this event.
  216. */
  217. public boolean isAltGraphDown() {
  218. return (modifiers & ALT_GRAPH_MASK) != 0;
  219. }
  220. /**
  221. * Returns the timestamp of when this event occurred.
  222. */
  223. public long getWhen() {
  224. return when;
  225. }
  226. /**
  227. * Returns the modifier mask for this event.
  228. */
  229. public int getModifiers() {
  230. return modifiers & JDK_1_3_MODIFIERS;
  231. }
  232. /**
  233. * Returns the extended modifier mask for this event.
  234. * Extended modifiers represent the state of all modal keys,
  235. * such as ALT, CTRL, META, and the mouse buttons just after
  236. * the event occurred
  237. * <P>
  238. * For example, if the user presses <b>button 1</b> followed by
  239. * <b>button 2</b>, and then releases them in the same order,
  240. * the following sequence of events is generated:
  241. * <PRE>
  242. * <code>MOUSE_PRESSED</code>: <code>BUTTON1_DOWN_MASK</code>
  243. * <code>MOUSE_PRESSED</code>: <code>BUTTON1_DOWN_MASK | BUTTON2_DOWN_MASK</code>
  244. * <code>MOUSE_RELEASED</code>: <code>BUTTON2_DOWN_MASK</code>
  245. * <code>MOUSE_CLICKED</code>: <code>BUTTON2_DOWN_MASK</code>
  246. * <code>MOUSE_RELEASED</code>:
  247. * <code>MOUSE_CLICKED</code>:
  248. * </PRE>
  249. * <P>
  250. * It is not recommended to compare the return value of this method
  251. * using <code>==</code> because new modifiers can be added in the future.
  252. * For example, the appropriate way to check that SHIFT and BUTTON1 are
  253. * down, but CTRL is up is demonstrated by the following code:
  254. * <PRE>
  255. * int onmask = SHIFT_DOWN_MASK | BUTTON1_DOWN_MASK;
  256. * int offmask = CTRL_DOWN_MASK;
  257. * if (event.getModifiersEx() & (onmask | offmask) == onmask) {
  258. * ...
  259. * }
  260. * </PRE>
  261. * The above code will work even if new modifiers are added.
  262. *
  263. * @since 1.4
  264. */
  265. public int getModifiersEx() {
  266. return modifiers & ~JDK_1_3_MODIFIERS;
  267. }
  268. /**
  269. * Consumes this event so that it will not be processed
  270. * in the default manner by the source which originated it.
  271. */
  272. public void consume() {
  273. consumed = true;
  274. }
  275. /**
  276. * Returns whether or not this event has been consumed.
  277. * @see #consume
  278. */
  279. public boolean isConsumed() {
  280. return consumed;
  281. }
  282. // state serialization compatibility with JDK 1.1
  283. static final long serialVersionUID = -2482525981698309786L;
  284. /**
  285. * Returns a String describing the extended modifier keys and
  286. * mouse buttons, such as "Shift", "Button1", or "Ctrl+Shift".
  287. * These strings can be localized by changing the
  288. * awt.properties file.
  289. *
  290. * @param modifiers a modifier mask describing the extended
  291. * modifier keys and mouse buttons for the event
  292. * @return a text description of the combination of extended
  293. * modifier keys and mouse buttons that were held down
  294. * during the event.
  295. * @since 1.4
  296. */
  297. public static String getModifiersExText(int modifiers) {
  298. StringBuffer buf = new StringBuffer();
  299. if ((modifiers & InputEvent.META_DOWN_MASK) != 0) {
  300. buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
  301. buf.append("+");
  302. }
  303. if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
  304. buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
  305. buf.append("+");
  306. }
  307. if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) {
  308. buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
  309. buf.append("+");
  310. }
  311. if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
  312. buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
  313. buf.append("+");
  314. }
  315. if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
  316. buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
  317. buf.append("+");
  318. }
  319. if ((modifiers & InputEvent.BUTTON1_DOWN_MASK) != 0) {
  320. buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
  321. buf.append("+");
  322. }
  323. if ((modifiers & InputEvent.BUTTON2_DOWN_MASK) != 0) {
  324. buf.append(Toolkit.getProperty("AWT.button2", "Button2"));
  325. buf.append("+");
  326. }
  327. if ((modifiers & InputEvent.BUTTON3_DOWN_MASK) != 0) {
  328. buf.append(Toolkit.getProperty("AWT.button3", "Button3"));
  329. buf.append("+");
  330. }
  331. if (buf.length() > 0) {
  332. buf.setLength(buf.length()-1); // remove trailing '+'
  333. }
  334. return buf.toString();
  335. }
  336. }