import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/** DemoLowLevelEvents - demonstrate low-level events.<p>
*
* This program and the next, <code>DemoHighLevelEvents</code>, serve to
* distinguish low-level events from high-level events.<p>
*
* The GUI window presents a single button labeled "Beep".  A beep is sounded
* whenever the button is "pressed", either by clicking on it with
* the mouse pointer, or by entering ALT_B on the keyboard.<p>
*
* The "B" in
* the button's label is underlined, as common in GUIs to indicate
* that the button may be activated by pressing the indicated letter
* on the keyboard in
* conjunction with the ALT modifier key.  This is known as a
* <i>mnemonic</i>.  The following statement enables this feature:<p>
*
* <pre>
*     beep.setMnemonic(KeyEvent.VK_B);
* </pre>
*
* Since we want the button to listen for both key events and mouse events,
* two listeners are added:<p>
*
* <pre>
*     beep.addKeyListener(this);
*     beep.addMouseListener(this);
* </pre>
*
* The beep is sounded in two places:
* in the <code>keyReleased</code> method (if the
* ALT_B keystroke is detected), and in the <code>mouseReleased</code>
* method (when the mouse button is released after clicking on the
* button).
* As you might imagine, this approach is rather inefficient.<p>
*
* <b>Levels of Events</b><p>
*
* In Java, events can be divided into two categories: <i>low-level events</i>
* and <i>high-level events</i>.  High-level events are more
* commonly called <i>semantic</i> events.
* Low-level events represent window or system-level occurrences at a very
* low level, usually as the direct result of user input.  All other
* events are semantic events.
* <p>
*
* <b>Low-level Events</b>. 
* Mouse and key events are low-level events. Other low-level
* events include component, container, focus, and window events.
* Component events are responses to changes in a
* component's position, size, or visibility.
* Container events occur when components are added to or
* removed from a container.
* Focus events occur when a component gains or loses the keyboard focus.
* Window events occur when the basic status
* of a window (such as a dialog box or a frame) changes.<p>
*
* <b>Semantic events</b>.  Semantic events are high-level events: they
* are closer to the intended <i>meaning</i> of an action than to
* the <i>physical trigger</i> for an action.
* Examples of semantic events include action events,
* item events, and list selection events. The trigger for a semantic
* event can differ by component. For example, a button customarily
* fires an action event when the user clicks it, but
* a text field fires an action event when the user presses
* ENTER.
* <p>
*
* Whenever possible, listen for semantic events
* rather than low-level events.  Among other things,
* the resulting code is more robust and portable.
* For example, listening for action events on buttons,
* rather than
* mouse events, means that the button will react
* appropriately for any action designed to activate the 
* button, such as a
* keyboard mnemonic or a look-and-feel-specific gesture.
* This is demonstrated in <code>DemoHighLevelEvents</code>.
* <p>
*
* Screen snap ...<p>
* <center><img src="DemoLowLevelEvents-1.gif"></center><p>
*
* @see <a href="DemoLowLevelEvents.java">source code</a>
* @author Scott MacKenzie, 2003
*/
public class DemoLowLevelEvents
{
   public static void main(String[] args)
   {
      DemoLowLevelEventsFrame frame = new DemoLowLevelEventsFrame();
      frame.setTitle("DemoLowLevelEvents");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.show();
   }
}

// -----------------------------
// define the application window
// -----------------------------

class DemoLowLevelEventsFrame extends JFrame
implements KeyListener, MouseListener
{
   // -----------------------------------------------------------------
   // declare variables and components accessed by more than one method
   // -----------------------------------------------------------------

   private JButton beep;

   // -----------
   // constructor
   // -----------

   public DemoLowLevelEventsFrame()
   {
      // -------------------------------
      // create and configure components
      // -------------------------------

      beep = new JButton("Beep");
      beep.setMnemonic(KeyEvent.VK_B);

      // -------------
      // add listeners
      // -------------

      beep.addKeyListener(this);
      beep.addMouseListener(this);

      // ------------------
      // arrange components
      // ------------------

      // put components in a panel

      JPanel panel = new JPanel();
      panel.add(beep);

      // make the panel this extended JFrame's content pane

      this.setContentPane(panel);
   }

   // --------------------------
   // implement listener methods
   // --------------------------

   // KeyListener methods

   public void keyTyped(KeyEvent ke) {}
   public void keyPressed(KeyEvent ke) {}
   public void keyReleased(KeyEvent ke)
   {
      final KeyStroke ALT_B =
         KeyStroke.getKeyStroke(KeyEvent.VK_B, KeyEvent.ALT_MASK);

      KeyStroke k =
         KeyStroke.getKeyStroke(ke.getKeyCode(), ke.getModifiers());

      if (k == ALT_B)
		{
         //Toolkit.getDefaultToolkit().beep();
			System.out.print("\07"); 
			System.out.flush();
		}
   }

   // MouseListener methods

   public void mouseClicked(MouseEvent me) {}
   public void mouseEntered(MouseEvent me) {}
   public void mouseExited(MouseEvent me) {}
   public void mousePressed(MouseEvent me) {}
   public void mouseReleased(MouseEvent me)
   {
      //Toolkit.getDefaultToolkit().beep();
		System.out.print("\07"); 
		System.out.flush();
   }
}

