Record Class Accelerator

java.lang.Object
java.lang.Record
limn.components.Accelerator

public record Accelerator(int key, int modifiers) extends Record
A keyboard shortcut for a MenuItem: one key plus the modifier mask that must be held with it. Immutable, comparable by value, and cheap enough to build inline.

 MenuItem.of("Save", this::save).setAccelerator(Accelerator.command(Keys.S));       // Cmd+S / Ctrl+S
 MenuItem.of("Save As…", this::saveAs).setAccelerator(
         Accelerator.command(Keys.S, Keys.MOD_SHIFT));                              // + Shift
 MenuItem.of("Refresh", this::refresh).setAccelerator(Accelerator.of(Keys.F5));     // no modifier
 

command(int) is the portable form and the one to reach for. It resolves to the Command key on macOS and to Control everywhere else, which is the difference between a shortcut that reads as native on both and one that fights the platform's own bindings. of(int, int) takes the mask literally and is for the rare shortcut that means a specific physical modifier on every platform.

The mask is matched exactly: Ctrl+S does not fire on Ctrl+Shift+S, so the two can be different commands. Only the four Keys.MOD_* bits are legal.

display() follows the platform: ⇧⌘S on macOS, where a shortcut is written as an unbroken run of symbols in the platform's own order, and Ctrl+Shift+S everywhere else, where the words are the convention. Modifier names are untranslated on both, as they are on every platform's own menus.

The symbols are not free, and the reason is worth knowing before removing them. No font in the ordinary UI stack has them (not Roboto, and not the Noto faces the toolkit falls back to for CJK and emoji), so the toolkit ships a small face built for exactly this set. A build stripped of that face draws a .notdef box where each modifier belongs, which is worse than the words it replaced.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Accelerator(int key, int modifiers)
    Creates an instance of a Accelerator record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    command(int key)
    The key with the platform's command modifier: Command on macOS, Control elsewhere.
    command(int key, int extraModifiers)
    command(int) plus further Keys.MOD_* bits (typically MOD_SHIFT).
    static int
     
    The hint a menu row shows, right-aligned: "⇧⌘S" on macOS, "Ctrl+Shift+S" elsewhere.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    int
    key()
    Returns the value of the key record component.
    boolean
    matches(int pressedKey, int pressedModifiers)
    Whether a key press with this exact modifier mask is this accelerator.
    int
    Returns the value of the modifiers record component.
    of(int key)
    The bare key, with no modifier: a function key, typically.
    of(int key, int modifiers)
    The key with an explicit, literal Keys.MOD_* mask on every platform.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Accelerator

      public Accelerator(int key, int modifiers)
      Creates an instance of a Accelerator record class.
      Parameters:
      key - the value for the key record component
      modifiers - the value for the modifiers record component
  • Method Details

    • of

      public static Accelerator of(int key)
      The bare key, with no modifier: a function key, typically.
    • of

      public static Accelerator of(int key, int modifiers)
      The key with an explicit, literal Keys.MOD_* mask on every platform.
    • command

      public static Accelerator command(int key)
      The key with the platform's command modifier: Command on macOS, Control elsewhere.
    • command

      public static Accelerator command(int key, int extraModifiers)
      command(int) plus further Keys.MOD_* bits (typically MOD_SHIFT).
    • commandModifier

      public static int commandModifier()
      Returns:
      Keys.MOD_SUPER on macOS, Keys.MOD_CONTROL elsewhere: the bit an application needs when it tests a raw KeyEvent mask itself and wants the same answer this class gives
    • matches

      public boolean matches(int pressedKey, int pressedModifiers)
      Whether a key press with this exact modifier mask is this accelerator. Exact, not "contains": an extra modifier makes it a different chord.
    • display

      public String display()
      The hint a menu row shows, right-aligned: "⇧⌘S" on macOS, "Ctrl+Shift+S" elsewhere. Never empty and never null.

      Both forms put the modifiers in their own platform's order, which is not the same order: macOS is Control, Option, Shift, Command and writes them with no separator at all, because the symbols already read as separate keys. The rest write Control, Alt, Shift, Meta joined with +.

    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • key

      public int key()
      Returns the value of the key record component.
      Returns:
      the value of the key record component
    • modifiers

      public int modifiers()
      Returns the value of the modifiers record component.
      Returns:
      the value of the modifiers record component