Jump to content

Mod Loader/Reference/Keybind API

From Threadbox

ModKeybindApi adds a remappable action under Mod Settings → Keybinds → Mod shortcuts. Available in Mod Loader 0.6.79 and later.

Register a key

Register the action in your plugin's Awake. Pass a stable mod ID, a local action ID, a name for the menu, and the default key. The full ID used to check input joins the mod and local IDs with a dot. Keep the returned handle and dispose of it when your plugin unloads.

using System;
using BepInEx;
using DressmakerMods;
using UnityEngine;

[BepInPlugin("example.shortcut", "Shortcut Example", "1.0.0")]
[BepInDependency("yuu.dressmaker.mods", "0.6.79")]
public sealed class ShortcutExample : BaseUnityPlugin
{
    private IDisposable keybind;

    private void Awake()
    {
        keybind = ModKeybindApi.Register("example.shortcut", "action", "Example action", KeyCode.V);
    }

    private void Update()
    {
        if (ModKeybindApi.WasPressed("example.shortcut.action"))
            Logger.LogInfo("Example action pressed.");
    }

    private void OnDestroy() => keybind?.Dispose();
}

IsHeld(id) checks whether the key is down. WasPressed(id) fires on the press, and WasReleased(id) fires on release. Pass an optional fifth KeyCode argument to Register if you want an alternate default key. All three checks return false while the player is changing a keybind, typing into a text field, or using the debug console.

The player's bindings are kept in the Mod Loader BepInEx config. A duplicate action ID is rejected. Disposing the handle removes the action from the menu; if the mod registers the same ID again later, the player's saved binding is still used.

To use a key while snapping accessories, register it here and check IsHeld in an Accessory Placement API callback.

Back to the Mod Loader guide