src/share/classes/java/awt/event/KeyEvent.java

Print this page




 913         NativeLibLoader.loadLibraries();
 914         if (!GraphicsEnvironment.isHeadless()) {
 915             initIDs();
 916         }
 917 
 918         AWTAccessor.setKeyEventAccessor(
 919             new AWTAccessor.KeyEventAccessor() {
 920                 public void setRawCode(KeyEvent ev, long rawCode) {
 921                     ev.rawCode = rawCode;
 922                 }
 923 
 924                 public void setPrimaryLevelUnicode(KeyEvent ev,
 925                                                    long primaryLevelUnicode) {
 926                     ev.primaryLevelUnicode = primaryLevelUnicode;
 927                 }
 928 
 929                 public void setExtendedKeyCode(KeyEvent ev,
 930                                                long extendedKeyCode) {
 931                     ev.extendedKeyCode = extendedKeyCode;
 932                 }




 933             });
 934     }
 935 
 936     /**
 937      * Initialize JNI field and method IDs for fields that may be
 938      * accessed from C.
 939      */
 940     private static native void initIDs();
 941 








 942     private KeyEvent(Component source, int id, long when, int modifiers,
 943                     int keyCode, char keyChar, int keyLocation, boolean isProxyActive) {
 944         this(source, id, when, modifiers, keyCode, keyChar, keyLocation);
 945         this.isProxyActive = isProxyActive;

 946     }
 947 
 948     /**
 949      * Constructs a <code>KeyEvent</code> object.
 950      * <p>This method throws an
 951      * <code>IllegalArgumentException</code> if <code>source</code>
 952      * is <code>null</code>.
 953      *
 954      * @param source    The <code>Component</code> that originated the event
 955      * @param id              An integer indicating the type of event.
 956      *                  For information on allowable values, see
 957      *                  the class description for {@link KeyEvent}
 958      * @param when      A long integer that specifies the time the event
 959      *                  occurred.
 960      *                     Passing negative or zero value
 961      *                     is not recommended
 962      * @param modifiers The modifier keys down during event (shift, ctrl,
 963      *                  alt, meta).
 964      *                     Passing negative value
 965      *                     is not recommended.


1006                 throw new IllegalArgumentException("invalid keyCode");
1007             }
1008             if (keyLocation != KEY_LOCATION_UNKNOWN) {
1009                 throw new IllegalArgumentException("invalid keyLocation");
1010             }
1011         }
1012 
1013         this.keyCode = keyCode;
1014         this.keyChar = keyChar;
1015 
1016         if ((keyLocation < KEY_LOCATION_UNKNOWN) ||
1017             (keyLocation > KEY_LOCATION_NUMPAD)) {
1018             throw new IllegalArgumentException("invalid keyLocation");
1019         }
1020         this.keyLocation = keyLocation;
1021         if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
1022             setNewModifiers();
1023         } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
1024             setOldModifiers();
1025         }

1026     }
1027 
1028     /**
1029      * Constructs a <code>KeyEvent</code> object.
1030      * <p> This method throws an
1031      * <code>IllegalArgumentException</code> if <code>source</code>
1032      * is <code>null</code>.
1033      *
1034      * @param source    The <code>Component</code> that originated the event
1035      * @param id              An integer indicating the type of event.
1036      *                  For information on allowable values, see
1037      *                  the class description for {@link KeyEvent}
1038      * @param when      A long integer that specifies the time the event
1039      *                  occurred.
1040      *                     Passing negative or zero value
1041      *                     is not recommended
1042      * @param modifiers The modifier keys down during event (shift, ctrl,
1043      *                  alt, meta).
1044      *                     Passing negative value
1045      *                     is not recommended.


1052      * @param keyChar   The Unicode character generated by this event, or
1053      *                  CHAR_UNDEFINED (for key-pressed and key-released
1054      *                  events which do not map to a valid Unicode character)
1055      * @throws IllegalArgumentException  if <code>id</code> is
1056      *     <code>KEY_TYPED</code> and <code>keyChar</code> is
1057      *     <code>CHAR_UNDEFINED</code>; or if <code>id</code> is
1058      *     <code>KEY_TYPED</code> and <code>keyCode</code> is not
1059      *     <code>VK_UNDEFINED</code>
1060      * @throws IllegalArgumentException if <code>source</code> is null
1061      * @see #getSource()
1062      * @see #getID()
1063      * @see #getWhen()
1064      * @see #getModifiers()
1065      * @see #getKeyCode()
1066      * @see #getKeyChar()
1067      */
1068     public KeyEvent(Component source, int id, long when, int modifiers,
1069                     int keyCode, char keyChar) {
1070         this(source, id, when, modifiers, keyCode, keyChar,
1071           KEY_LOCATION_UNKNOWN);

1072     }
1073 
1074     /**
1075      * @deprecated as of JDK1.1
1076      */
1077     @Deprecated
1078     public KeyEvent(Component source, int id, long when, int modifiers,
1079                     int keyCode) {
1080         this(source, id, when, modifiers, keyCode, (char)keyCode);

1081     }
1082 
1083     /**
1084      * Returns the integer keyCode associated with the key in this event.
1085      *
1086      * @return the integer code for an actual key on the keyboard.
1087      *         (For <code>KEY_TYPED</code> events, the keyCode is
1088      *         <code>VK_UNDEFINED</code>.)
1089      */
1090     public int getKeyCode() {
1091         return keyCode;
1092     }
1093 
1094     /**
1095      * Set the keyCode value to indicate a physical key.
1096      *
1097      * @param keyCode an integer corresponding to an actual key on the keyboard.
1098      */
1099     public void setKeyCode(int keyCode) {
1100         this.keyCode = keyCode;




 913         NativeLibLoader.loadLibraries();
 914         if (!GraphicsEnvironment.isHeadless()) {
 915             initIDs();
 916         }
 917 
 918         AWTAccessor.setKeyEventAccessor(
 919             new AWTAccessor.KeyEventAccessor() {
 920                 public void setRawCode(KeyEvent ev, long rawCode) {
 921                     ev.rawCode = rawCode;
 922                 }
 923 
 924                 public void setPrimaryLevelUnicode(KeyEvent ev,
 925                                                    long primaryLevelUnicode) {
 926                     ev.primaryLevelUnicode = primaryLevelUnicode;
 927                 }
 928 
 929                 public void setExtendedKeyCode(KeyEvent ev,
 930                                                long extendedKeyCode) {
 931                     ev.extendedKeyCode = extendedKeyCode;
 932                 }
 933 
 934                 public Component getOriginalSource( KeyEvent ev ) {
 935                     return ev.originalSource;
 936                 }
 937             });
 938     }
 939 
 940     /**
 941      * Initialize JNI field and method IDs for fields that may be
 942      * accessed from C.
 943      */
 944     private static native void initIDs();
 945 
 946     /**
 947      * The original event source.
 948      *
 949      * Event source can be changed during processing, but in some cases
 950      * we need to able to obtain original source.
 951      */
 952     private Component originalSource;
 953 
 954     private KeyEvent(Component source, int id, long when, int modifiers,
 955                     int keyCode, char keyChar, int keyLocation, boolean isProxyActive) {
 956         this(source, id, when, modifiers, keyCode, keyChar, keyLocation);
 957         this.isProxyActive = isProxyActive;
 958         originalSource = source;
 959     }
 960 
 961     /**
 962      * Constructs a <code>KeyEvent</code> object.
 963      * <p>This method throws an
 964      * <code>IllegalArgumentException</code> if <code>source</code>
 965      * is <code>null</code>.
 966      *
 967      * @param source    The <code>Component</code> that originated the event
 968      * @param id              An integer indicating the type of event.
 969      *                  For information on allowable values, see
 970      *                  the class description for {@link KeyEvent}
 971      * @param when      A long integer that specifies the time the event
 972      *                  occurred.
 973      *                     Passing negative or zero value
 974      *                     is not recommended
 975      * @param modifiers The modifier keys down during event (shift, ctrl,
 976      *                  alt, meta).
 977      *                     Passing negative value
 978      *                     is not recommended.


1019                 throw new IllegalArgumentException("invalid keyCode");
1020             }
1021             if (keyLocation != KEY_LOCATION_UNKNOWN) {
1022                 throw new IllegalArgumentException("invalid keyLocation");
1023             }
1024         }
1025 
1026         this.keyCode = keyCode;
1027         this.keyChar = keyChar;
1028 
1029         if ((keyLocation < KEY_LOCATION_UNKNOWN) ||
1030             (keyLocation > KEY_LOCATION_NUMPAD)) {
1031             throw new IllegalArgumentException("invalid keyLocation");
1032         }
1033         this.keyLocation = keyLocation;
1034         if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
1035             setNewModifiers();
1036         } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
1037             setOldModifiers();
1038         }
1039         originalSource = source;
1040     }
1041 
1042     /**
1043      * Constructs a <code>KeyEvent</code> object.
1044      * <p> This method throws an
1045      * <code>IllegalArgumentException</code> if <code>source</code>
1046      * is <code>null</code>.
1047      *
1048      * @param source    The <code>Component</code> that originated the event
1049      * @param id              An integer indicating the type of event.
1050      *                  For information on allowable values, see
1051      *                  the class description for {@link KeyEvent}
1052      * @param when      A long integer that specifies the time the event
1053      *                  occurred.
1054      *                     Passing negative or zero value
1055      *                     is not recommended
1056      * @param modifiers The modifier keys down during event (shift, ctrl,
1057      *                  alt, meta).
1058      *                     Passing negative value
1059      *                     is not recommended.


1066      * @param keyChar   The Unicode character generated by this event, or
1067      *                  CHAR_UNDEFINED (for key-pressed and key-released
1068      *                  events which do not map to a valid Unicode character)
1069      * @throws IllegalArgumentException  if <code>id</code> is
1070      *     <code>KEY_TYPED</code> and <code>keyChar</code> is
1071      *     <code>CHAR_UNDEFINED</code>; or if <code>id</code> is
1072      *     <code>KEY_TYPED</code> and <code>keyCode</code> is not
1073      *     <code>VK_UNDEFINED</code>
1074      * @throws IllegalArgumentException if <code>source</code> is null
1075      * @see #getSource()
1076      * @see #getID()
1077      * @see #getWhen()
1078      * @see #getModifiers()
1079      * @see #getKeyCode()
1080      * @see #getKeyChar()
1081      */
1082     public KeyEvent(Component source, int id, long when, int modifiers,
1083                     int keyCode, char keyChar) {
1084         this(source, id, when, modifiers, keyCode, keyChar,
1085           KEY_LOCATION_UNKNOWN);
1086         originalSource = source;
1087     }
1088 
1089     /**
1090      * @deprecated as of JDK1.1
1091      */
1092     @Deprecated
1093     public KeyEvent(Component source, int id, long when, int modifiers,
1094                     int keyCode) {
1095         this(source, id, when, modifiers, keyCode, (char)keyCode);
1096         originalSource = source;
1097     }
1098 
1099     /**
1100      * Returns the integer keyCode associated with the key in this event.
1101      *
1102      * @return the integer code for an actual key on the keyboard.
1103      *         (For <code>KEY_TYPED</code> events, the keyCode is
1104      *         <code>VK_UNDEFINED</code>.)
1105      */
1106     public int getKeyCode() {
1107         return keyCode;
1108     }
1109 
1110     /**
1111      * Set the keyCode value to indicate a physical key.
1112      *
1113      * @param keyCode an integer corresponding to an actual key on the keyboard.
1114      */
1115     public void setKeyCode(int keyCode) {
1116         this.keyCode = keyCode;