Keybinds - PULSAR-Modders/pulsar-mod-loader GitHub Wiki

Registration

Keybinds can be added through the interface located at PulsarModLoader.Keybinds.IKeybind, as an extension to the PulsarMod class. These keybinds are added to the game's controls interface and can be customized by users.

To register your keybind, add the IKeybind interface to your PulsarMod class. Add the RegisterBinds(KeybindManager) interface, then register using the KeybindManager's NewBind(inName, inID, inCategory, inKey) method.

NewBind() Arguments:

  • inName - The display name for the keybind, visible when users bind the key.
  • inID - The internal ID for the keybind.
  • inCategory - The category in the keybinds customizer under which your bind will appear. 'Basics' is the recommended value.
  • inKey - The default key for the bind.
using PulsarModLoader;
using PulsarModLoader.Keybinds;

namespace MyNamespace
{
    public class Mod : PulsarMod, IKeybind
    {
        public void RegisterBinds(KeybindManager manager)
        {
            manager.NewBind("MyKeybind", "mykeybind", "Basics", "k");
        }
    }
}

Reading your Keybind

The KeybindManager has a built in function for getting the pressed state of your key, bool KeybindManager.GetButtonDown(inID), which can be read using KeybindManager.Instance.GetButtonDown("myKeybindID"). This will return a bool representing whether the key has been pressed. It only counts single presses and will only fire once while the key is held down. Alternatively you can use PLInput.Instance.GetButtonDown(inID), which might be slightly faster due to being called by the KeybindManager. A variety of other ways for reading can be found under PLInput.

Example

Generally you want your keybind read on update methods. A good place to start is with a patch on the PLInGameUI Update method, as it is only read while the game is open. The methods used in this example throw up a notification as well as a new line into in-game chat.

[HarmonyLib.HarmonyPatch(typeof(PLInGameUI), "Update")]
class patchclass
{
    static void Postfix()
    {
        if (PulsarModLoader.Keybinds.KeybindManager.Instance.GetButtonDown("keybindtest"))
        {
            PulsarModLoader.Utilities.Messaging.Echo(PLNetworkManager.Instance.LocalPlayer, "Test button pressed.");
            PulsarModLoader.Utilities.Messaging.Notification("Test button pressed.");
        }
    }
}