Listening to All AWT Events

Listening to All AWT Events

For some programs, it is helpful to create a listener to keep track of all the events being dispatched by the UI. This can be very useful in debugging programs. This is a way to listen to all such events in the Java environment.
Bascially, you implement AWTEventListener, which listens for all AWT events. To manipulate the object dispatching the events, just use the event method getSource () to retrieve the object. This code demonstrates how to listen to all mouse right-clicks:

  public class MySystemAWTEventListener implements AWTEventListener{   public MySystemAWTEventListener()    {    }    /**     * Invoked when an event is dispatched in the AWT.     */    public void eventDispatched(AWTEvent evt)    {      MouseEvent me = (MouseEvent)evt;      currentObj  = me.getSource ();      if(SwingUtilities.isRightMouseButton(me))        {          //make something with the currentObj        }    }


Share the Post: