java.awt.event.KeyEvent (JDK 1.1)
An event of this type indicates that the user has pressed or released a key or typed a character.
Call getID() to determine the particular type of key event that has occurred. The constant KEY_PRESSED indicates that a key has been pressed, while the constant KEY_RELEASED indicates that a key has been released. Not all keystrokes actually correspond to or generate Unicode characters. Modifier keys and function keys, for example, do not correspond to characters. Furthermore, for internationalized input, multiple keystrokes are sometimes required to generate a single character of input. Therefore, getID() returns a third constant, KEY_TYPED, to indicate a KeyEvent that actually contains a character value.
For KEY_PRESSED and KEY_RELEASED key events, use getKeyCode() to obtain the "virtual key code" of the key that was pressed or released. KeyEvent defines a number of VK_ constants that represent these "virtual keys." Note that not all keys on all keyboards have corresponding constants in the KeyEvent class, and not all keyboards can generate all of the virtual key codes defined by the class. In JDK 1.1, the VK_ constants for letter keys, number keys, and some other keys have the same values as the ASCII encodings of the letters and numbers. You should not rely on this to always be the case, however. If the key that was pressed or released corresponds directly to a Unicode character, you can obtain that character by calling getKeyChar(). If there is not a corresponding Unicode character, this method returns the constant CHAR_UNDEFINED. The isActionKey() method returns true if the key that was pressed or released does not have a corresponding character.
For KEY_TYPED key events, use getKeyChar() to return the Unicode character that was typed. If you call getKeyCode() for this type of key event, it returns VK_UNDEFINED.
See InputEvent for information on inherited methods you can use to obtain the keyboard modifiers that were down during the event and other important methods.
Use getComponent(), inherited from ComponentEvent, to determine what component the event occurred over.
The static method getKeyText() returns a (possibly localized) textual name for a given key code. The static method getKeyModifiersText() returns a (possibly localized) textual description for a set of modifiers.
The KeyEvent has methods that allow you to change the key code, key character, or modifiers of an event. These methods, along with the consume() method, allow a KeyListener to perform filtering of key events before they are passed to the underlying AWT component.
public classKeyEventextends InputEvent { //Public ConstructorspublicKeyEvent(Componentsource, intid, longwhen, intmodifiers, intkeyCode, charkeyChar); publicKeyEvent(Componentsource, intid, longwhen, intmodifiers, intkeyCode); //Constants//Event Type Constantspublic static final intKEY_FIRST; public static final intKEY_LAST; public static final intKEY_PRESSED; public static final intKEY_RELEASED; public static final intKEY_TYPED; //Undefined Key and Characterpublic static final intVK_UNDEFINED; public static final charCHAR_UNDEFINED; //Alphanumeric Keyspublic static final intVK_A, VK_B,VK_C,VK_D,VK_E,VK_F,VK_G,VK_H,VK_I; public static final intVK_J,VK_K,VK_L,VK_M,VK_N,VK_O,VK_P,VK_Q,VK_R; public static final intVK_S,VK_T,VK_U,VK_V,VK_W,VK_X,VK_Y,VK_Z; public static final intVK_SPACE; public static final intVK_0,VK_1,VK_2,VK_3,VK_4,VK_5,VK_6,VK_7,VK_8,VK_9; public static final intVK_NUMPAD0,VK_NUMPAD1,VK_NUMPAD2,VK_NUMPAD3,VK_NUMPAD4; public static final intVK_NUMPAD5,VK_NUMPAD6,VK_NUMPAD7,VK_NUMPAD8,VK_NUMPAD9; //Control Keyspublic static final intVK_BACK_SPACE,VK_ENTER,VK_ESCAPE,VK_TAB; //Modifier Keyspublic static final intVK_ALT,VK_CAPS_LOCK,VK_CONTROL,VK_META,VK_SHIFT; //Function Keyspublic static final intVK_F1,VK_F2,VK_F3,VK_F4,VK_F5,VK_F6; public static final intVK_F7,VK_F8,VK_F9,VK_F10,VK_F11,VK_F12; public static final intVK_PRINTSCREEN,VK_SCROLL_LOCK,VK_PAUSE; public static final intVK_DELETE,VK_INSERT; public static final intVK_PAGE_DOWN,VK_PAGE_UP; public static final intVK_DOWN,VK_LEFT,VK_RIGHT,VK_UP; public static final intVK_END,VK_HOME; public static final intVK_ACCEPT,VK_NUM_LOCK,VK_CANCEL; public static final intVK_CLEAR,VK_CONVERT,VK_FINAL; public static final intVK_HELP,VK_KANA,VK_KANJI; public static final intVK_MODECHANGE,VK_NONCONVERT; //Punctuation Keyspublic static final intVK_ADD,VK_BACK_QUOTE,VK_BACK_SLASH; public static final intVK_CLOSE_BRACKET,VK_COMMA,VK_DECIMAL; public static final intVK_DIVIDE,VK_EQUALS,VK_MULTIPLY; public static final intVK_OPEN_BRACKET,VK_PERIOD,VK_QUOTE; public static final intVK_SEMICOLON,VK_SEPARATER,VK_SLASH; public static final intVK_SUBTRACT; //Class Methodspublic static StringgetKeyModifiersText(intmodifiers); public static StringgetKeyText(intkeyCode); //Public Instance Methodspublic chargetKeyChar(); public intgetKeyCode(); public booleanisActionKey(); public StringparamString(); //Overrides ComponentEventpublic voidsetKeyChar(charkeyChar); public voidsetKeyCode(intkeyCode); public voidsetModifiers(intmodifiers); }
Hierarchy:
Object->EventObject(Serializable)->AWTEvent->ComponentEvent->InputEvent->KeyEvent
Passed To:
AWTEventMulticaster.keyPressed(), AWTEventMulticaster.keyReleased(), AWTEventMulticaster.keyTyped(), Component.processKeyEvent(), KeyAdapter.keyPressed(), KeyAdapter.keyReleased(), KeyAdapter.keyTyped(), KeyListener.keyPressed(), KeyListener.keyReleased(), KeyListener.keyTyped()