#

| Chapter 21 java.awt.event Reference |

# KeyEvent

## Name

KeyEvent

##

## Description

Key events are generated when the user types on the keyboard.

## Class Definition

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

## Constants

### CHAR_UNDEFINED

#### public final static int CHAR_UNDEFINED

This constant is used for key presses have that no associated character.

### KEY_FIRST

#### public final static int KEY_FIRST

Specifies the beginning range of key event ID values.

### KEY_LAST

#### public final static int KEY_LAST

Specifies the ending range of key event ID values.

### KEY_PRESSED

#### public final static int KEY_PRESSED

An event ID type for a key press.

### KEY_RELEASED

#### public final static int KEY_RELEASED

An event ID type for a key release.

### KEY_TYPED

#### public final static int KEY_TYPED

An event ID type for a typed key (a press and a release).

### VK_0

#### public final static int VK_0

The 0 key.

### VK_1

#### public final static int VK_1

The 1 key.

### VK_2

#### public final static int VK_2

The 2 key.

### VK_3

#### public final static int VK_3

The 3 key.

### VK_4

#### public final static int VK_4

The 4 key.

### VK_5

#### public final static int VK_5

The 5 key.

### VK_6

#### public final static int VK_6

The 6 key.

### VK_7

#### public final static int VK_7

The 7 key.

### VK_8

#### public final static int VK_8

The 8 key.

### VK_9

#### public final static int VK_9

The 9 key.

### VK_A

#### public final static int VK_A

The `a' key.

### VK_ACCEPT

#### public final static int VK_ACCEPT

This constant is used for Asian keyboards.

### VK_ADD

#### public final static int VK_ADD

The plus (+) key on the numeric keypad.

### VK_ALT

#### public final static int VK_ALT

The Alt key.

### VK_B

#### public final static int VK_B

The `b' key.

### VK_BACK_QUOTE

#### public final static int VK_BACK_QUOTE

The backquote (`) key.

### VK_BACK_SLASH

#### public final static int VK_BACK_SLASH

The backslash key.

### VK_BACK_SPACE

#### public final static int VK_BACK_SPACE

The Backspace key.

### VK_C

#### public final static int VK_C

The `c' key.

### VK_CANCEL

#### public final static int VK_CANCEL

The Cancel key.

### VK_CAPS_LOCK

#### public final static int VK_CAPS_LOCK

The Caps Lock key.

### VK_CLEAR

#### public final static int VK_CLEAR

The Clear key.

### VK_CLOSE_BRACKET

#### public final static int VK_CLOSE_BRACKET

The close bracket `]' key.

### VK_COMMA

#### public final static int VK_COMMA

The comma (,) key.

### VK_CONTROL

#### public final static int VK_CONTROL

The Control key.

### VK_CONVERT

#### public final static int VK_CONVERT

This constant is used for Asian keyboards.

### VK_D

#### public final static int VK_D

The `d' key.

### VK_DECIMAL

#### public final static int VK_DECIMAL

The decimal (.) key on the numeric keypad.

### VK_DELETE

#### public final static int VK_DELETE

The Delete key.

### VK_DIVIDE

#### public final static int VK_DIVIDE

The divide (/) key on the numeric keypad.

### VK_DOWN

#### public final static int VK_DOWN

The Down arrow key.

### VK_E

#### public final static int VK_E

The `e' key.

### VK_END

#### public final static int VK_END

The End key.

### VK_ENTER

#### public final static int VK_ENTER

The Enter key.

### VK_EQUALS

#### public final static int VK_ EQUALS

The equals (=) key.

### VK_ESCAPE

#### public final static int VK_ESCAPE

The Escape key.

### VK_F

#### public final static int VK_F

The `f' key.

### VK_F1

#### public final static int VK_F1

The F1 key.

### VK_F2

#### public final static int VK_F2

The F2 key.

### VK_F3

#### public final static int VK_F3

The F3 key.

### VK_F4

#### public final static int VK_F4

The F4 key.

### VK_F5

#### public final static int VK_F5

The F5 key.

### VK_F6

#### public final static int VK_F6

The F6 key.

### VK_F7

#### public final static int VK_F7

The F7 key.

### VK_F8

#### public final static int VK_F8

The F8 key.

### VK_F9

#### public final static int VK_F9

The F9 key.

### VK_F10

#### public final static int VK_F10

The F10 key.

### VK_F11

#### public final static int VK_F11

The F11 key.

### VK_F12

#### public final static int VK_F12

The F12 key.

### VK_FINAL

#### public final static int VK_FINAL

This constant is used for Asian keyboards.

### VK_G

#### public final static int VK_G

The `g' key.

### VK_H

#### public final static int VK_H

The `h' key.

### VK_HELP

#### public final static int VK_HELP

The Help key.

### VK_HOME

#### public final static int VK_HOME

The Home key.

### VK_I

#### public final static int VK_I

The `i' key.

### VK_INSERT

#### public final static int VK_INSERT

The Insert key.

### VK_J

#### public final static int VK_J

The `j' key.

### VK_K

#### public final static int VK_K

The `k' key.

### VK_KANA

#### public final static int VK_KANA

This constant is used for Asian keyboards.

### VK_KANJI

#### public final static int VK_KANJI

This constant is used for Asian keyboards.

### VK_L

#### public final static int VK_L

The `l' key.

### VK_LEFT

#### public final static int VK_LEFT

The Left arrow key.

### VK_M

#### public final static int VK_M

The `m' key.

### VK_MODECHANGE

#### public final static int VK_MODECHANGE

This constant is used for Asian keyboards.

### VK_META

#### public final static int VK_META

The Meta key.

### VK_MULTIPLY

#### public final static int VK_MULTIPLY

The * key on the numeric keypad.

### VK_N

#### public final static int VK_N

The `n' key.

### VK_NONCONVERT

#### public final static int VK_NONCONVERT

This constant is used for Asian keyboards.

### VK_NUM_LOCK

#### public final static int VK_NUM_LOCK

The Num Lock key.

### VK_NUMPAD0

#### public final static int VK_NUMPAD0

The 0 key on the numeric keypad.

### VK_NUMPAD1

#### public final static int VK_NUMPAD1

The 1 key on the numeric keypad.

### VK_NUMPAD2

#### public final static int VK_NUMPAD2

The 2 key on the numeric keypad.

### VK_NUMPAD3

#### public final static int VK_NUMPAD3

The 3 key on the numeric keypad.

### VK_NUMPAD4

#### public final static int VK_NUMPAD4

The 4 key on the numeric keypad.

### VK_NUMPAD5

#### public final static int VK_NUMPAD5

The 5 key on the numeric keypad.

### VK_NUMPAD6

#### public final static int VK_NUMPAD6

The 6 key on the numeric keypad.

### VK_NUMPAD7

#### public final static int VK_NUMPAD7

The 7 key on the numeric keypad.

### VK_NUMPAD8

#### public final static int VK_NUMPAD8

The 8 key on the numeric keypad.

### VK_NUMPAD9

#### public final static int VK_NUMPAD9

The 9 key on the numeric keypad.

### VK_O

#### public final static int VK_O

The `o' key.

### VK_OPEN_BRACKET

#### public final static int VK_OPEN_BRACKET

The open bracket `[` key.

### VK_P

#### public final static int VK_P

The `p' key.

### VK_PAGE_DOWN

#### public final static int VK_PAGE_DOWN

The Page Down key.

### VK_PAGE_UP

#### public final static int VK_PAGE_UP

The Page Up key.

### VK_PAUSE

#### public final static int VK_PAUSE

The Pause key.

### VK_PERIOD

#### public final static int VK_PERIOD

The period (.) key.

### VK_PRINTSCREEN

#### public final static int VK_PRINTSCREEN

The Print Screen key.

### VK_Q

#### public final static int VK_Q

The `q' key.

### VK_QUOTE

#### public final static int VK_QUOTE

The quotation mark (") key.

### VK_R

#### public final static int VK_R

The `r' key.

### VK_RIGHT

#### public final static int VK_RIGHT

The Right arrow key.

### VK_S

#### public final static int VK_S

The `s' key.

### VK_SCROLL_LOCK

#### public final static int VK_SCROLL_LOCK

The Scroll Lock key.

### VK_SEMICOLON

#### public final static int VK_SEMICOLON

The semicolon (;) key.

### VK_SEPARATER

#### public final static int VK_SEPARATER

The numeric separator key on the numeric keypad (i.e., the locale-dependent key used to separate groups of digits). A misspelling of VK_SEPARATOR.

### VK_SHIFT

#### public final static int VK_SHIFT

The Shift key.

### VK_SLASH

#### public final static int VK_SLASH

The slash (/) key.

### VK_SPACE

#### public final static int VK_SPACE

The space key.

### VK_SUBTRACT

#### public final static int VK_SUBTRACT

The subtract (-) key on the numeric keypad.

### VK_T

#### public final static int VK_T

The `t' key.

### VK_TAB

#### public final static int VK_TAB

The Tab key.

### VK_U

#### public final static int VK_U

The `u' key.

### VK_UNDEFINED

#### public final static int VK_UNDEFINED

An undefined key.

### VK_UP

#### public final static int VK_UP

The Up arrow key.

### VK_V

#### public final static int VK_V

The `v' key.

### VK_W

#### public final static int VK_W

The `w' key.

### VK_X

#### public final static int VK_X

The `x' key.

### VK_Y

#### public final static int VK_Y

The `y' key.

### VK_Z

#### public final static int VK_Z

The `z' key.

## Constructors

### KeyEvent

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

Parameters

  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.

Description

  Constructs a `KeyEvent` with the given characteristics.

## Class Methods

### getKeyModifiersText

#### public static String getKeyModifiersText(int modifiers)

Parameters

  modifiers

    One or more modifier keys.

Returns

  A string describing the modifiers.

### getKeyText

#### public static String getKeyText(int keyCode)

Parameters

  keyCode

    One of the key codes.

Returns

  A string describing the given key.

## Instance Methods

### getKeyChar

#### public char getKeyChar()

Returns

  The character corresponding to this event. KEY_TYPED events have characters.

### getKeyCode

#### public int getKeyCode()

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.

### isActionKey

#### public boolean isActionKey()

Returns

  `true` if this event is for one of the action keys; `false` otherwise.

Description

  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.

### paramString

#### public String paramString()

Returns

  A string with current settings of the `KeyEvent`.

Overrides

  `ComponentEvent``.paramString()`

Description

  Helper method for `toString()` to generate string of current settings.

### setKeyChar

#### public void setKeyChar(char keyChar)

Parameters

  keyChar

    The new key character.

Description

  Sets the character code of this `KeyEvent`.

### setKeyCode

#### public void setKeyCode (int keyCode)

Parameters

  keyCode

    The new key code.

Description

  Sets the key code of this `KeyEvent`.

### setModifiers

#### public void setModifiers (int modifiers)

Parameters

  modifiers

    The new modifiers.

Description

  This is a combination of the mask constants defined in `java.awt.event.InputEvent`.

## See Also

`Component`, `ComponentEvent`, `InputEvent`, `KeyAdapter`, `KeyListener`

KeyAdapter |  | KeyListener