Class KeyEvent
- Direct Known Subclasses:
KeyDownEvent,KeyPressedEvent,KeyUpEvent
This class serves as the parent for all keyboard events that occur when users press, release, or hold down keys. It encapsulates key code, character code, and which key information from browser keyboard events. Key events enable applications to implement keyboard shortcuts, navigation, form submission, and other keyboard-driven interactions.
Features:
- Base class for key down, key pressed, and key up events
- Stores key code, character code, and which key information
- Integrates with the KeyListener interface
- Supports detection of all keyboard keys including special keys
Usage: This class is not instantiated directly. Instead, use its concrete subclasses KeyDownEvent, KeyPressedEvent, and KeyUpEvent to represent specific keyboard interactions.
// Handled through KeyListener implementations
public void onEvent(KeyDownEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == 13) { // Enter key
submitForm();
}
}
- Since:
- 2007
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintReturns the character code of the pressed key.intReturns the key code of the pressed key.getWhich()Returns the which key value.Methods inherited from class com.oorian.messaging.events.client.ClientEvent
getSource, getTarget, setSource, setTargetMethods inherited from class com.oorian.messaging.events.Event
bubbles, dispatchTo
-
Constructor Details
-
KeyEvent
Constructs a new KeyEvent with the specified key information.Protected constructor that parses string representations of key codes from the browser event into integer values. This constructor is called by subclasses to initialize the key event data.
- Parameters:
keyCode- the key code as a string (will be parsed to integer)charCode- the character code as a string (will be parsed to integer)which- the which key value as a string
-
-
Method Details
-
getKeyCode
public int getKeyCode()Returns the key code of the pressed key.The key code represents the physical key on the keyboard, independent of the character it produces. For example, the 'A' key has key code 65 regardless of whether Shift is pressed. Common key codes include: 13 (Enter), 27 (Escape), 37-40 (arrow keys), 65-90 (A-Z).
- Returns:
- the integer key code of the pressed key
-
getCharCode
public int getCharCode()Returns the character code of the pressed key.The character code represents the Unicode value of the character produced by the key press, taking into account modifier keys. This is primarily useful for printable characters. For example, pressing 'A' with Shift produces a different character code than 'a' without Shift.
- Returns:
- the integer character code of the pressed key
-
getWhich
Returns the which key value.This is a legacy browser property that was used to identify which key was pressed. Modern applications should use getKeyCode() instead, but this property is maintained for backward compatibility.
- Returns:
- the which key value as a string
-