Class KeyDownEvent
This event is triggered when a user first presses a key on the keyboard, before the key is released. KeyDownEvent is the first event in the keyboard event sequence (keydown, keypress, keyup) and fires repeatedly if the key is held down. This event is commonly used for detecting modifier keys (Shift, Ctrl, Alt), function keys, arrow keys, and implementing keyboard shortcuts.
Features:
- Fires when a key is initially pressed (first event in sequence)
- Repeats while the key is held down
- Provides key code, character code, and which key information
- Supports detection of all keys including non-printable keys
Usage: Handle this event through a KeyListener to respond to initial key presses. Common use cases include implementing keyboard shortcuts, detecting special keys (Enter, Escape, arrow keys), and preventing default key behavior.
public class ShortcutHandler implements KeyListener {
@Override
public void onEvent(KeyDownEvent event) {
int keyCode = event.getKeyCode();
// Detect Escape key
if (keyCode == 27) {
closeDialog();
}
// Detect Enter key
if (keyCode == 13) {
submitForm();
}
}
@Override
public void onEvent(KeyPressedEvent event) { }
@Override
public void onEvent(KeyUpEvent event) { }
}
- Since:
- 2007
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionKeyDownEvent(String keyCode, String charCode, String which) Constructs a new KeyDownEvent with the specified key information. -
Method Summary
Modifier and TypeMethodDescriptionvoiddispatchTo(KeyListener listener) Dispatches this event to the specified listener.Methods inherited from class com.oorian.messaging.events.client.KeyEvent
getCharCode, getKeyCode, getWhichMethods inherited from class com.oorian.messaging.events.client.ClientEvent
getSource, getTarget, setSource, setTarget
-
Constructor Details
-
KeyDownEvent
Constructs a new KeyDownEvent with the specified key information.Creates a key down event with key code, character code, and which key information from the browser's keyboard event.
- 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
-
dispatchTo
Dispatches this event to the specified listener.This method implements the visitor pattern to deliver the event to the appropriate handler method on the KeyListener. It calls the listener's onEvent(KeyDownEvent) method.
- Specified by:
dispatchToin classEvent<KeyListener>- Parameters:
listener- the KeyListener that will handle this event
-