User Tools

Site Tools


Sidebar

Setup

Basics

Items

Blocks and Block Entities

Fluids

Entities

World Generation

Miscellaneous

Events

Mixins

Advanced

Tutorials for Minecraft 1.15

Tutorials for Minecraft 1.14

Documentation

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.

This tutorial assumes you have the key bindings API, if not add “fabric-key-binding-api-v1”:“*” to “depends” in your fabric.mod.json file.

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

  • create a KeyBinding object
  • react to the key being pressed

See here for an updated example.

Creating your Keybind

Declare one of these in an area of your preference:

private static KeyBinding keyBinding;

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

  1. keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding(
  2. "key.examplemod.spook", // The translation key of the keybinding's name
  3. InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse.
  4. GLFW.GLFW_KEY_R, // The keycode of the key
  5. "category.examplemod.test" // The translation key of the keybinding's category.
  6. ));

If you want a sticky key, add “() → true” as last parameter.

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.

Responding to your Keybind

The code here will print “Key 1 was pressed!” ingame.

ClientTickEvents.END_CLIENT_TICK.register(client -> {
    while (keyBinding.wasPressed()) {
	client.player.sendMessage(new LiteralText("Key 1 was pressed!"), false);
    }
});

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: 2020/08/21 10:22 by boogiemonster1o1