User Tools

Site Tools


Sidebar

Setup

Basics

Items and Item Groups

Blocks and Block Entities

Entities

World Generation

Miscellaneous

Advanced

Documentation

Examples


Fabric Сontributors

If you'd like to contribute to Fabric, you might be interested in these links:


Extremely Strange People


Wiki Meta

  • Wiki Meta - Starting point for contributing to the wiki
  • Wiki Agenda - See what is on the current agenda, and what other contributors are currently working on.
tutorial:keybinds

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("tutorial", "spook"),
  3. InputUtil.Type.KEYSYM,
  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.txt · Last modified: 2019/06/17 19:03 by draylar