Record Class Accelerator
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
ConstructorsConstructorDescriptionAccelerator(int key, int modifiers) Creates an instance of aAcceleratorrecord class. -
Method Summary
Modifier and TypeMethodDescriptionstatic Acceleratorcommand(int key) The key with the platform's command modifier: Command on macOS, Control elsewhere.static Acceleratorcommand(int key, int extraModifiers) static intdisplay()The hint a menu row shows, right-aligned:"⇧⌘S"on macOS,"Ctrl+Shift+S"elsewhere.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.intkey()Returns the value of thekeyrecord component.booleanmatches(int pressedKey, int pressedModifiers) Whether a key press with this exact modifier mask is this accelerator.intReturns the value of themodifiersrecord component.static Acceleratorof(int key) The bare key, with no modifier: a function key, typically.static Acceleratorof(int key, int modifiers) The key with an explicit, literalKeys.MOD_*mask on every platform.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
Accelerator
public Accelerator(int key, int modifiers) Creates an instance of aAcceleratorrecord class.- Parameters:
key- the value for thekeyrecord componentmodifiers- the value for themodifiersrecord component
-
-
Method Details
-
of
The bare key, with no modifier: a function key, typically. -
of
The key with an explicit, literalKeys.MOD_*mask on every platform. -
command
The key with the platform's command modifier: Command on macOS, Control elsewhere. -
command
-
commandModifier
public static int commandModifier()- Returns:
Keys.MOD_SUPERon macOS,Keys.MOD_CONTROLelsewhere: the bit an application needs when it tests a rawKeyEventmask 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
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
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. -
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. -
equals
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 '=='. -
key
public int key()Returns the value of thekeyrecord component.- Returns:
- the value of the
keyrecord component
-
modifiers
public int modifiers()Returns the value of themodifiersrecord component.- Returns:
- the value of the
modifiersrecord component
-