Create Keybinding in Settings - piotrulos/MSCModLoader GitHub Wiki

[!IMPORTANT] Below example shows newest keybind format creation updated in version 1.3.3 build 355, old have been obsoleted but are still backwards compatible in existing mods

Keybinding

You can add Keybind Settings that allows you to have rebindable keybinds for custom actions.

Just like Mod Settings creating custom keybinds is done inside "ModSettings" function.

public class MyMod1 : Mod
{
    public override string ID => "MyMod1"; 
    public override string Name => "Testing";
    public override string Author => "piotrulos";
    public override string Version => "1.0";
    public override string Description => "";
    
    public override void ModSetup()
    {       
        //(...)
        SetupFunction(Setup.ModSettings, Mod_Settings);
    }
    private void Mod_Settings()
    {
        // Creating custom keybinds should be here
    } 
}

Example usage

public (static) SettingsKeybind keybind1, keybind2;
private void Mod_Settings()
{
    Keybind.AddHeader("Example Keybinds");    
    keybind1 = Keybind.Add("ex1", "Signle default keybind test", KeyCode.F1);
    keybind2 = Keybind.Add("ex2", "Keybind and modifier test", KeyCode.F2, KeyCode.LeftControl);
    // (...)
    // Rest of regular settings
} 

//Update can be Update from custom monobehaviours
private void Update()
{
    if(keybind1.GetKeybindDown())
    {
        ModConsole.Warning("Pressing just single key is required for this to show");
    }
    if (keybind2.GetKeybindDown())
    {
        ModConsole.Warning("Pressing both keys is required for this to show");
    }
}

This example is easy to follow:

  • Create public SettingsKeybind variable (can be static if you want to access it from other classes or you can just pass instance)
  • Inside your Mod_Settings function register your keybinds by using Keybind.Add
  • Access your rebindable keybind in any Update function.
    • GetKeybindDown() works exactly same as unity's GetKeyDown()
    • GetKeybindUp() works exactly same as unity's GetKeyUp()
    • GetKeybind() works exactly same as unity's GetKey()
  • Rebindable keys shows in separate settings menu
    keybinds