Both KeyEvent
and MouseEvent
are
subclasses of java.awt.event.InputEvent
. The primary
feature InputEvent
adds is the ability to test for
additional conditions on the event such as whether the ALT key was
pressed at the same time as another key, or whether the option key
was held while the mouse was dragged. For example, when the shift
key is held down, many drawing programs constrain lines to be
vertical or horizontal, rectangles to be squares, ovals to be
circles, and so on.
The following four methods tell you whether or not the specified key was pressed when this event was sent.
public boolean isShiftDown()
public boolean isControlDown()
public boolean isMetaDown()
public boolean isAltDown()
All may be invoked on either MouseEvent
objects or
KeyEvent
objects.
There is also a getWhen()
method which returns the
time the event occurred. This is given as the number of
milliseconds since midnight, January 1, 1970, Greenwich Mean
Time.
public long getWhen()
The java.util.Date
and
java.util.Calendar
classes have methods you can use to
convert this to a date and time. However, most of the time you'll
only be concerned with differences in the time between one event
and another.