FileDocCategorySizeDatePackage
EventTester1.javaAPI DocExample3162Sat Jun 02 02:40:16 BST 2001None

EventTester1.java

// This example is from _Java Examples in a Nutshell_. (http://www.oreilly.com)
// Copyright (c) 1997 by David Flanagan
// This example is provided WITHOUT ANY WARRANTY either expressed or implied.
// You may study, use, modify, and distribute it for non-commercial purposes.
// For any commercial use, see http://www.davidflanagan.com/javaexamples

import java.applet.*;
import java.awt.*;
import java.util.*;

/** An applet that gives details about Java 1.0 events */
public class EventTester1 extends Applet {
  // Handle mouse events
  public boolean mouseDown(Event e, int x, int y)  {
    showLine(mods(e.modifiers) +  "Mouse Down: [" + x + "," + y + "]");
    return true;
  }
  public boolean mouseUp(Event e, int x, int y)  {
    showLine(mods(e.modifiers) + "Mouse Up: [" + x + "," + y + "]");
    return true;
  }
  public boolean mouseDrag(Event e, int x, int y)  {
    showLine(mods(e.modifiers) + "Mouse Drag: [" + x + "," + y + "]");
    return true;
  }
  public boolean mouseMove(Event e, int x, int y) {
    showLine(mods(e.modifiers) + "Mouse Move: [" + x + "," + y + "]");
    return true;
  }
  public boolean mouseEnter(Event e, int x, int y)  {
    showLine("Mouse Enter: [" + x + "," + y + "]"); return true;
  }
  public boolean mouseExit(Event e, int x, int y)  {
    showLine("Mouse Exit: [" + x + "," + y + "]"); return true;
  }

  // Handle focus events
  public boolean gotFocus(Event e, Object what)  {
    showLine("Got Focus"); return true;
  }
  public boolean lostFocus(Event e, Object what)  {
    showLine("Lost Focus"); return true;
  }

  // Handle key down and key up events
  // This gets more confusing because there are two types of key events
  public boolean keyDown(Event e, int key)  {
    int flags = e.modifiers;
    if (e.id == Event.KEY_PRESS)                 // a regular key
      showLine("Key Down: " + mods(flags) + key_name(e));
    else if (e.id == Event.KEY_ACTION)           // a function key
      showLine("Function Key Down: " + mods(flags) + function_key_name(key));
    return true;
  }
  public boolean keyUp(Event e, int key)  {
    int flags = e.modifiers;
    if (e.id == Event.KEY_RELEASE)               // a regular key
      showLine("Key Up: " + mods(flags) + key_name(e));
    else if (e.id == Event.KEY_ACTION_RELEASE)   // a function key
      showLine("Function Key Up: " + mods(flags) + function_key_name(key));
    return true;
  }

  // The remaining methods help us sort out the various modifiers and keys

  // Return the current list of modifier keys
  private String mods(int flags) {
    String s = "[ ";
    if (flags == 0) return "";
    if ((flags & Event.SHIFT_MASK) != 0) s += "Shift ";
    if ((flags & Event.CTRL_MASK) != 0) s += "Control ";
    if ((flags & Event.META_MASK) != 0) s += "Meta ";
    if ((flags & Event.ALT_MASK) != 0) s += "Alt ";
    s += "] ";
    return s;
  }

  // Return the name of a regular (non-function) key.
  private String key_name(Event e) {
    char c = (char) e.key;
    if (e.controlDown()) {    // If CTRL flag is set, handle control chars.
      if (c