Abstract Class InputEvent in Java

This abstract class serves as the superclass for the raw user input event types MouseEvent and KeyEvent.

We need to use the inherited getComponent() to determine in which component the event occurred. We need to use getWhen() to obtain a timestamp for the event. We need to usegetModifiers() to determine which keyboard modifier keys or mouse buttons were down when the event occurred.

We can decode the getModifiers() return value using the various _MASK constants defined by this class. The class also defines four convenience methods for determining the state of keyboard modifiers. In Java, input events are delivered to the appropriate listener objects before they are delivered to the AWT components themselves.

If a listener calls the consume() method of the event, the event is not passed on to the component. For example, if a listener registered on a Button “consumes” a mouse click, it prevents the button itself from responding to that event. We need to use isConsumed() to test whether some other listener object has already consumed the event.

InputEvent is the root class for representing user input events. Input events are passed to listeners before the event source processes them. If one of the listeners consumes an event by using consume(), the event will not be processed by the event source peer.

The class structure for the abstract class InputEvent is given as

public abstract class java.awt.event.InputEvent extends java.awt.event.ComponentEvent{
// No Constructor
// Constants
public static final int ALT_MASK;
public static final int BUTTON1_MASK;
public static final int BUTTON2_MASK;
public static final int BUTTON3_MASK;
public static final int CTRL_MASK;
public static final int META_MASK;
public static final int SHIFT_MASK;
// Public Instance Methods
public void consume(); // Overrides AWTEvent
public int getModifiers();
public long getWhen();
public boolean isAltDown();
public boolean isConsumed(); // Overrides AWTEvent
public boolean isControlDown();
public boolean isMetaDown();
public boolean isShiftDown();
}

The details of the class structure are given as follows:

public final static int ALT_MASK;

public final static int ALT_MASK is a constant that represents the ALT key mask. ORed with other masks to form modifiers setting of event.

public final static int BUTTON1_MASK;

public final static int BUTTON1_MASK is a constant that represents the mouse button 1 key mask. ORed with other masks to form modifiers setting of event.

public final static int BUTTON2_MASK;

public final static int BUTTON2_MASK is a constant that represents the mouse button 2 key mask. ORed with other masks to form modifiers setting of event. This constant is identical to ALT_MASK.

public final static int BUTTON3_MASK;

public final static int BUTTON3_MASK is a constant that represents the mouse button 3 key mask. ORed with other masks to form modifiers setting of event. This constant is identical to ALT_MASK.

public final static int CTRL_MASK;

public final static int CTRL_MASK is a constant that represents the Control key mask. ORed with other masks to form modifiers setting of event.

public final static int META_MASK;

public final static int META_MASK is a constant that represents the Meta key mask. ORed with other masks to form modifiers setting of event.

public final static int SHIFT_MASK;

public final static int SHIFT_MASK is a constant that represents the Shift key mask. ORed with other masks to form modifiers setting of event.

public void consume();

public void consume() method consumes an event and that will not be delivered to its source for default processing.

public int getModifiers();

public int getModifiers() method is used to find out what modifier keys were pressed when an input event occurred.

This method returns the modifier flags, a combination of the _MASK constants.

public long getWhen();

public long getWhen() method returns the time of the event as the number of milliseconds since the epoch (00:00:00 UTC, January 1, 1970). Conveniently, java.util.Date has a constructor that accepts such values.

This method returns the time at which this event occurred.

public boolean isAltDown();

public boolean isAltDown() method returns true if the Alt key was pressed; false otherwise.

This method returns true if the Alt key was pressed; false otherwise.

public boolean isConsumed();

public boolean isConsumed() method returns true if the event has been consumed; false otherwise.

This method returns true if the event has been consumed; false otherwise.

public boolean isControlDown();

public boolean isControlDown() method returns true if the Control key was pressed; false otherwise.

This method returns true if the Control key was pressed; false otherwise.

public boolean isMetaDown();

public boolean isMetaDown() method returns true if the Meta key was pressed; false otherwise.

This method returns true if the Meta key was pressed; false otherwise.

public boolean isShiftDown();

public boolean isShiftDown() method returns true if the Shift key was pressed; false otherwise.

This method returns true if the Shift key was pressed; false otherwise.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

Write A Comment