---
title: "Change and save keyboard bindings in a C# game"
url: https://recursiveai.org/onegame/cookbook/rebind-and-save-game-controls/
description: "Change and save keyboard bindings in a C# game with OneGame. Rebind Jump from Space to J and reload the saved override into a fresh action map."
version: "2.7.0"
---

# Change and save keyboard bindings in a C# game

**onegame** is a free, open-source game engine for .NET from Recursive AI. MIT or Apache-2.0, no editor required.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneGame.Host) · [GitHub](https://github.com/recursive-ai-oss/onegame) · [Docs](https://recursiveai.org/onegame/docs/) · [Download example (.zip)](https://recursiveai.org/onegame/cookbook/rebind-and-save-game-controls/rebind-and-save-game-controls.zip)

Change Jump from Space to J.

```shell
dotnet add package RecursiveAI.OneGame.Host
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneGame.Platform;

var actions = new ActionMap()
    .Context("play", c =>
        c.Button("jump", Bind.Key(Key.Space)));
actions.SetBinding("jump", 0, Bind.Key(Key.J));
const string path = "example-bindings.txt";
actions.Save(path);
Console.Write(File.ReadAllText(path));
```

```text
onegame-bindings 1
jump = key:J
```

Unzip [the example](https://recursiveai.org/onegame/cookbook/rebind-and-save-game-controls/rebind-and-save-game-controls.zip), then `dotnet run`.

## The full program

Use `BeginRebind` to wait for the player’s next input instead of specifying a key in code. The program injects a virtual J press, saves the override and loads it into a fresh action map with the original defaults. It then presses Space and J separately to show which triggers Jump. No window or physical keyboard input is needed. `Frame` stands in for the game loop and passes a virtual window width and height of `200, 100` pixels to `Update` for touch regions; OneGame’s host handles that update, so your game only calls `BeginRebind`, `Save`, and `Load` to change and persist bindings.

```csharp
using OneGame.Platform;

const string path = "example-bindings.txt";
using var app = PlatformApp.Create(new PlatformOptions
{
    Backend = PlatformBackendKind.Headless,
    EnableGamepadDevices = false
});
var window = app.CreateWindow().Id;
var actions = Defaults();
actions.BeginRebind("jump");
Frame(actions, Key.J, true);
Frame(actions, Key.J, false);
actions.Save(path);
Console.Write(File.ReadAllText(path));

var restarted = Defaults();
var load = restarted.Load(path);
if (load.Problems.Count != 0)
    throw new InvalidOperationException(string.Join("; ", load.Problems));
Console.WriteLine($"Overrides loaded: {load.Applied}");
Frame(restarted, Key.Space, true);
Console.WriteLine($"Space triggers Jump: {restarted.Pressed("jump")}");
Frame(restarted, Key.Space, false);
Frame(restarted, Key.J, true);
Console.WriteLine($"J triggers Jump: {restarted.Pressed("jump")}");
Frame(restarted, Key.J, false);
restarted.ResetBindings();
restarted.Save(path);
Console.WriteLine($"Overrides after reset: {Defaults().Load(path).Applied}");

void Frame(ActionMap map, Key key, bool down)
{
    app.Injector.Key(window, key, down);
    app.PumpEvents();
    while (app.TryGetEvent(out var e))
        map.Observe(e);
    map.Update(app.Input, 200, 100);
    app.Input.EndFrame();
}

static ActionMap Defaults() => new ActionMap().Context("play", c => c
    .Button("jump", Bind.Key(Key.Space), Bind.Pad(GamepadButton.South))
    .Button("pause", Bind.Key(Key.Escape)));
```

```text
onegame-bindings 1
jump = key:J | pad:South
Overrides loaded: 1
Space triggers Jump: False
J triggers Jump: True
Overrides after reset: 0
```

A bad binding line is skipped and returned in `Load(...).Problems`; the other valid overrides still apply.

## Variations

### Keep the default gamepad button

With Space and the south gamepad button in separate slots, `slot: 0` changes only the keyboard binding and leaves `pad:South` alone.

```csharp
// On the action map, with Jump declared:
actions.BeginRebind("jump", slot: 0);
```

### Reset saved controls

Reset the action map and save to the same path to remove saved overrides. Calling `ResetBindings` alone doesn’t update the file.

```csharp
actions.ResetBindings();
actions.Save(path);
```

## Related recipes

- [Input actions in C# for keyboard and gamepad](https://recursiveai.org/onegame/cookbook/input-actions/)
- [Save slots and settings](https://recursiveai.org/onegame/cookbook/save-slots-and-settings/)

---

## The short answer

Declare defaults in the `ActionMap`. In your settings screen, call `BeginRebind("jump")` and let the player press a new key, then call `Save` after the change. At startup, declare the defaults again and call `Load` with the same path. Gameplay still reads `Pressed("jump")`; it never has to check which key the player picked.

## How do I customize keybinds while the game is running?

Use `BeginRebind` when the player picks a control to change. It waits for the next eligible key, button or stick movement; Escape cancels it. The slot index selects which binding to change. A keyboard slot alongside a gamepad slot lets a player change the key for Jump without losing the controller button. Keep the name `jump` stable so that the gameplay code and saved settings refer to the same action. See the [input guide](https://recursiveai.org/onegame/docs/input/) for the stock binding list UI and support for input contexts.

## How do I save custom keybinds between sessions?

Save when the player leaves settings. For a shipped game, `ActionMap.DefaultPath` gives you a per-user file named `bindings.txt` in a folder for your game under local application data. Use the same game name and path when loading at startup; OneGame does not load the file automatically. A missing file leaves a fresh action map at its defaults.

The file has one line per changed action, not one line per changed slot. In the example, the `jump` line includes the unchanged gamepad button because it describes Jump’s complete current binding list. There’s no `pause` line because Pause hasn’t changed. This lets players without a Pause override pick up a new Pause default you ship later, but changing the defaults for Jump won’t replace an existing Jump override.

## How do I reset keybindings to their defaults?

Reset the in-memory action map with `ResetBindings`, then save again. If no bindings differ from the defaults, the saved file contains only the format header. On the next launch, loading that file applies no overrides. This is distinct from canceling an in-progress rebind with Escape: canceling leaves the binding that was already in use, even if it was customized.
