internal static void Register() // A method for registering all keybinds. Must be called during PreLoad. { KeybindRegistry.Register(new[] { Keybinds.Test, // Each keybind to register. Keybinds.Jest, Keybinds.Kest, Keybinds.Vest, Keybinds.Nest }); KeybindRegistry.Update += (_) => // Add an action to run every frame (when accepting input). { if (Keybinds.Test.Action?.WasRepeated ?? false) // `Keybind.Action` is nullable, because it isn't present before PreLoad. To be safe, use `?.` instead of `!.` or `.` in `KeybindRegistry.Update` handlers. { // You don't need to worry about `Keybind.Action` being null in the keybind's own events, as those will only be called if it's non-null. Console.Log("Test repeated!"); } }; }
public override void Load() { if (Keybinds.Test.Action !.IsPressed) // `Keybind.Action` will be null until after PreLoad. If you're not sure, use `?.` instead of `!.` to be safe. { // This code will never actually run, because I don't think the game starts listening for keybinds until after mods are loaded. Console.Log("Hello World from KeybindLib!"); } KeybindRegistry.RegisterKeyAction( // Add an action to run when a `PlayerAction` is activated. For `PlayerAction`s that don't have a corresponding `Keybind`, like vanilla ones. SRInput.Actions.jump, // You can only access `SRInput.Actions.jump` after the PreLoad step, therefore you can only call `KeybindRegistry.RegisterKeyAction` after the PreLoad step. (player) => // They `KeyAction` to run. { Console.Log("Thanks for jumping!"); player.Run.TryStart(); }, keyReleased: true // Make it activate when the key is released instead of pressed. ); // ... }