---
title: "Navigate a game menu with a controller in C#"
url: https://recursiveai.org/onegame/cookbook/navigate-game-menu-with-controller/
description: "Drive onegame menu focus with controller navigation, confirm and back events. Skip disabled buttons, trap focus in a modal and test without hardware."
version: "2.0.0"
---

# Navigate a game menu with a controller in C#

**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.UI.Kit) · [GitHub](https://github.com/recursive-ai-oss/onegame) · [Docs](https://recursiveai.org/onegame/docs/) · [Download example (.zip)](https://recursiveai.org/onegame/cookbook/navigate-game-menu-with-controller/navigate-game-menu-with-controller.zip)

Move menu focus and confirm a choice without a mouse. This headless example needs no controller hardware.

```shell
dotnet add package RecursiveAI.OneGame.UI.Kit
```

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

```csharp
using OneGame.UI;
using OneGame.UI.Kit;

var ui = new UiTree();
ui.SetViewport(640, 360);
var play = UiKit.Button("Play",
    () => Console.WriteLine("Play"));
var options = UiKit.Button("Options",
    () => Console.WriteLine("Options"));
ui.Root.Add(Ui.Column(play, options));
ui.Frame();
ui.Focus(play);
ui.Dispatch(UiEvent.Navigate(
    NavDirection.Next));
ui.Dispatch(UiEvent.Activate());
```

```text
Options
```

Prints `Options`: navigate from Play to Options, then activate it.

Unzip [the example](https://recursiveai.org/onegame/cookbook/navigate-game-menu-with-controller/navigate-game-menu-with-controller.zip), then `dotnet run`.

## The full program

The full program tests disabled-item traversal, confirm, a modal focus trap and back navigation in a headless UI tree. It does not open a window or test a physical controller. The modal’s Cancel handler removes it and restores focus to Options; a focus trap alone does not implement that close action.

A focus request for a disabled or hidden control returns `false`; those controls are skipped during navigation.

```csharp
using OneGame.UI;
using OneGame.UI.Kit;

var ui = new UiTree();
ui.SetViewport(640, 360);
var host = new KitHost(ui);
var start = UiKit.Button("Start", () => Console.WriteLine("Start activated"), "start");
var unavailable = UiKit.Button("Continue", () => { }, "continue");
unavailable.IsEnabled = false;
var options = UiKit.Button("Options", () => { }, "options");
ui.Root.Add(Ui.Column(start, unavailable, options));
Frame();
ui.Focus(start);
Console.WriteLine($"Initial: {ui.Focused?.Id}");
host.Dispatch(UiEvent.Navigate(NavDirection.Down));
Console.WriteLine($"Down skips disabled: {ui.Focused?.Id}");

var yes = UiKit.Button("Yes", () => Console.WriteLine("Confirmed"), "yes");
var no = UiKit.Button("No", () => { }, "no");
var modal = Ui.Column(yes, no).TrapFocus();
modal.On(UiEventKind.Cancel, (_, _) =>
{
    modal.RemoveFromParent();
    ui.Focus(options);
    return true;
});
ui.Root.Add(modal);
Frame();
ui.Focus(yes);
host.Dispatch(UiEvent.Activate());
host.Dispatch(UiEvent.Navigate(NavDirection.Next));
Console.WriteLine($"Modal next: {ui.Focused?.Id}");
host.Dispatch(UiEvent.Navigate(NavDirection.Next));
Console.WriteLine($"Modal wraps: {ui.Focused?.Id}");
host.Dispatch(UiEvent.Cancel());
Console.WriteLine($"After cancel: {ui.Focused?.Id}");

void Frame()
{
    for (var i = 0; i < 4; i++)
    {
        host.Advance(0);
        ui.Frame();
    }
}
```

`dotnet run` prints:

```text
Initial: start
Down skips disabled: options
Confirmed
Modal next: no
Modal wraps: yes
After cancel: options
```

## Variations

After the snippet at the top, mark Play as the default and focus it when the screen opens. This prints `True`.

```csharp
play.AsDefaultFocus();
FocusHelpers.FocusDefault(ui, ui.Root);
Console.WriteLine(ReferenceEquals(ui.Focused, play));
```

## Related recipes

- [Build a game UI screen](https://recursiveai.org/onegame/cookbook/game-ui-screen/)
- [Declare gameplay input actions](https://recursiveai.org/onegame/cookbook/input-actions/)

## The short answer

To navigate a game menu with a controller in C#, give a onegame UI control initial focus, send `UiEvent.Navigate` for navigation steps, `Activate` for confirm and `Cancel` for back. The UI owns one focused element, available as `ui.Focused`. Disabled and hidden controls are skipped. Wrap modal content in a focus trap so navigation stays inside it until the modal closes.

The example uses the released UI kit directly. It tests event routing and focus without a game window or physical gamepad. The [UI guide](https://recursiveai.org/onegame/docs/ui-guide/#9-focus-keyboard-and-controller-navigation) documents how the game host maps controller buttons onto these same events.

## How do I navigate a game menu with a gamepad?

Build the menu tree and run layout before testing directional movement. Directional navigation uses element positions: Up, Down, Left and Right seek the nearest suitable focusable control in that direction, preferring controls beyond the current one and lined up with it. Next and Previous provide sequential movement, as shoulder buttons do in the host.

Set an initial focus when opening each screen. Without it, a confirm press has no selected action to click. `FocusHelpers.FocusDefault` uses the control marked with `AsDefaultFocus`, or the first focusable element otherwise. The full example sets Start directly so the test has an explicit starting point.

In the host, South confirms, East goes back, and d-pad and shoulder buttons navigate. The headless program calls those event factories itself; it does not poll a controller. A custom input adapter should produce navigation steps rather than dispatching a new step on every frame that a stick remains held.

## Why is gamepad menu navigation not working?

Check `ui.Focused` first. A control can be visible but not focusable, or it can be disabled through a parent. `ui.Focus(element)` reports whether the request succeeded. Hidden and disabled elements do not take focus, which is why the example moves from Start directly to Options past Continue.

Next check whether a component consumed the navigation event. Sliders use Left and Right to change their value. Text fields use arrow keys for editing. A list uses Up and Down to change the selected row, then lets unused steps leave at its boundary. That behavior is intentional; repeatedly forcing focus to another button can break the control’s own interaction.

Keep focus ownership in the UI tree, rather than maintaining a separate selected-button index that disagrees with it. A click from confirm is delivered to the focused element. The same focus state also determines where back events begin bubbling.

## How do I keep controller focus inside a modal menu?

Call `TrapFocus()` on the modal container. The example’s Yes and No buttons form a small trap: Next moves to No, then wraps to Yes instead of reaching Options behind it. The printed focused ids show the containment directly.

Handle Cancel on the modal to close it. The example removes the container and restores focus to Options before returning `true` to mark the event handled. Closing and restoring focus belong together: leaving focus on a removed control gives the next confirm press no useful destination.

Built-in dialogs and drop-downs already provide focus trapping. Use them when their behavior fits. For a custom modal, test open, confirm, back and return focus as one sequence, including disabled controls. The deterministic event sequence above covers those transitions without requiring a particular controller model.
