FileDocCategorySizeDatePackage
EventTester.javaAPI DocExample3556Sat Jun 02 03:10:06 BST 2001None

EventTester.java

// This example is from the book _Java in a Nutshell_ by David Flanagan.
// Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.

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

public class EventTester extends Applet {
    // display the instructions
    public void paint(Graphics g)  { 
        g.drawString("Click, drag, and type in this window.", 10, 20);
    }
    
    // Handle mouse events
    public boolean mouseDown(Event e, int x, int y)  {
        showStatus(modifier_key_names(e.modifiers) + 
               "Mouse Down: [" + x + "," + y + "]");
        return true;
    }
    public boolean mouseUp(Event e, int x, int y)  {
        showStatus(modifier_key_names(e.modifiers) + 
               "Mouse Up: [" + x + "," + y + "]");
        return true;
    }
    public boolean mouseDrag(Event e, int x, int y)  {
        showStatus(modifier_key_names(e.modifiers) + 
               "Mouse Drag: [" + x + "," + y + "]");
        return true;
    }
    public boolean mouseEnter(Event e, int x, int y)  {
        showStatus("Mouse Enter: [" + x + "," + y + "]");
        return true;
    }
    public boolean mouseExit(Event e, int x, int y)  {
        showStatus("Mouse Exit: [" + x + "," + y + "]");
        return true;
    }
    
    // Handle focus events
    public boolean gotFocus(Event e, Object what)  {
        showStatus("Got Focus");
        return true;
    }
    public boolean lostFocus(Event e, Object what)  {
        showStatus("Lost Focus");
        return true;
    }
    
    // Handle key down and key up events
    public boolean keyDown(Event e, int key)  {
        int flags = e.modifiers;
        if (e.id == Event.KEY_PRESS)            // a regular key
            showStatus("Key Down: " 
                   + modifier_key_names(flags) 
                   + key_name(e));
        else if (e.id == Event.KEY_ACTION)      // a function key
            showStatus("Function Key Down: "
                   + modifier_key_names(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
            showStatus("Key Up: " 
                   + modifier_key_names(flags)
                   + key_name(e));
        else if (e.id == Event.KEY_ACTION_RELEASE) // a function key
            showStatus("Function Key Up: "
                   + modifier_key_names(flags)
                   + function_key_name(key));
        return true;
    }
    
    // The remaining methods help us sort out the various key events

    // Return the current list of modifier keys
    private String modifier_key_names(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 (ASCII) key.
    private String key_name(Event e) {
        char c = (char) e.key;
        
        // If CTRL flag is set, handle ASCII control characters.
        if (e.controlDown()) {
            if (c