home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Java AWT

Previous Chapter 21
java.awt.event Reference
Next
 

KeyEvent (New)

Name

KeyEvent (New)

[Graphic: Figure from the text]

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_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_CONVERT

public final static int VK_CONVERT

This constant is used for Asian keyboards.

VK_DECIMAL

public final static int VK_DECIMAL

The decimal (.) key on the numeric keypad.

VK_DIVIDE

public final static int VK_DIVIDE

The divide (/) key on the numeric keypad.

VK_FINAL

public final static int VK_FINAL

This constant is used for Asian keyboards.

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_MODECHANGE

public final static int VK_MODECHANGE

This constant is used for Asian keyboards.

VK_MULTIPLY

public final static int VK_MULTIPLY

The * key on the numeric keypad.

VK_NONCONVERT

public final static int VK_NONCONVERT

This constant is used for Asian keyboards.

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_QUOTE

public final static int VK_QUOTE

The quotation mark (") 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_SUBTRACT

public final static int VK_SUBTRACT

The subtract (-) key on the numeric keypad.

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


Previous Home Next
KeyAdapter (New) Book Index KeyListener (New)

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java