Class KeyDownEvent


public class KeyDownEvent extends KeyEvent
Event fired when a key is initially pressed down.

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 Details

    • KeyDownEvent

      public KeyDownEvent(String keyCode, String charCode, String which)
      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

      public void dispatchTo(KeyListener listener)
      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:
      dispatchTo in class Event<KeyListener>
      Parameters:
      listener - the KeyListener that will handle this event