> For the complete documentation index, see [llms.txt](https://walksy.gitbook.io/walksylib-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://walksy.gitbook.io/walksylib-docs/keybinds/creating-a-keybind.md).

# Creating a Keybind

```java
import com.mojang.blaze3d.platform.InputConstants;
import main.walksy.lib.core.config.value.WLKeyMapping;
import org.lwjgl.glfw.GLFW;

public final class MyMod implements ModInitializer {
    public static final WLKeyMapping TOGGLE = WLKeyMapping.create("Toggle", GLFW.GLFW_KEY_R, "My Mod");
    public static final WLKeyMapping ZOOM = WLKeyMapping.create("Zoom", InputConstants.UNKNOWN.getValue(), "My Mod");
}
```

The arguments are **name**, **default key** and **category**. Use `InputConstants.UNKNOWN.getValue()` for a keybind that starts unbound.

## Reading a keybind

```java
WalksyLibEvents.END_CLIENT_TICK.register(minecraft -> {
    while (MyMod.TOGGLE.consumeClick()) {
        enabled = !enabled;
    }
    zooming = MyMod.ZOOM.isDown();
});
```

| Method                | Returns                                                                            |
| --------------------- | ---------------------------------------------------------------------------------- |
| `consumeClick()`      | `true` once per press. Use it for toggles and actions.                             |
| `isDown()`            | `true` while the key is held, as tracked by Minecraft's key handling.              |
| `isHeld()`            | `true` while the physical key or mouse button is held, even when a screen is open. |
| `isUnbound()`         | `true` if the user unbound it.                                                     |
| `displayKeyMessage()` | The bound key's display name, e.g. "R".                                            |

## Showing a keybind in your config

To let users rebind it from your own config screen, add it as an option:

```java
.option(Options.ofKeybind("Toggle", MyMod.TOGGLE).build())
```

Every keybind also appears on WalksyLib's [Key Binds page](/walksylib-docs/keybinds/keybinds-page.md) automatically, whether or not you add it to your config.

## Rebinding from code

```java
MyMod.TOGGLE.rebind(InputConstants.Type.KEYSYM.getOrCreate(GLFW.GLFW_KEY_G));
MyMod.TOGGLE.resetToDefault();
```

## Removing a keybind

```java
MyMod.TOGGLE.unregister();
```

This is useful for keybinds created at runtime, such as one per user-created page.
