Keybind

Keybind is a key binding button like the Controls screen's - click it, press a key or mouse button, or Escape to unbind - storing a version-independent Key.

Last updated

On this page

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; the next press is bound, and Escape unbinds. The key is a Key, the same on every Minecraft version.

Keybind.builder("zoomKey")
        .label(Text.translatable("mymod.zoomKey", "Zoom"))
        .value(() -> config.zoomKey)          // a Key; null shows as unbound
        .onChange(key -> config.zoomKey = key)
        .build();

Builder#

Method Required Default Notes
label(Supplier<Text>) / label(Text) yes the text before ": key"
value(Supplier<Key>) yes your current key; null or Key.UNBOUND for none
onChange(Consumer<Key>) yes stores the newly bound key; gets Key.UNBOUND when the player pressed Escape
allowMouse(boolean) no true whether mouse buttons can be bound; if false, a click while waiting just stops waiting
enabled, tooltip, visible no options every widget has

Methods#

Method Does
key() your current key; Key.UNBOUND if your supplier returns null
set(Key) binds a key, as given, and calls onChange. Use it for a Reset button: zoom.set(Key.keyboard(90)).

Behaviour#

Test it#

host.pressKey("zoomKey", Key.keyboard(82));     // binds R
host.pressKey("zoomKey", Key.ESCAPE);           // unbinds

Rendered as [keybind zoomKey] Zoom: R, or Zoom: Not Bound.

See also#