Adding Custom Keybinds

One of the necessary things for mods, keybinds. And in this tutorial we’ll learn to make just that!

Let’s get started! First, you’ll need a new class specially designated for all of your mod’s keybinds. (Or you don’t, it’s up to you, but it’s good practice to keep different functions in seperate classes). Anyway, I’ll call this class Keybinds.

public class Keybinds
{
    public static KeyBinding hello;

    public static void register()
    {
        hello = new KeyBinding("key.hello", Keyboard.KEY_V, "key.categories.tutorial");

        ClientRegistry.registerKeyBinding(hello);
    }
}

In the class, you’ll need to make a static method, so you can call this method from your main class to register the keybinds.

Now, you’re going to add the actual keybind objects, in my case the hello keybind. Initialize this in the register class with the following constructor:

new KeyBinding(String name, int key, String category);

The first parameter is the unlocalized name of the key, which we’ll register soon in our language file. The second parameter is an integer referring to the key to press. You don’t need to use raw integers for this, though, you can use LWJGL, Keyboard class to simplify your life. Lastly, you have your category. You can use your own string here which you need to register in the language file, or you can simply use one of six available categories from vanilla, which are:

  • Movement – key.categories.movement
  • Inventory – key.categories.inventory
  • Gameplay – key.categories.gameplay
  • Multiplayer – key.categories.multiplayer
  • Misc – key.categories.misc

When you’re done, remember to register the new entries to en_US.lang!

key.hello=Speak hello to yourself
key.categories.tutorial=Tutorial

And that’s right, I’ll make a keybind to speak hello to yourself. Don’t judge.

Back to coding, let’s simply put a line of code to register the keybinds you just created.

public static void preInit(FMLPreInitializationEvent event)
{
    Keybinds.register();
}

Next thing to do is create a way to check if the keybind is pressed. So, we’ll use a listener provided by Forge, specifically the KeyInputEvent listener.

Create a new class to handle this listener:

public class KeyInputHandler
{
    @SubscribeEvent
    public void onKeyInput(KeyInputEvent event)
    {
        if (Keybinds.hello.isPressed())
        {
            Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("Hello!"));
        }
    }
}

Use an if-check to check if the keybind is pressed with isPressed() from the keybind object, and then add what you’re going to do. Like I said, I added a code to show “Hello!” in chat to myself (client sided).

Note that you don’t necessarily need to use KeyInputEvent. You can place this piece of code anywhere, for instance in onUpdate() so it only triggers when the item is in a player’s inventory.

Lastly, register this in your main mod class.

public static void preInit(FMLPreInitializationEvent event)
{
    Keybinds.register();

    MinecraftForge.EVENT_BUS.register(new KeyInputHandler());
}

That’s it, you’re done!

Minecraft 1.7.10 1_10_2016 6_37_39 PM