User Tools

Site Tools


tutorial:keybinds

This is an old revision of the document!


Custom Keybinds

Keybinds: straight from the keyboard

Minecraft handles user input from peripherals such as the keyboard & mouse using key-binds. When you press W your character moves forward, and when you press E your inventory opens. Every keybind can also be configured with the settings menu, so you can make your player move with arrow keys instead of WASD if you so desire.

Adding a key-bind is easy. You'll need to:

  • create a FabricKeyBinding object
  • register your key
  • react to the key being pressed

Creating your Keybind

Fabric API has a FabricKeyBinding object, which makes it easier to create your own KeyBinding. Declare one of these in an area of your preference:

private static FabricKeyBinding keyBinding;

FabricKeyBinding has a Builder for initialization. It takes in an Identifier, InputUtil.Type, key code, and binding category:

  1. keyBinding = FabricKeyBinding.Builder.create(
  2. new Identifier("wiki-keybinds", "spook"),
  3. InputUtil.Type.KEY_KEYBOARD,
  4. GLFW.GLFW_KEY_R,
  5. "Wiki Keybinds"
  6. ).build();

GLFW.GLFW_KEY_R can be replaced with whatever key you want the binding to default to. The category is related to how the keybinding is grouped in the settings page.

Registering your Keybind

To register your keybinding, use the KeybindingRegistry:

KeyBindingRegistry.INSTANCE.register(keyBinding);

If you log in to your game now, you will see your key binding in the settings page.

Responding to your Keybind

Unfortunately, there's no clear-cut way to respond to a keybinding. Most would agree the best way is to hook into the client tick event:

ClientTickCallback.EVENT.register(e ->
{
    if(keyBinding.isPressed()) System.out.println("was pressed!");
});

Keep note that this is entirely client-side. To have the server respond to a keybind, you'll need to send a custom packet and have the server handle it separately.

tutorial/keybinds.1558889193.txt.gz · Last modified: 2019/05/26 16:46 by jamieswhiteshirt