1. /*
  2. * @(#)InputEvent.java 1.20 01/11/29
  3. *
  4. * Copyright 2002 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. /**
  11. * The root event class for all component-level input events.
  12. *
  13. * Input events are delivered to listeners before they are
  14. * processed normally by the source where they originated.
  15. * This allows listeners and component subclasses to "consume"
  16. * the event so that the source will not process them in their
  17. * default manner. For example, consuming mousePressed events
  18. * on a Button component will prevent the Button from being
  19. * activated.
  20. *
  21. * @see KeyEvent
  22. * @see KeyAdapter
  23. * @see MouseEvent
  24. * @see MouseAdapter
  25. * @see MouseMotionAdapter
  26. *
  27. * @version 1.20 11/29/01
  28. * @author Carl Quinn
  29. */
  30. public abstract class InputEvent extends ComponentEvent {
  31. /**
  32. * The shift key modifier constant.
  33. */
  34. public static final int SHIFT_MASK = Event.SHIFT_MASK;
  35. /**
  36. * The control key modifier constant.
  37. */
  38. public static final int CTRL_MASK = Event.CTRL_MASK;
  39. /**
  40. * The meta key modifier constant.
  41. */
  42. public static final int META_MASK = Event.META_MASK;
  43. /**
  44. * The alt key modifier constant.
  45. */
  46. public static final int ALT_MASK = Event.ALT_MASK;
  47. /**
  48. * The alt-graph key modifier constant.
  49. */
  50. public static final int ALT_GRAPH_MASK = 1 << 5;
  51. /**
  52. * The mouse button1 modifier constant.
  53. */
  54. public static final int BUTTON1_MASK = 1 << 4;
  55. /**
  56. * The mouse button2 modifier constant.
  57. */
  58. public static final int BUTTON2_MASK = Event.ALT_MASK;
  59. /**
  60. * The mouse button3 modifier constant.
  61. */
  62. public static final int BUTTON3_MASK = Event.META_MASK;
  63. /**
  64. * The input events Time stamp. The time stamp is in
  65. * UTC format that indicates when the input event was
  66. * created.
  67. *
  68. * @serial
  69. * @see getWhen()
  70. */
  71. long when;
  72. /**
  73. * The state of the modifier key at the time the input
  74. * event was fired.
  75. *
  76. * @serial
  77. * @see getModifiers()
  78. * @see java.awt.event.MouseEvent
  79. */
  80. int modifiers;
  81. static {
  82. /* ensure that the necessary native libraries are loaded */
  83. NativeLibLoader.loadLibraries();
  84. initIDs();
  85. }
  86. /**
  87. * Initialize JNI field and method IDs for fields that may be
  88. accessed from C.
  89. */
  90. private static native void initIDs();
  91. /**
  92. * Constructs an InputEvent object with the specified source component,
  93. * modifiers, and type.
  94. * @param source the object where the event originated
  95. * @id the event type
  96. * @when the time the event occurred
  97. * @modifiers the modifier keys down while event occurred
  98. */
  99. InputEvent(Component source, int id, long when, int modifiers) {
  100. super(source, id);
  101. this.when = when;
  102. this.modifiers = modifiers;
  103. }
  104. /**
  105. * Returns whether or not the Shift modifier is down on this event.
  106. */
  107. public boolean isShiftDown() {
  108. return (modifiers & Event.SHIFT_MASK) != 0;
  109. }
  110. /**
  111. * Returns whether or not the Control modifier is down on this event.
  112. */
  113. public boolean isControlDown() {
  114. return (modifiers & Event.CTRL_MASK) != 0;
  115. }
  116. /**
  117. * Returns whether or not the Meta modifier is down on this event.
  118. */
  119. public boolean isMetaDown() {
  120. return (modifiers & Event.META_MASK) != 0;
  121. }
  122. /**
  123. * Returns whether or not the Alt modifier is down on this event.
  124. */
  125. public boolean isAltDown() {
  126. return (modifiers & Event.ALT_MASK) != 0;
  127. }
  128. /**
  129. * Returns whether or not the Alt-Graph modifier is down on this event.
  130. */
  131. public boolean isAltGraphDown() {
  132. return (modifiers & InputEvent.ALT_GRAPH_MASK) != 0;
  133. }
  134. /**
  135. * Returns the timestamp of when this event occurred.
  136. */
  137. public long getWhen() {
  138. return when;
  139. }
  140. /**
  141. * Returns the modifiers flag for this event.
  142. */
  143. public int getModifiers() {
  144. return modifiers;
  145. }
  146. /**
  147. * Consumes this event so that it will not be processed
  148. * in the default manner by the source which originated it.
  149. */
  150. public void consume() {
  151. consumed = true;
  152. }
  153. /**
  154. * Returns whether or not this event has been consumed.
  155. * @see #consume
  156. */
  157. public boolean isConsumed() {
  158. return consumed;
  159. }
  160. // state serialization compatibility with JDK 1.1
  161. static final long serialVersionUID = -2482525981698309786L;
  162. }