Class Keybind

java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.Keybind

public final class Keybind extends Entry
A key binding button, as on vanilla's Controls screen: "label: key". Clicking it makes the button wait for the next key or mouse button ("> key <"); the next press is bound, and Escape unbinds. The key lives in the mod, as a Key, which is the same in every Minecraft version and can be stored as text with Key.name().
Keybind.builder("zoomKey")
        .label(Text.translatable("mymod.zoomKey", "Zoom"))
        .value(() -> config.zoomKey)                       // a Key; null shows as unbound
        .onChange(key -> config.zoomKey = key)
        .build();

// Storing it in a config file:
String stored = config.zoomKey.name();                     // "key.keyboard.z"
Key loaded = Key.fromName(stored);                         // null if the text is no key name

This widget only edits a key; it doesn't register a vanilla key mapping or listen for the key in game. The mod checks the key itself in its input handler, comparing with Key.code() (GLFW codes, 1.13+) or Key.toLwjgl2() (1.7.10 – 1.12.2). There are no modifier combinations (Ctrl + key).

Required: label, value, onChange. Mouse buttons can be bound unless allowMouse(false). Can't be in a MenuScreen's footer.

For backends: clicking the button starts waiting; the next key or mouse button goes to capture(Key). Backends show the key's name the way vanilla does (translated, following the keyboard layout), not Key.englishName().

  • Method Details

    • builder

      public static Keybind.Builder builder(String id)
      Starts building a key binding button.
      Parameters:
      id - the button's id, unique within its screen
      Returns:
      a new builder
      Throws:
      IllegalArgumentException - if id is invalid
    • label

      public Text label()
      The text before ": key", from the mod's supplier.
      Returns:
      the text shown before the key name
    • key

      public Key key()
      The mod's current key; Key.UNBOUND if its supplier returns null.
      Returns:
      the key bound now, or Key.UNBOUND for none
    • allowsMouse

      public boolean allowsMouse()
      Whether a mouse button can be bound; if not, a click while waiting for a key is ignored.
      Returns:
      true if a mouse button can be bound
    • set

      public void set(Key key)
      Binds key: passes it to onChange, even if it is the current key. Unlike capture(Key), binds Escape and mouse buttons as given. Mods can use it for a "Reset" button: zoom.set(Key.keyboard(90)).
      Parameters:
      key - the key, Key.UNBOUND to unbind
      Throws:
      IllegalStateException - if key is null
    • capture

      public boolean capture(Key pressed)
      What the player pressed while the button waited for a key: Escape unbinds, a mouse button binds only if allowsMouse(), anything else binds. The backend stops waiting either way and doesn't pass the press on.
      Parameters:
      pressed - the key or mouse button pressed
      Returns:
      false if it changed nothing (a mouse button that may not be bound), true otherwise
      Throws:
      IllegalStateException - if pressed is null
    • accept

      public <R> R accept(EntryVisitor<R> visitor)
      Description copied from class: Entry
      Calls the EntryVisitor method for this entry's type (visitButton for a Button, …) and returns its result. A Section visits as its header, a NumberField as its TextInput.
      Specified by:
      accept in class Entry
      Type Parameters:
      R - the visitor's result type
      Parameters:
      visitor - the visitor, usually a backend's widget factory
      Returns:
      what the visitor returned