Class Shortcuts

java.lang.Object
com.vaadin.flow.component.Shortcuts

public final class Shortcuts extends Object
Collections of methods for configuring more complex Shortcut interactions.

Unlike the shortcut methods offered by Focusable and ClickNotifier, these methods allow for configuring the lifecycleOwner directly, making it possible to add the shortcut onto any component. The lifecycleOwner denotes the component to which the shortcut is bound to. If the lifecycle owner is not attached, visible, or enabled, the shortcut won't work, and vice-versa.

Since:
1.3
Author:
Vaadin Ltd.
See Also:
  • Method Details

    • addShortcutListener

      public static ShortcutRegistration addShortcutListener(Component lifecycleOwner, Command command, Key key, KeyModifier... keyModifiers)
      Invoke a Command when the shortcut is invoked.

      Registering a shortcut using this method will tie it to lifecycleOwner and the shortcut is available in the global scope.

      By default, the shortcut's listener is bound to UI. The listening component can be changed by calling ShortcutRegistration.listenOn(Component...).

      Parameters:
      lifecycleOwner - the component that controls, when the shortcut is active. If the component is either invisible or detached, the shortcut won't work. Cannot be null
      command - code to execute when the shortcut is invoked. Cannot be null
      key - primary Key used to trigger the shortcut. Cannot be null
      keyModifiers - KeyModifiers which also need to be pressed for the shortcut to trigger
      Returns:
      ShortcutRegistration for configuring and removing the shortcut
    • addShortcutListener

      public static ShortcutRegistration addShortcutListener(Component lifecycleOwner, ShortcutEventListener listener, Key key, KeyModifier... keyModifiers)
      Invoke a ShortcutEventListener when the shortcut is invoked.

      Registering a shortcut using this method will tie it to lifecycleOwner and the shortcut is available in the global scope.

      By default, the shortcut's listener is bound to UI. The listening component can be changed by calling ShortcutRegistration.listenOn(Component...).

      Parameters:
      lifecycleOwner - the component that controls, when the shortcut is active. If the component is either invisible or detached, the shortcut won't work. Cannot be null
      listener - listener to execute when the shortcut is invoked. Receives a ShortcutEvent. Cannot be null
      key - primary Key used to trigger the shortcut. Cannot be null
      keyModifiers - KeyModifiers which also need to be pressed for the shortcut to trigger
      Returns:
      ShortcutRegistration for configuring and removing the shortcut
    • setShortcutListenOnElement

      @Deprecated(since="25.4", forRemoval=true) public static Registration setShortcutListenOnElement(String elementLocatorJs, Component listenOnComponent)
      Deprecated, for removal: This API element is subject to removal in a future version.
      The browser compiles the locator from a string. Use setShortcutListenOnElementLocator(JsFunction, Component) with a function declared in a JsDefinition interface.
      Setup an element, that is only accessible on the browser (not server side), to listen for shortcut events on and delegate to the given component. The element will be located in the browser by evaluating the given JS expression. This needs to be set for each listen-on component instance.

      This should be only used by component developers, when their component is used as the listenOn component for shortcuts, and their component does some magic on the browser which means that the shortcut events are not coming through from the actual element. Thus when an application developer calls e.g.
      myButton.addClickShortcut(Key.ENTER).listenOn(dialog);
      the framework will automatically make sure the events are passed from the browser only element to the listenOn component (dialog in this case).

      The expression is evaluated with the element of the listen-on component as this. The browser compiles it from a string, so it does not work with a Content Security Policy that does not allow unsafe-eval. Use setShortcutListenOnElementLocator(JsFunction, Component) with a declared function instead.

      Parameters:
      elementLocatorJs - js execution string that references the desired element in DOM or null to any remove existing locator
      listenOnComponent - the component that is setup for listening shortcuts on ShortcutRegistration.listenOn(Component...)
      Returns:
      a registration for removing the locator, does not affect active shortcuts or if the locator has changed from what was set for this registration
      Since:
      6.0.2
    • setShortcutListenOnElementLocator

      public static Registration setShortcutListenOnElementLocator(JsFunction elementLocator, Component listenOnComponent)
      Setup an element, that is only accessible on the browser (not server side), to listen for shortcut events on and delegate to the given component. The element will be located in the browser by calling the given function. This needs to be set for each listen-on component instance.

      This should be only used by component developers, when their component is used as the listenOn component for shortcuts, and their component does some magic on the browser which means that the shortcut events are not coming through from the actual element. Thus when an application developer calls e.g.
      myButton.addClickShortcut(Key.ENTER).listenOn(dialog);
      the framework will automatically make sure the events are passed from the browser only element to the listenOn component (dialog in this case).

      The function is called without arguments and with the element of the listen-on component as this, and returns the element to listen on. Declare it in a JsDefinition interface, so that the browser does not compile it from a string, which also works with a Content Security Policy that does not allow unsafe-eval:

       @JsDefinition
       public interface OverlayJs extends Serializable {
      
           @JsExpression("return this.$.overlay;")
           JsFunction locateOverlay();
       }
      
       Shortcuts.setShortcutListenOnElementLocator(
               JsSnippets.of(OverlayJs.class).locateOverlay(), this);
       

      Unlike setShortcutListenOnElement(String, Component), which takes a JavaScript expression that the browser compiles, this takes a function that the build adds to the bundle.

      Parameters:
      elementLocator - the function that returns the desired element in DOM, or null to remove any existing locator
      listenOnComponent - the component that is setup for listening shortcuts on ShortcutRegistration.listenOn(Component...)
      Returns:
      a registration for removing the locator, does not affect active shortcuts or if the locator has changed from what was set for this registration
      Since:
      25.4