Class Keybind
java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.Keybind
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().
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescription<R> Raccept(EntryVisitor<R> visitor) Calls theEntryVisitormethod for this entry's type (visitButtonfor aButton, …) and returns its result.booleanWhether a mouse button can be bound; if not, a click while waiting for a key is ignored.static Keybind.BuilderStarts building a key binding button.booleanWhat the player pressed while the button waited for a key: Escape unbinds, a mouse button binds only ifallowsMouse(), anything else binds.key()The mod's current key;Key.UNBOUNDif its supplier returnsnull.label()The text before ": key", from the mod's supplier.voidBindskey: passes it toonChange, even if it is the current key.
-
Method Details
-
builder
Starts building a key binding button.- Parameters:
id- the button's id, unique within its screen- Returns:
- a new builder
- Throws:
IllegalArgumentException- ifidis invalid
-
label
The text before ": key", from the mod's supplier.- Returns:
- the text shown before the key name
-
key
The mod's current key;Key.UNBOUNDif its supplier returnsnull.- Returns:
- the key bound now, or
Key.UNBOUNDfor none
-
allowsMouse
public boolean allowsMouse()Whether a mouse button can be bound; if not, a click while waiting for a key is ignored.- Returns:
trueif a mouse button can be bound
-
set
Bindskey: passes it toonChange, even if it is the current key. Unlikecapture(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.UNBOUNDto unbind- Throws:
IllegalStateException- ifkeyis null
-
capture
What the player pressed while the button waited for a key: Escape unbinds, a mouse button binds only ifallowsMouse(), 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:
falseif it changed nothing (a mouse button that may not be bound),trueotherwise- Throws:
IllegalStateException- ifpressedis null
-
accept
Description copied from class:EntryCalls theEntryVisitormethod for this entry's type (visitButtonfor aButton, …) and returns its result. ASectionvisits as its header, aNumberFieldas itsTextInput.
-