Class KeyHandler

Object
KeyHandler

public class KeyHandler extends Object
Handles global key events and delegates actions to the owning EvoLudoWeb.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static KeyHandler
    Controller that currently has priority (typically the popup lab).
    private final EvoLudoWeb
    The EvoLudoWeb instance that owns this controller.
    private boolean
    Tracks whether the Alt modifier is currently pressed.
    private boolean
    Tracks whether the Shift modifier is currently pressed.
    static final String
    String value emitted when the Alt key is pressed.
    static final String
    String value emitted when the Backspace key is pressed.
    static final String
    String value emitted when the Delete key is pressed.
    static final String
    String value emitted when the Enter key is pressed.
    static final String
    String value emitted when the Escape key is pressed.
    static final String
    String value emitted when the Shift key is pressed.
    private static final List<KeyHandler>
    All handlers that have requested global registration.
    private static boolean
    Tracks whether the global JS listeners have been installed.
    private boolean
    Indicates whether this controller has been registered globally.
    private static int
    Number of handlers that have requested registration.
    private int
    Index of previously active view before switching to console view.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initializes the key event handler.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static void
    Installs JS listeners that route keyboard events to all controllers.
    private static boolean
    Dispatches keydown events first to the active controller, then to others.
    private static boolean
    Dispatches keyup events first to the active controller, then to others.
    private boolean
    Handles key events when the command line options field is active.
    private boolean
    Handles toggling the console view.
    private boolean
    Handles the Delete and Backspace key functionality.
    private boolean
    Handles the Escape key functionality.
    private boolean
    Handles exporting the current model state.
    private boolean
    Handles toggling fullscreen mode.
    private boolean
    Handles global key events.
    private boolean
    Handles numeric view switching for keys 1-9.
    void
    Hides the touch-activated Alt mode and restores the original labels.
    boolean
    Check whether this controller currently receives priority for key events.
    boolean
    Determine whether the Alt modifier key is currently engaged.
    boolean
    Determine whether the Shift modifier key is currently engaged.
    boolean
    Process keydown events to allow for repeating keyboard shortcuts.
    boolean
    Process keyup events to allow for non-repeating keyboard shortcuts.
    void
    Registers the handler and installs global listeners if needed.
    private static void
    Registers a handler for global key event handling.
    private static void
    Removes the JS listeners that were installed by addGlobalKeyListeners().
    private static void
    Sets the active handler that receives priority for key events.
    void
    Marks Alt mode active for touch interactions so button labels update accordingly.
    void
    Unregisters the handler and removes listeners when no handlers remain.
    private static void
    Unregisters a handler from global key event handling.
    private void
    Updates the modifier key states when keys are released.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • gui

      private final EvoLudoWeb gui
      The EvoLudoWeb instance that owns this controller.
    • isAltDown

      private boolean isAltDown
      Tracks whether the Alt modifier is currently pressed.
    • isShiftDown

      private boolean isShiftDown
      Tracks whether the Shift modifier is currently pressed.
    • KEY_ALT

      public static final String KEY_ALT
      String value emitted when the Alt key is pressed.
      See Also:
    • KEY_SHIFT

      public static final String KEY_SHIFT
      String value emitted when the Shift key is pressed.
      See Also:
    • KEY_ENTER

      public static final String KEY_ENTER
      String value emitted when the Enter key is pressed.
      See Also:
    • KEY_ESCAPE

      public static final String KEY_ESCAPE
      String value emitted when the Escape key is pressed.
      See Also:
    • KEY_BACKSPACE

      public static final String KEY_BACKSPACE
      String value emitted when the Backspace key is pressed.
      See Also:
    • KEY_DELETE

      public static final String KEY_DELETE
      String value emitted when the Delete key is pressed.
      See Also:
    • active

      private static KeyHandler active
      Controller that currently has priority (typically the popup lab).
    • keyHandlers

      private static final List<KeyHandler> keyHandlers
      All handlers that have requested global registration.
    • registrationCount

      private static int registrationCount
      Number of handlers that have requested registration.
    • listenersInstalled

      private static boolean listenersInstalled
      Tracks whether the global JS listeners have been installed.
    • registered

      private boolean registered
      Indicates whether this controller has been registered globally.
    • toggleIdx

      private int toggleIdx
      Index of previously active view before switching to console view.
  • Constructor Details

    • KeyHandler

      public KeyHandler(EvoLudoWeb gui)
      Initializes the key event handler.
      Parameters:
      gui - the owning EvoLudoWeb instance
  • Method Details

    • register

      public void register()
      Registers the handler and installs global listeners if needed.
    • unregister

      public void unregister()
      Unregisters the handler and removes listeners when no handlers remain.
    • setActive

      private static void setActive(KeyHandler handler)
      Sets the active handler that receives priority for key events.
      Parameters:
      handler - the handler to set active
    • registerGlobal

      private static void registerGlobal(KeyHandler handler)
      Registers a handler for global key event handling.
      Parameters:
      handler - the handler to register
    • unregisterGlobal

      private static void unregisterGlobal(KeyHandler handler)
      Unregisters a handler from global key event handling.
      Parameters:
      handler - the handler to unregister
    • showAltModeFromTouch

      public void showAltModeFromTouch()
      Marks Alt mode active for touch interactions so button labels update accordingly.
    • hideAltModeFromTouch

      public void hideAltModeFromTouch()
      Hides the touch-activated Alt mode and restores the original labels.
    • dispatchKeyDown

      private static boolean dispatchKeyDown(String key)
      Dispatches keydown events first to the active controller, then to others.
      Parameters:
      key - the key value reported by the browser event
      Returns:
      true if any controller consumed the event
    • dispatchKeyUp

      private static boolean dispatchKeyUp(String key)
      Dispatches keyup events first to the active controller, then to others.
      Parameters:
      key - the key value reported by the browser event
      Returns:
      true if any controller consumed the event
    • onKeyDown

      public boolean onKeyDown(String key)
      Process keydown events to allow for repeating keyboard shortcuts. Use for events where repeating does make sense, such as advancing a model by a single step or changing the speed of the model execution by adjusting the delay between subsequent updates. For non-repeating events, such starting or stopping the model or changing the view, see onKeyUp(String). The set of keys handled by onKeyUp and onKeyDown should be disjoint.

      Implementation Notes:

      • keydown events are ignored if:
        1. this EvoLudo model is not visible.
        2. in an ePub, except when on a standalone page.
      • keydown events do not propagate further (stopPropagation() is always called).
      • returning true also prevents default behaviour (calls preventDefault()).

      Global shortcuts provided for the following keys:

      Alt
      Toggles the mode for some buttons. For example to switch between Init and Reset.
      Shift
      Toggles the mode for some keys, see Enter below for an example.
      ArrowRight, n
      Advance the model by a single step. Same as pressing the Step-button.
      +, =
      Increase the speed of the model execution. Decrease the delay between updates. Moves the speed-slider to the right.
      -
      Decrease the speed of the model execution. Increase the delay between updates. Moves the speed-slider to the right.
      Parameters:
      key - the string value of the pressed key
      Returns:
      true if key has been handled
      See Also:
    • onKeyUp

      public boolean onKeyUp(String key)
      Process keyup events to allow for non-repeating keyboard shortcuts. Use for events where repeating does not make sense, such as stopping a model or changing views. For repeating events, such as advancing the model by a single step, see onKeyDown(String). The set of keys handled by onKeyUp and onKeyDown should be disjoint.

      Implementation Notes:

      • keyup events are ignored if:
        1. this EvoLudo model is not visible.
        2. the command line options field has the focus. With the exception of Shift-Enter to apply the new settings to the model.
        3. in an ePub, except when on a standalone page.
      • keyup events do not propagate further (stopPropagation() is always called).
      • returning true also prevents default behaviour (calls preventDefault()).
      keyup events are ignored if:
      • this EvoLudo model is not visible.
      • the command line options field has the focus. With the exception of Shift-Enter, which applies the new settings to the model.
      • when shown in an ePub, except when on a standalone page.
      • keydown event does not propagate further.
      • returning true also prevents default behaviour.

      Global shortcuts provided for the following keys:

      Alt
      Toggles the mode for some buttons. For example to switch between Init and Reset.
      Shift
      Toggles the mode for some keys, see Enter below for an example.
      0
      Toggles the visibility of the field to view and modify parameter settings.
      1-9
      Quick view selector. Switches to data view with the selected index if it exists. 1 is the first view etc.
      Enter, Space
      Starts (or stops) the current model. Note, Shift-Enter applies the new parameter settings if the field is visible and has the keyboard focus. Same as pressing the Apply-button.
      Escape
      Implements several functions depending on context:
      1. Ignored in ePub.
      2. Closes current EvoLudo simulation if running in a EvoLudoTrigger popup panel.
      3. Stops any running model.
      4. Initializes a model that is not running. Note, resets the model if Alt is pressed.
      Backspace, Delete
      Stops running models and initializes (resets if Alt is pressed) stopped models.
      E
      Export the current state of the model (as a modified plist). Ignored if model is running and in ePubs.
      H
      Show the help screen with brief descriptions of all parameters in the console view.
      Parameters:
      key - the string value of the released key
      Returns:
      true if key has been handled
      See Also:
    • updateModifierStates

      private void updateModifierStates(String key)
      Updates the modifier key states when keys are released.
      Parameters:
      key - the released key
    • handleCLOActiveKeys

      private boolean handleCLOActiveKeys(String key)
      Handles key events when the command line options field is active.
      Parameters:
      key - the released key
      Returns:
      true if key has been handled
    • handleGlobalKeys

      private boolean handleGlobalKeys(String key)
      Handles global key events.
      Parameters:
      key - the released key
      Returns:
      true if key has been handled
    • handleNumericViewSwitch

      private boolean handleNumericViewSwitch(String key)
      Handles numeric view switching for keys 1-9.
      Parameters:
      key - the released key
      Returns:
      true if key has been handled
    • handleConsoleToggle

      private boolean handleConsoleToggle()
      Handles toggling the console view.
      Returns:
      true if key has been handled
    • handleEscapeKey

      private boolean handleEscapeKey()
      Handles the Escape key functionality.
      Returns:
      true if key has been handled
    • handleDeleteKeys

      private boolean handleDeleteKeys()
      Handles the Delete and Backspace key functionality.
      Returns:
      true if key has been handled
    • handleExport

      private boolean handleExport()
      Handles exporting the current model state.
      Returns:
      true if key has been handled
    • handleFullscreen

      private boolean handleFullscreen()
      Handles toggling fullscreen mode.
      Returns:
      true if key has been handled
    • addGlobalKeyListeners

      private static void addGlobalKeyListeners()
      Installs JS listeners that route keyboard events to all controllers.
    • removeGlobalKeyListeners

      private static void removeGlobalKeyListeners()
      Removes the JS listeners that were installed by addGlobalKeyListeners().
    • isActive

      public boolean isActive()
      Check whether this controller currently receives priority for key events.
      Returns:
      true if this controller currently receives priority for key events.
    • isAltDown

      public boolean isAltDown()
      Determine whether the Alt modifier key is currently engaged.
      Returns:
      true if the Alt modifier is currently engaged.
    • isShiftDown

      public boolean isShiftDown()
      Determine whether the Shift modifier key is currently engaged.
      Returns:
      true if the Shift modifier is currently engaged.