Class KeyEvent in Java

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.

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. We 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, we 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 we call getKeyCode() for this type of key event, it returns VK_UNDEFINED.
See InputEvent for information on inherited methods we 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 us to change the key code, key character, or modifiers of an event. These methods, along with with the consume() method, allow a KeyListener to perform filtering of key events before they are passed to the underlying AWT component.

In short, Key events are generated when the user types on the keyboard.

The class structure for the class KeyEvent is given as

public class java.awt.event.KeyEvent extends java.awt.event.InputEvent{
// Public Constructors
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar);
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode);
// Constants
// Event Type Constants
public static final int KEY_FIRST;
public static final int KEY_LAST;
public static final int KEY_PRESSED;
public static final int KEY_RELEASED;
public static final int KEY_TYPED;
// Undefined Key and Character
public static final int VK_UNDEFINED;
public static final char CHAR_UNDEFINED;
// Alphanumeric Keys
public static final int VK_A, VK_B, VK_C, VK_D, VK_E, VK_F, VK_G, VK_H,VK_I;
public static final int VK_J, VK_K, VK_L, VK_M, VK_N, VK_O, VK_P, VK_Q,VK_R;
public static final int VK_S, VK_T, VK_U, VK_V, VK_W, VK_X, VK_Y, VK_Z;
public static final int VK_SPACE;
public static final int VK_0, VK_1, VK_2, VK_3, VK_4, VK_5, VK_6, VK_7,VK_8, VK_9;
public static final int VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3,VK_NUMPAD4;
public static final int VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, VK_NUMPAD8,VK_NUMPAD9;
// Control Keys
public static final int VK_BACK_SPACE, VK_ENTER, VK_ESCAPE, VK_TAB;
// Modifier Keys
public static final int VK_ALT, VK_CAPS_LOCK, VK_CONTROL, VK_META,VK_SHIFT;
// Function Keys
public static final int VK_F1, VK_F2, VK_F3, VK_F4, VK_F5, VK_F6;
public static final int VK_F7, VK_F8, VK_F9, VK_F10, VK_F11, VK_F12;
public static final int VK_PRINTSCREEN, VK_SCROLL_LOCK, VK_PAUSE;
public static final int VK_DELETE, VK_INSERT;
public static final int VK_PAGE_DOWN, VK_PAGE_UP;
public static final int VK_DOWN, VK_LEFT, VK_RIGHT, VK_UP;
public static final int VK_END, VK_HOME;
public static final int VK_ACCEPT, VK_NUM_LOCK, VK_CANCEL;
public static final int VK_CLEAR, VK_CONVERT, VK_FINAL;
public static final int VK_HELP, VK_KANA, VK_KANJI;
public static final int VK_MODECHANGE, VK_NONCONVERT;
// Punctuation Keys
public static final int VK_ADD, VK_BACK_QUOTE, VK_BACK_SLASH;
public static final int VK_CLOSE_BRACKET, VK_COMMA, VK_DECIMAL;
public static final int VK_DIVIDE, VK_EQUALS, VK_MULTIPLY;
public static final int VK_OPEN_BRACKET, VK_PERIOD, VK_QUOTE;
public static final int VK_SEMICOLON, VK_SEPARATER, VK_SLASH;
public static final int VK_SUBTRACT;
// Class Methods
public static String getKeyModifiersText(int modifiers);
public static String getKeyText(int keyCode);
// Public Instance Methods
public char getKeyChar();
public int getKeyCode();
public boolean isActionKey();
public String paramString(); // Overrides ComponentEvent
public void setKeyChar(char keyChar);
public void setKeyCode(int keyCode);
public void setModifiers(int modifiers);
}

The details of the class structure are given as follows:

public final static int CHAR_UNDEFINED;

public final static int CHAR_UNDEFINED is a constant that is used for key presses have that no associated character.

public final static int KEY_FIRST;

public final static int KEY_FIRST is a constant that specifies the beginning range of key event ID values.

public final static int KEY_LAST;

public final static int KEY_LAST is a constant that specifies the ending range of key event ID values.

public final static int KEY_PRESSED;

public final static int KEY_PRESSED is a constant that is an event ID type for a key press.

public final static int KEY_RELEASED;

public final static int KEY_RELEASED is a constant that is an event ID type for a key release.

public final static int KEY_TYPED;

public final static int KEY_TYPED is a constant that is an event ID type for a typed key (a press and a release).

public final static int VK_0;

public final static int VK_0 is a constant that represents the 0 key.

public final static int VK_1;

public final static int VK_1 is a constant that represents the 1 key.

public final static int VK_2;

public final static int VK_2 is a constant that represents the 2 key.

public final static int VK_3;

public final static int VK_3 is a constant that represents the 3 key.

public final static int VK_4;

public final static int VK_4 is a constant that represents the 4 key.

public final static int VK_5;

public final static int VK_5 is a constant that represents the 5 key.

public final static int VK_6;

public final static int VK_6 is a constant that represents the 6 key.

public final static int VK_7;

public final static int VK_7 is a constant that represents the 7 key.

public final static int VK_8;

public final static int VK_8 is a constant that represents the 8 key.

public final static int VK_9;

public final static int VK_9 is a constant that represents the 9 key.

public final static int VK_A;

public final static int VK_A is a constant that represents the ‘a’ key.

public final static int VK_ACCEPT;

public final static int VK_ACCEPT is a constant that is used for Asian keyboards.

public final static int VK_ADD;

public final static int VK_ADD is a constant that represents the plus (+) key on the numeric keypad.

public final static int VK_ALT;

public final static int VK_ALT is a constant that represents the Alt key.

public final static int VK_B;

public final static int VK_B is a constant that represents the ‘b’ key.

public final static int VK_BACK_QUOTE;

public final static int VK_BACK_QUOTE is a constant that represents the backquote (‘) key.

public final static int VK_BACK_SLASH;

public final static int VK_BACK_SLASH is a constant that represents the backslash key.

public final static int VK_BACK_SPACE;

public final static int VK_BACK_SPACE is a constant that represents the Backspace key.

public final static int VK_C;

public final static int VK_C is a constant that represents the ‘c’ key.

public final static int VK_CANCEL;

public final static int VK_CANCEL is a constant that represents the Cancel key.

public final static int VK_CAPS_LOCK;

public final static int VK_CAPS_LOCK is a constant that represents the Caps Lock key.

public final static int VK_CLEAR;

public final static int VK_CLEAR is a constant that represents the Clear key.

public final static int VK_CLOSE_BRACKET;

public final static int VK_CLOSE_BRACKET is a constant that represents The close bracket ‘]’ key.

public final static int VK_COMMA;

public final static int VK_COMMA is a constant that represents the comma (,) key.

public final static int VK_CONTROL;

public final static int VK_CONTROL is a constant that represents the Control key.

public final static int VK_CONVERT;

public final static int VK_CONVERT is a constant that is used for Asian keyboards.

public final static int VK_D;

public final static int VK_D is a constant that represents the ‘d’ key.

public final static int VK_DECIMAL;

public final static int VK_DECIMAL is a constant that represents the decimal (.) key on the numeric keypad.

public final static int VK_DELETE;

public final static int VK_DELETE is a constant that represents the Delete key.

public final static int VK_DIVIDE;

public final static int VK_DIVIDE is a constant that represents the divide (/) key on the numeric keypad.

public final static int VK_DOWN;

public final static int VK_DOWN is a constant that represents the Down arrow key.

public final static int VK_E;

public final static int VK_E is a constant that represents the ‘e’ key.

public final static int VK_END;

public final static int VK_END is a constant that represents the End key.

public final static int VK_ENTER;

public final static int VK_ENTER is a constant that represents the Enter key.

public final static int VK_EQUALS;

public final static int VK_EQUALS is a constant that represents the equals (=) key.

public final static int VK_ESCAPE;

public final static int VK_ESCAPE is a constant that represents the Escape key.

public final static int VK_F;

public final static int VK_F is a constant that represents the ‘f’ key.

public final static int VK_F1;

public final static int VK_F1 is a constant that represents the F1 key.

public final static int VK_F2;

public final static int VK_F2 is a constant that represents the F2 key.

public final static int VK_F3;

public final static int VK_F3 is a constant that represents the F3 key.

public final static int VK_F4;

public final static int VK_F4 is a constant that represents the F4 key.

public final static int VK_F5;

public final static int VK_F5 is a constant that represents the F5 key.

public final static int VK_F6;

public final static int VK_F6 is a constant that represents the F6 key.

public final static int VK_F7;

public final static int VK_F7 is a constant that represents the F7 key.

public final static int VK_F8;

public final static int VK_F8 is a constant that represents the F8 key.

public final static int VK_F9;

public final static int VK_F9 is a constant that represents the F9 key.

public final static int VK_F10;

public final static int VK_F10 is a constant that represents the F10 key.

public final static int VK_F11;

public final static int VK_F11 is a constant that represents the F11 key.

public final static int VK_F12;

public final static int VK_F12 is a constant that represents the F12 key.

public final static int VK_FINAL;

public final static int VK_FINAL is a constant that is used for Asian keyboards.

public final static int VK_G;

public final static int VK_G is a constant that represents the ‘g’ key.

public final static int VK_H;

public final static int VK_H is a constant that represents the ‘h’ key.

public final static int VK_HELP;

public final static int VK_HELP is a constant that represents the Help key.

public final static int VK_HOME;

public final static int VK_HOME is a constant that represents the Home key.

public final static int VK_I;

public final static int VK_I is a constant that represents the ‘i’ key.

public final static int VK_INSERT;

public final static int VK_INSERT is a constant that represents the Insert key.

public final static int VK_J;

public final static int VK_J is a constant that represents the ‘j’ key.

public final static int VK_K;

public final static int VK_K is a constant that represents the ‘k’ key.

public final static int VK_KANA;

public final static int VK_KANA is a constant that is used for Asian keyboards.

public final static int VK_KANJI;

public final static int VK_KANJI is a constant that is used for Asian keyboards.

public final static int VK_L;

public final static int VK_L is a constant that represents the ‘l’ key.

public final static int VK_LEFT;

public final static int VK_LEFT is a constant that represents the Left arrow key.

public final static int VK_M;

public final static int VK_M is a constant that represents the ‘m’ key.

public final static int VK_META;

public final static int VK_META is a constant that represents the Meta key.

public final static int VK_MODECHANGE;

public final static int VK_MODECHANGE is a constant that is used for Asian keyboards.

public final static int VK_MULTIPLY;

public final static int VK_MULTIPLY is a constant that represents the * key on the numeric keypad.

public final static int VK_N;

public final static int VK_N is a constant that represents the ‘n’ key.

public final static int VK_NONCONVERT;

public final static int VK_NONCONVERT is a constant that is used for Asian keyboards.

public final static int VK_NUM_LOCK;

public final static int VK_NUM_LOCK is a constant that represents the Num Lock key.

public final static int VK_NUMPAD0;

public final static int VK_NUMPAD0 is a constant that represents the 0 key on the numeric keypad.

public final static int VK_NUMPAD1;

public final static int VK_NUMPAD1 is a constant that represents the 1 key on the numeric keypad.

public final static int VK_NUMPAD2;

public final static int VK_NUMPAD2 is a constant that represents the 2 key on the numeric keypad.

public final static int VK_NUMPAD3;

public final static int VK_NUMPAD3 is a constant that represents the 3 key on the numeric keypad.

public final static int VK_NUMPAD4;

public final static int VK_NUMPAD4 is a constant that represents the 4 key on the numeric keypad.

public final static int VK_NUMPAD5;

public final static int VK_NUMPAD5 is a constant that represents the 5 key on the numeric keypad.

public final static int VK_NUMPAD6;

public final static int VK_NUMPAD6 is a constant that represents the 6 key on the numeric keypad.

public final static int VK_NUMPAD7;

public final static int VK_NUMPAD7 is a constant that represents the 7 key on the numeric keypad.

public final static int VK_NUMPAD8;

public final static int VK_NUMPAD8 is a constant that represents the 8 key on the numeric keypad.

public final static int VK_NUMPAD9;

public final static int VK_NUMPAD9 is a constant that represents the 9 key on the numeric keypad.

public final static int VK_O;

public final static int VK_O is a constant that represents the ‘o’ key.

public final static int VK_OPEN_BRACKET;

public final static int VK_OPEN_BRACKET is a constant that represents the open bracket ‘[‘ key.

public final static int VK_P;

public final static int VK_P is a constant that represents the ‘p’ key.

public final static int VK_PAGE_DOWN;

public final static int VK_PAGE_DOWN is a constant that represents the Page Down key.

public final static int VK_PAGE_UP;

public final static int VK_PAGE_UP is a constant that represents the Page Up key.

public final static int VK_PAUSE;

public final static int VK_PAUSE is a constant that represents the Pause key.

public final static int VK_PERIOD;

public final static int VK_PERIOD is a constant that represents the period (.) key.

public final static int VK_PRINTSCREEN;

public final static int VK_PRINTSCREEN is a constant that represents the Print Screen key.

public final static int VK_Q;

public final static int VK_Q is a constant that represents the ‘q’ key.

public final static int VK_QUOTE;

public final static int VK_QUOTE is a constant that represents the quotation mark (“) key.

public final static int VK_R;

public final static int VK_R is a constant that represents the ‘r’ key.

public final static int VK_RIGHT;

public final static int VK_RIGHT is a constant that represents the Right arrow key.

public final static int VK_S;

public final static int VK_S is a constant that represents the ‘s’ key.

public final static int VK_SCROLL_LOCK;

public final static int VK_SCROLL_LOCK is a constant that represents the Scroll Lock key.

public final static int VK_SEMICOLON;

public final static int VK_SEMICOLON is a constant that represents the semicolon (;) key.

public final static int VK_SEPARATER;

public final static int VK_SEPARATER is a constant that represents the numeric separator key on the numeric keypad (i.e., the locale-dependent key used to separate groups of digits).Possibly a misspelling of VK_SEPARATOR.

public final static int VK_SHIFT;

public final static int VK_SHIFT is a constant that represents the Shift key.

public final static int VK_SLASH;

public final static int VK_SLASH is a constant that represents the slash (/) key.

public final static int VK_SPACE;

public final static int VK_SPACE is a constant that represents the space key.

public final static int VK_SUBTRACT;

public final static int VK_SUBTRACT is a constant that represents the subtract (-) key on the numeric keypad.

public final static int VK_T;

public final static int VK_T is a constant that represents the ‘t’ key.

public final static int VK_TAB;

public final static int VK_TAB is a constant that the Tab key.

public final static int VK_U;

public final static int VK_U is a constant that represents the ‘u’ key.

public final static int VK_UNDEFINED;

public final static int VK_UNDEFINED is a constant that represents an undefined key.

public final static int VK_UP;

public final static int VK_UP is a constant that represents the Up arrow key.

public final static int VK_V;

public final static int VK_V is a constant that represents the ‘v’ key.

public final static int VK_W;

public final static int VK_W is a constant that represents the ‘w’ key.

public final static int VK_X;

public final static int VK_X is a constant that represents the ‘x’ key.

public final static int VK_Y;

public final static int VK_Y is a constant that represents the ‘y’ key.

public final static int VK_Z;

public final static int VK_Z is a constant that represents the ‘z’ key.

public KeyEvent(Component source, int id, long when, int modifiers,int keyCode, char keyChar);

public KeyEvent(Component source, int id, long when, int modifiers,int keyCode, char keyChar) constructs a KeyEvent with the given characteristics.

Parameter
source – The object that generated the event.
id – The event type ID of the event.
when – When the event occurred, in milliseconds from the epoch.
modifiers – What modifier keys were pressed with this key.
keyCode – The code of the key.
keyChar – The character for this key.

public static String getKeyModifiersText(int modifiers);

public static String getKeyModifiersText(int modifiers) method returns a string describing the modifiers.

This method returns a string describing the modifiers.

Parameter
modifiers – One or more modifier keys.

public static String getKeyText(int keyCode);

public static String getKeyText(int keyCode) method returns a string describing the given key.

This method returns a string describing the given key.

Parameter
keyCode – One of the key codes.

public char getKeyChar();

public char getKeyChar() method returns the character corresponding to this event. KEY_TYPED events have characters.

This method returns the character corresponding to this event. KEY_TYPED events have characters.

public int getKeyCode();

public int getKeyCode() method returns the integer key code corresponding to this event. This will be one of the constants defined above. KEY_PRESSED and KEY_RELEASED events have codes. Key codes are virtual keys, not actual. Pressing the ‘a’ key is identical to ‘A’, but has different modifiers. Same for ‘/’ and ‘?’ on a standard keyboard.

This method returns the integer key code corresponding to this event. This will be one of the constants defined above. KEY_PRESSED and KEY_RELEASED events have codes. Key codes are virtual keys, not actual. Pressing the ‘a’ key is identical to ‘A’, but has different modifiers. Same for ‘/’ and ‘?’ on a standard keyboard.

public boolean isActionKey();

public boolean isActionKey() method returns true if this event is for one of the action keys; false otherwise. In general, an action key is a key that causes an action but has no printing equivalent. The action keys are the function keys, the arrow keys, Caps Lock, End, Home, Insert, Num Lock, Pause, Page Down, Page Up, Print Screen, and Scroll Lock. They do not generate a KEY_TYPED event, only KEY_PRESSED and KEY_RELEASED.

This method returns true if this event is for one of the action keys; false otherwise.

public String paramString();

public String paramString() method is a helper method for toString() to generate string of current settings.

This method returns a string with current settings of the KeyEvent.

public void setKeyChar(char keyChar);

public void setKeyChar(char keyChar) method sets the character code of this KeyEvent.

Parameter
keyChar – The new key character.

public void setKeyCode(int keyCode);

public void setKeyCode(int keyCode) method sets the key code of this KeyEvent.

Parameter
keyChar – The new key character.

public void setModifiers(int modifiers);

public void setModifiers(int modifiers) method is a combination of the mask constants defined in java.awt.event.InputEvent.

Parameter
modifiers – The new modifiers.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

Write A Comment