home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / KeyEvent.java < prev    next >
Text File  |  1998-01-23  |  20KB  |  480 lines

  1. /*
  2.  * @(#)KeyEvent.java    1.25 97/11/03
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.awt.event;
  24.  
  25. import java.awt.Event;
  26. import java.awt.Component;
  27. import java.awt.Toolkit;
  28.  
  29. /**
  30.  * The component-level keyboard event.
  31.  *
  32.  * @version 1.23 08/04/97
  33.  * @author Carl Quinn
  34.  * @author Amy Fowler
  35.  */
  36. public class KeyEvent extends InputEvent {
  37.  
  38.     /**
  39.      * Marks the first integer id for the range of key event ids.
  40.      */
  41.     public static final int KEY_FIRST = 400;
  42.  
  43.     /**
  44.      * Marks the last integer id for the range of key event ids.
  45.      */
  46.     public static final int KEY_LAST  = 402;
  47.  
  48.     /**
  49.      * The key typed event type.  This type is generated by a combination
  50.      * of a key press followed by a key release.
  51.      */
  52.     public static final int KEY_TYPED = KEY_FIRST;
  53.  
  54.     /**
  55.      * The key pressed event type.
  56.      */
  57.     public static final int KEY_PRESSED = 1 + KEY_FIRST; //Event.KEY_PRESS
  58.  
  59.     /**
  60.      * The key released event type.
  61.      */
  62.     public static final int KEY_RELEASED = 2 + KEY_FIRST; //Event.KEY_RELEASE
  63.  
  64.     /**
  65.      * Virtual key codes.  These codes report which keyboard key has
  66.      * been pressed, rather than any character generated by one or more
  67.      * keys being pressed.  
  68.      *
  69.      * For example, pressing the Shift key will cause a KEY_PRESSED event 
  70.      * with a VK_SHIFT keyCode, while pressing the 'a' key will result in 
  71.      * a VK_A keyCode.  After the 'a' key is released, a KEY_RELEASED event 
  72.      * will be fired with VK_A, followed by a KEY_TYPED event with a keyChar 
  73.      * value of 'A'.  Key combinations which do not result in characters,
  74.      * such as action keys like F1, will not generate KEY_TYPED events.
  75.      *
  76.      * Note: not all keyboards or systems are capable of generating all
  77.      * virtual key codes.  No attempt is made in Java to artificially
  78.      * generate these keys.
  79.      *
  80.      * WARNING:  aside from those keys where are defined by the Java language
  81.      * (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of these
  82.      * constants.  Sun reserves the right to change these values as needed
  83.      * to accomodate a wider range of keyboards in the future.  
  84.      */
  85.     public static final int VK_ENTER          = '\n';
  86.     public static final int VK_BACK_SPACE     = '\b';
  87.     public static final int VK_TAB            = '\t';
  88.     public static final int VK_CANCEL         = 0x03;
  89.     public static final int VK_CLEAR          = 0x0C;
  90.     public static final int VK_SHIFT          = 0x10;
  91.     public static final int VK_CONTROL        = 0x11;
  92.     public static final int VK_ALT            = 0x12;
  93.     public static final int VK_PAUSE          = 0x13;
  94.     public static final int VK_CAPS_LOCK      = 0x14;
  95.     public static final int VK_ESCAPE         = 0x1B;
  96.     public static final int VK_SPACE          = 0x20;
  97.     public static final int VK_PAGE_UP        = 0x21;
  98.     public static final int VK_PAGE_DOWN      = 0x22;
  99.     public static final int VK_END            = 0x23;
  100.     public static final int VK_HOME           = 0x24;
  101.     public static final int VK_LEFT           = 0x25;
  102.     public static final int VK_UP             = 0x26;
  103.     public static final int VK_RIGHT          = 0x27;
  104.     public static final int VK_DOWN           = 0x28;
  105.     public static final int VK_COMMA          = 0x2C;
  106.     public static final int VK_PERIOD         = 0x2E;
  107.     public static final int VK_SLASH          = 0x2F;
  108.  
  109.     /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
  110.     public static final int VK_0              = 0x30;
  111.     public static final int VK_1              = 0x31;
  112.     public static final int VK_2              = 0x32;
  113.     public static final int VK_3              = 0x33;
  114.     public static final int VK_4              = 0x34;
  115.     public static final int VK_5              = 0x35;
  116.     public static final int VK_6              = 0x36;
  117.     public static final int VK_7              = 0x37;
  118.     public static final int VK_8              = 0x38;
  119.     public static final int VK_9              = 0x39;
  120.  
  121.     public static final int VK_SEMICOLON      = 0x3B;
  122.     public static final int VK_EQUALS         = 0x3D;
  123.  
  124.     /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
  125.     public static final int VK_A              = 0x41;
  126.     public static final int VK_B              = 0x42;
  127.     public static final int VK_C              = 0x43;
  128.     public static final int VK_D              = 0x44;
  129.     public static final int VK_E              = 0x45;
  130.     public static final int VK_F              = 0x46;
  131.     public static final int VK_G              = 0x47;
  132.     public static final int VK_H              = 0x48;
  133.     public static final int VK_I              = 0x49;
  134.     public static final int VK_J              = 0x4A;
  135.     public static final int VK_K              = 0x4B;
  136.     public static final int VK_L              = 0x4C;
  137.     public static final int VK_M              = 0x4D;
  138.     public static final int VK_N              = 0x4E;
  139.     public static final int VK_O              = 0x4F;
  140.     public static final int VK_P              = 0x50;
  141.     public static final int VK_Q              = 0x51;
  142.     public static final int VK_R              = 0x52;
  143.     public static final int VK_S              = 0x53;
  144.     public static final int VK_T              = 0x54;
  145.     public static final int VK_U              = 0x55;
  146.     public static final int VK_V              = 0x56;
  147.     public static final int VK_W              = 0x57;
  148.     public static final int VK_X              = 0x58;
  149.     public static final int VK_Y              = 0x59;
  150.     public static final int VK_Z              = 0x5A;
  151.  
  152.     public static final int VK_OPEN_BRACKET   = 0x5B;
  153.     public static final int VK_BACK_SLASH     = 0x5C;
  154.     public static final int VK_CLOSE_BRACKET  = 0x5D;
  155.  
  156.     public static final int VK_NUMPAD0        = 0x60;
  157.     public static final int VK_NUMPAD1        = 0x61;
  158.     public static final int VK_NUMPAD2        = 0x62;
  159.     public static final int VK_NUMPAD3        = 0x63;
  160.     public static final int VK_NUMPAD4        = 0x64;
  161.     public static final int VK_NUMPAD5        = 0x65;
  162.     public static final int VK_NUMPAD6        = 0x66;
  163.     public static final int VK_NUMPAD7        = 0x67;
  164.     public static final int VK_NUMPAD8        = 0x68;
  165.     public static final int VK_NUMPAD9        = 0x69;
  166.     public static final int VK_MULTIPLY       = 0x6A;
  167.     public static final int VK_ADD            = 0x6B;
  168.     public static final int VK_SEPARATER      = 0x6C;
  169.     public static final int VK_SUBTRACT       = 0x6D;
  170.     public static final int VK_DECIMAL        = 0x6E;
  171.     public static final int VK_DIVIDE         = 0x6F;
  172.     public static final int VK_F1             = 0x70;
  173.     public static final int VK_F2             = 0x71;
  174.     public static final int VK_F3             = 0x72;
  175.     public static final int VK_F4             = 0x73;
  176.     public static final int VK_F5             = 0x74;
  177.     public static final int VK_F6             = 0x75;
  178.     public static final int VK_F7             = 0x76;
  179.     public static final int VK_F8             = 0x77;
  180.     public static final int VK_F9             = 0x78;
  181.     public static final int VK_F10            = 0x79;
  182.     public static final int VK_F11            = 0x7A;
  183.     public static final int VK_F12            = 0x7B;
  184.     public static final int VK_DELETE         = 0x7F; /* ASCII DEL */
  185.     public static final int VK_NUM_LOCK       = 0x90;
  186.     public static final int VK_SCROLL_LOCK    = 0x91;
  187.  
  188.     public static final int VK_PRINTSCREEN    = 0x9A;
  189.     public static final int VK_INSERT         = 0x9B;
  190.     public static final int VK_HELP           = 0x9C;
  191.     public static final int VK_META           = 0x9D;
  192.  
  193.     public static final int VK_BACK_QUOTE     = 0xC0;
  194.     public static final int VK_QUOTE          = 0xDE;
  195.  
  196.     /** for Asian Keyboard */
  197.     public static final int VK_FINAL          = 0x18;
  198.     public static final int VK_CONVERT        = 0x1C;
  199.     public static final int VK_NONCONVERT     = 0x1D;
  200.     public static final int VK_ACCEPT         = 0x1E;
  201.     public static final int VK_MODECHANGE     = 0x1F;
  202.     public static final int VK_KANA           = 0x15;
  203.     public static final int VK_KANJI          = 0x19;
  204.     
  205.     /**
  206.      * KEY_TYPED events do not have a defined keyCode.
  207.      */
  208.     public static final int VK_UNDEFINED      = 0x0;
  209.  
  210.     /**
  211.      * KEY_PRESSED and KEY_RELEASED events which do not map to a
  212.      * valid Unicode character do not have a defined keyChar.
  213.      */
  214.     public static final char CHAR_UNDEFINED   = 0x0;
  215.  
  216.     int  keyCode;
  217.     char keyChar;
  218.  
  219.     /*
  220.      * JDK 1.1 serialVersionUID 
  221.      */
  222.      private static final long serialVersionUID = -2352130953028126954L;
  223.  
  224.     /**
  225.      * Constructs a KeyEvent object with the specified source component,
  226.      * type, modifiers, and key.
  227.      * @param source the object where the event originated
  228.      * @id the event type
  229.      * @when the time the event occurred
  230.      * @modifiers the modifier keys down during event
  231.      * @keyCode the integer code representing the key of the event 
  232.      * @keyChar the Unicode character generated by this event, or NUL
  233.      */
  234.     public KeyEvent(Component source, int id, long when, int modifiers,
  235.                     int keyCode, char keyChar) {
  236.         super(source, id, when, modifiers);
  237.  
  238.         if (id == KEY_TYPED && keyChar == CHAR_UNDEFINED) {
  239.             throw new IllegalArgumentException("invalid keyChar");
  240.         }
  241.         if (id == KEY_TYPED && keyCode != VK_UNDEFINED) {
  242.             throw new IllegalArgumentException("invalid keyCode");
  243.         }
  244.  
  245.         this.keyCode = keyCode;
  246.         this.keyChar = keyChar;
  247.     }
  248.  
  249.     /*
  250.      * @deprecated, as of JDK1.1 - Do NOT USE; will be removed in 1.1.1.
  251.      */
  252.     public KeyEvent(Component source, int id, long when, int modifiers,
  253.                     int keyCode) {
  254.         this(source, id, when, modifiers, keyCode, (char)keyCode);
  255.     }
  256.  
  257.     /**
  258.      * Returns the integer key-code associated with the key in this event.
  259.      * For KEY_TYPED events, keyCode is VK_UNDEFINED.
  260.      */
  261.     public int getKeyCode() {
  262.         return keyCode;
  263.     }
  264.  
  265.     public void setKeyCode(int keyCode) {
  266.         this.keyCode = keyCode;
  267.     }
  268.  
  269.     public void setKeyChar(char keyChar) {
  270.         this.keyChar = keyChar;
  271.     }
  272.  
  273.     /**
  274.      * Change the modifiers for a KeyEvent.  
  275.      * <p>
  276.      * NOTE:  use of this method is not recommended, because many AWT
  277.      * implementations do not recognize modifier changes.  This is
  278.      * especially true for KEY_TYPED events where the shift modifier
  279.      * is changed.
  280.      * @deprecated, as of JDK1.1.4
  281.      */
  282.     public void setModifiers(int modifiers) {
  283.         this.modifiers = modifiers;
  284.     }
  285.  
  286.     /**
  287.      * Returns the character associated with the key in this event.
  288.      * If no valid Unicode character exists for this key event, keyChar
  289.      * is CHAR_UNDEFINED.
  290.      */
  291.     public char getKeyChar() {
  292.         return keyChar;
  293.     }
  294.  
  295.     /**
  296.      * Returns a String describing the keyCode, such as "HOME", "F1" or "A".
  297.      * These strings can be localized by changing the awt.properties file.
  298.      */
  299.     public static String getKeyText(int keyCode) {
  300.         if (keyCode >= VK_0 && keyCode <= VK_9 || 
  301.             keyCode >= VK_A && keyCode <= VK_Z) {
  302.             return String.valueOf((char)keyCode);
  303.         }
  304.  
  305.         // Check for other ASCII keyCodes.
  306.         int index = ",./;=[\\]".indexOf(keyCode);
  307.         if (index >= 0) {
  308.             return String.valueOf((char)keyCode);
  309.         }
  310.     
  311.         switch(keyCode) {
  312.           case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter");
  313.           case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace");
  314.           case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab");
  315.           case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel");
  316.           case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear");
  317.           case VK_SHIFT: return Toolkit.getProperty("AWT.shift", "Shift");
  318.           case VK_CONTROL: return Toolkit.getProperty("AWT.control", "Control");
  319.           case VK_ALT: return Toolkit.getProperty("AWT.alt", "Alt");
  320.           case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause");
  321.           case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock");
  322.           case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape");
  323.           case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space");
  324.           case VK_PAGE_UP: return Toolkit.getProperty("AWT.pgup", "Page Up");
  325.           case VK_PAGE_DOWN: return Toolkit.getProperty("AWT.pgdn", "Page Down");
  326.           case VK_END: return Toolkit.getProperty("AWT.end", "End");
  327.           case VK_HOME: return Toolkit.getProperty("AWT.home", "Home");
  328.           case VK_LEFT: return Toolkit.getProperty("AWT.left", "Left");
  329.           case VK_UP: return Toolkit.getProperty("AWT.up", "Up");
  330.           case VK_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
  331.           case VK_DOWN: return Toolkit.getProperty("AWT.down", "Down");
  332.  
  333.           case VK_MULTIPLY: return Toolkit.getProperty("AWT.multiply", "NumPad *");
  334.           case VK_ADD: return Toolkit.getProperty("AWT.add", "NumPad +");
  335.           case VK_SEPARATER: return Toolkit.getProperty("AWT.separater", "NumPad ,");
  336.           case VK_SUBTRACT: return Toolkit.getProperty("AWT.subtract", "NumPad -");
  337.           case VK_DECIMAL: return Toolkit.getProperty("AWT.decimal", "NumPad .");
  338.           case VK_DIVIDE: return Toolkit.getProperty("AWT.divide", "NumPad /");
  339.  
  340.           case VK_F1: return Toolkit.getProperty("AWT.f1", "F1");
  341.           case VK_F2: return Toolkit.getProperty("AWT.f2", "F2");
  342.           case VK_F3: return Toolkit.getProperty("AWT.f3", "F3");
  343.           case VK_F4: return Toolkit.getProperty("AWT.f4", "F4");
  344.           case VK_F5: return Toolkit.getProperty("AWT.f5", "F5");
  345.           case VK_F6: return Toolkit.getProperty("AWT.f6", "F6");
  346.           case VK_F7: return Toolkit.getProperty("AWT.f7", "F7");
  347.           case VK_F8: return Toolkit.getProperty("AWT.f8", "F8");
  348.           case VK_F9: return Toolkit.getProperty("AWT.f9", "F9");
  349.           case VK_F10: return Toolkit.getProperty("AWT.f10", "F10");
  350.           case VK_F11: return Toolkit.getProperty("AWT.f11", "F11");
  351.           case VK_F12: return Toolkit.getProperty("AWT.f12", "F12");
  352.           case VK_DELETE: return Toolkit.getProperty("AWT.delete", "Delete");
  353.           case VK_NUM_LOCK: return Toolkit.getProperty("AWT.numLock", "Num Lock");
  354.           case VK_SCROLL_LOCK: return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock");
  355.           case VK_PRINTSCREEN: return Toolkit.getProperty("AWT.printScreen", "Print Screen");
  356.           case VK_INSERT: return Toolkit.getProperty("AWT.insert", "Insert");
  357.           case VK_HELP: return Toolkit.getProperty("AWT.help", "Help");
  358.           case VK_META: return Toolkit.getProperty("AWT.meta", "Meta");
  359.           case VK_BACK_QUOTE: return Toolkit.getProperty("AWT.backQuote", "Back Quote");
  360.           case VK_QUOTE: return Toolkit.getProperty("AWT.quote", "Quote");
  361.              
  362.           case VK_FINAL: return Toolkit.getProperty("AWT.final", "Final");
  363.           case VK_CONVERT: return Toolkit.getProperty("AWT.convert", "Convert");
  364.           case VK_NONCONVERT: return Toolkit.getProperty("AWT.noconvert", "No Convert");
  365.           case VK_ACCEPT: return Toolkit.getProperty("AWT.accept", "Accept");
  366.           case VK_MODECHANGE: return Toolkit.getProperty("AWT.modechange", "Mode Change");
  367.           case VK_KANA: return Toolkit.getProperty("AWT.kana", "Kana");
  368.       case VK_KANJI: return Toolkit.getProperty("AWT.kanji", "Kanji");
  369.         }
  370.  
  371.         if (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) {
  372.             String numpad = Toolkit.getProperty("AWT.numpad", "NumPad");
  373.         char c = (char)(keyCode - VK_NUMPAD0 + '0');
  374.             return numpad + "-" + c;
  375.         }
  376.  
  377.         String unknown = Toolkit.getProperty("AWT.unknown", "Unknown keyCode");
  378.         return unknown + ": 0x" + Integer.toString(keyCode, 16);
  379.     }
  380.  
  381.     /**
  382.      * Returns a String describing the modifier key(s), such as "Shift",
  383.      * or "Ctrl+Shift".  These strings can be localized by changing the 
  384.      * awt.properties file.
  385.      */
  386.     public static String getKeyModifiersText(int modifiers) {
  387.         StringBuffer buf = new StringBuffer();
  388.         if ((modifiers & InputEvent.META_MASK) != 0) {
  389.             buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
  390.             buf.append("+");
  391.         }
  392.         if ((modifiers & InputEvent.CTRL_MASK) != 0) {
  393.             buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
  394.             buf.append("+");
  395.         }
  396.         if ((modifiers & InputEvent.ALT_MASK) != 0) {
  397.             buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
  398.             buf.append("+");
  399.         }
  400.         if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
  401.             buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
  402.             buf.append("+");
  403.         }
  404.         if (buf.length() > 0) {
  405.             buf.setLength(buf.length()-1); // remove trailing '+'
  406.         }
  407.         return buf.toString();
  408.     }
  409.  
  410.     /** Returns whether or not the key in this event is an "action" key,
  411.      *  as defined in Event.java.
  412.      */
  413.     public boolean isActionKey() {
  414.         switch (keyCode) {
  415.           case VK_HOME:
  416.           case VK_END:
  417.           case VK_PAGE_UP:
  418.           case VK_PAGE_DOWN:
  419.           case VK_UP:
  420.           case VK_DOWN:
  421.           case VK_LEFT:
  422.           case VK_RIGHT:
  423.           case VK_F1:
  424.           case VK_F2:
  425.           case VK_F3:
  426.           case VK_F4:
  427.           case VK_F5:
  428.           case VK_F6:
  429.           case VK_F7:
  430.           case VK_F8:
  431.           case VK_F9:
  432.           case VK_F10:
  433.           case VK_F11:
  434.           case VK_F12:
  435.           case VK_PRINTSCREEN:
  436.           case VK_SCROLL_LOCK:
  437.           case VK_CAPS_LOCK:
  438.           case VK_NUM_LOCK:
  439.           case VK_PAUSE:
  440.           case VK_INSERT:
  441.               return true;
  442.         }
  443.         return false;
  444.     }
  445.  
  446.     public String paramString() {
  447.         String typeStr;
  448.         switch(id) {
  449.           case KEY_PRESSED:
  450.               typeStr = "KEY_PRESSED";
  451.               break;
  452.           case KEY_RELEASED:
  453.               typeStr = "KEY_RELEASED";
  454.               break;
  455.           case KEY_TYPED:
  456.               typeStr = "KEY_TYPED";
  457.               break;
  458.           default:
  459.               typeStr = "unknown type";
  460.         }
  461.  
  462.         String str = typeStr + ",keyCode=" + keyCode;
  463.         if (isActionKey() || keyCode == VK_ENTER || keyCode == VK_BACK_SPACE || 
  464.         keyCode == VK_TAB || keyCode == VK_ESCAPE || keyCode == VK_DELETE ||
  465.         (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9)) {
  466.             str += "," + getKeyText(keyCode);
  467.     } else if (keyChar == '\n' || keyChar == '\b' ||
  468.         keyChar == '\t' || keyChar == VK_ESCAPE || keyChar == VK_DELETE) {
  469.             str += "," + getKeyText(keyChar);
  470.         } else {
  471.             str += ",keyChar='" + keyChar + "'";
  472.         }
  473.         if (modifiers > 0) {
  474.             str += ",modifiers=" + getKeyModifiersText(modifiers);
  475.         }
  476.         return str;
  477.     }
  478.  
  479. }
  480.