// KeyEvent.java
// By Ned Etcode
// Copyright 1996 Netscape Communications Corp.  All rights reserved.

package netscape.application;

import netscape.util.*;

import java.awt.Event;

/** Event subclass used for all key up and key down events.
  */

public class KeyEvent extends netscape.application.Event {
    /** The key that was pressed or released. */
    public int                  key;

    /** The modifier keys that the user held down when the KeyEvent was
      * generated.
      */
    public int                  modifiers;

    /** Mouse "down" event. */
    public final static int     KEY_DOWN = -11;
    /** Mouse "up" event. */
    public final static int     KEY_UP = -12;

    /** The Alternate key modifier bitmask. */
    public static final int     ALT_MASK = java.awt.Event.ALT_MASK;

    /** The Control key modifier bitmask. */
    public static final int     CONTROL_MASK = java.awt.Event.CTRL_MASK;

    /** The Shift key modifier bitmask. */
    public static final int     SHIFT_MASK = java.awt.Event.SHIFT_MASK;

    /** The Meta key modifier bitmask. */
    public static final int     META_MASK = java.awt.Event.META_MASK;

    final static int            RETURN_KEY = 10;

    /** Constructs a KeyEvent.
      */
    public KeyEvent() {
        super();
    }

     /** Constructs a KeyEvent to hold key press information for the
       * specified key.  <b>modifiers</b> is the bitmask representing the
       * modifier keys held down during the key press.  <b>down</b> specifies
       * whether the event represents a key up or key down event.
       */
    public KeyEvent(long timeStamp, int key, int modifiers, boolean down) {
        this();

        this.timeStamp = timeStamp;
        if (down) {
            type = KEY_DOWN;
        } else {
            type = KEY_UP;
        }
        this.key = key;
        this.modifiers = modifiers;
    }

    /** Returns <b>true</b> if the Shift key was held down during the key
      * event.
      */
    public boolean isShiftKeyDown() {
        return (modifiers & SHIFT_MASK) != 0;
    }

    /** Returns <b>true</b> if the Control key was held down during the key
      * event.
      */
    public boolean isControlKeyDown() {
        return (modifiers & CONTROL_MASK) != 0;
    }

    /** Returns <b>true</b> if the Meta key was held down during the key event.
      */
    public boolean isMetaKeyDown() {
        return (modifiers & META_MASK) != 0;
    }

    /** Returns <b>true</b> if the Alt key was held down during the key event.
      */
    public boolean isAltKeyDown() {
        return (modifiers & ALT_MASK) != 0;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Return key.
      */
    public boolean isReturnKey() {
        return key == RETURN_KEY;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Backspace key.
      */
    public boolean isBackspaceKey() {
        return key == 8;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Delete key.  In
      * general, the key used to delete characters is the "Backspace" key in
      * the upper-right corner of the keyboard (the Mac calls it "Delete").
      * @see #isBackspaceKey
      */
    public boolean isDeleteKey() {
        return key == 127;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Escape key.
      */
    public boolean isEscapeKey() {
        return key == 27;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Tab key.
      */
    public boolean isTabKey() {
        return (key == 9 && !isShiftKeyDown());
    }

    /** Returns <b>true</b> if the KeyEvent represents the BackTab
      * (Shift + Tab) key.
      */
    public boolean isBackTabKey() {
        return (key == 9 && isShiftKeyDown());
    }

    /** Returns <b>true</b> if the KeyEvent represents the Up Arrow key.
      */
    public boolean isUpArrowKey() {
        return key == java.awt.Event.UP;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Down Arrow key.
      */
    public boolean isDownArrowKey() {
        return key == java.awt.Event.DOWN;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Left Arrow key.
      */
    public boolean isLeftArrowKey() {
        return key == java.awt.Event.LEFT;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Right Arrow key.
      */
    public boolean isRightArrowKey() {
        return key == java.awt.Event.RIGHT;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Arrow key.
      */
    public boolean isArrowKey() {
        return (key == java.awt.Event.UP) || (key == java.awt.Event.DOWN) ||
                (key == java.awt.Event.LEFT) || (key == java.awt.Event.RIGHT);
    }

    /** Returns <b>true</b> if the KeyEvent represents the Home key.
      */
    public boolean isHomeKey() {
        return key == java.awt.Event.HOME;
    }

    /** Returns <b>true</b> if the KeyEvent represents the End key.
      */
    public boolean isEndKey() {
        return key == java.awt.Event.END;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Page Up key.
      */
    public boolean isPageUpKey() {
        return key == java.awt.Event.PGUP;
    }

    /** Returns <b>true</b> if the KeyEvent represents the Page Down key.
      */
    public boolean isPageDownKey() {
        return key == java.awt.Event.PGDN;
    }

    /** Returns the function key number or <b>0</b> if the KeyEvent does not
      * represent a function key.
     */
    public int isFunctionKey() {
        if (key == java.awt.Event.F1) {
            return 1;
        } else if (key == java.awt.Event.F2) {
            return 2;
        } else if (key == java.awt.Event.F3) {
            return 3;
        } else if (key == java.awt.Event.F4) {
            return 4;
        } else if (key == java.awt.Event.F5) {
            return 5;
        } else if (key == java.awt.Event.F6) {
            return 6;
        } else if (key == java.awt.Event.F7) {
            return 7;
        } else if (key == java.awt.Event.F8) {
            return 8;
        } else if (key == java.awt.Event.F9) {
            return 9;
        } else if (key == java.awt.Event.F10) {
            return 10;
        } else if (key == java.awt.Event.F11) {
            return 11;
        } else if (key == java.awt.Event.F12) {
            return 12;
        }

        return 0;
    }

    /** Returns <b>true</b> if the KeyEvent represents a printable ASCII
      * character.
      */
    public boolean isPrintableKey() {
        return !((key < ' ') || isArrowKey() || isHomeKey() ||
                 isEndKey() || (isFunctionKey() != 0));
    }

    /** Sets the RootView associated with the KeyEvent. */
    public void setRootView(RootView rootView) {
        processor = rootView;
    }

    /** Returns the RootView associated with the KeyEvent.
      * @see #setRootView
      */
    public RootView rootView() {
        return (RootView)processor;
    }

    /** Returns the KeyEvent's String representation.
      */
    public String toString() {
        String          typeString;

        if (type == KEY_DOWN) {
            typeString = "KeyDown";
        } else {
            typeString = "KeyUp";
        }

        if (key < ' ') {
            return typeString + ":\'\' (0x" +
                   Integer.toString(key, 16) + ")\':" + modifiers;
        }

        return typeString + ":\'" + (char)key + "\' (0x" +
               Integer.toString(key, 16) + ")\':" + modifiers;
    }
}
