Interface KeyListener

All Superinterfaces:
ClientEventListener, EventListener

public interface KeyListener extends ClientEventListener
Listener interface for handling keyboard-related client events.

This interface defines callback methods for the three keyboard event types: key pressed, key up, and key down. Implementations can respond to keyboard interactions including typing, keyboard shortcuts, special key detection, and navigation. The three event types correspond to different phases of a key press lifecycle.

Features:

  • Handles key pressed events (deprecated in modern browsers)
  • Handles key up events when keys are released
  • Handles key down events when keys are initially pressed
  • Provides type-safe event handling through method overloading

Usage: Implement this interface to create handlers that respond to keyboard input. Register the listener with focusable elements to receive keyboard events. For most modern applications, focus on KeyDownEvent and KeyUpEvent, as KeyPressedEvent is deprecated in modern browsers.


 public class KeyboardNavigationHandler implements KeyListener {
     @Override
     public void onEvent(KeyDownEvent event) {
         int keyCode = event.getKeyCode();
         switch (keyCode) {
             case 13: // Enter
                 submitForm();
                 break;
             case 27: // Escape
                 closeDialog();
                 break;
             case 37: // Left arrow
                 navigatePrevious();
                 break;
             case 39: // Right arrow
                 navigateNext();
                 break;
         }
     }

     @Override
     public void onEvent(KeyUpEvent event) {
         // Handle key release if needed
     }

     @Override
     public void onEvent(KeyPressedEvent event) {
         // Deprecated - rarely used
     }
 }
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Invoked when a key is initially pressed down.
    void
    Deprecated.
    This event type is deprecated in modern browsers.
    void
    Invoked when a key is released.
  • Method Details

    • onEvent

      @Deprecated void onEvent(KeyPressedEvent event)
      Deprecated.
      This event type is deprecated in modern browsers. Use KeyDownEvent instead.
      Invoked when a key is pressed.

      This method corresponds to the keypress event, which is deprecated in modern browsers. It historically fired between keydown and keyup for printable characters. Modern applications should use KeyDownEvent instead.

      Parameters:
      event - the key pressed event
    • onEvent

      void onEvent(KeyUpEvent event)
      Invoked when a key is released.

      This method is called when a user releases a previously pressed key. KeyUpEvent is the final event in the keyboard event sequence and fires once per key release. Use this for detecting when keys are let go, such as ending keyboard-driven continuous actions.

      Parameters:
      event - the key up event containing key code and character information
    • onEvent

      void onEvent(KeyDownEvent event)
      Invoked when a key is initially pressed down.

      This method is called when a user first presses a key on the keyboard. KeyDownEvent fires repeatedly if the key is held down. This is the primary event for detecting keyboard input in modern applications, supporting all keys including special keys, modifier keys, and printable characters.

      Parameters:
      event - the key down event containing key code and character information