---
title: "Handle a keyboard event in a C# HTML component"
url: https://recursiveai.org/oneui/cookbook/handle-keyboard-event-in-ui-component/
description: "Handle Enter with a typed KeyboardEvent in OneUI. Bind on:keydown in HTML, read the key in C#, and run the sample without opening a window."
version: "1.1.0"
---

# Handle a keyboard event in a C# HTML component

**OneUI** is a free, open-source .NET library from Recursive AI for writing UI components in HTML, CSS, and C# that run in native windows on desktops and phones, or in a browser via WebAssembly, with OneApp. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneUI) · [GitHub](https://github.com/recursive-ai-oss/oneui-csharp) · [Docs](https://recursiveai.org/oneui/docs/) · [Download example (.zip)](https://recursiveai.org/oneui/cookbook/handle-keyboard-event-in-ui-component/handle-keyboard-event-in-ui-component.zip)

Bind `on:keydown` to a `KeyboardEvent` handler and check `e.Key` for Enter.

```shell
dotnet add package RecursiveAI.OneUI
```

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

```html
<input id="keys" aria-label="Key demo"
       on:keydown="ReadKey" />
```

In `KeyBox.cs`:

```csharp
using OneUI.Components;

public partial class KeyBox
{
    void ReadKey(KeyboardEvent e)
    {
        if (e.Key == "Enter")
            Console.WriteLine("Enter pressed");
    }
}
```

[The example](https://recursiveai.org/oneui/cookbook/handle-keyboard-event-in-ui-component/handle-keyboard-event-in-ui-component.zip) is the full program below: unzip it and `dotnet run`.

The handler prints `Enter pressed`; it ignores Escape.

## The full program

OneUI’s source generator pairs the `.html` file with the partial class of the same name. The example uses this package reference:

```xml
<PackageReference
    Include="RecursiveAI.OneUI"
    Version="1.1.0" />
```

Save the input markup as `KeyBox.html` beside `KeyBox.cs`.

`Demo.cs` mounts `KeyBox` in a `ReferenceDocument`, an in-memory HTML tree, and sends Escape and Enter to the input. No window opens.

```csharp
using OneUI.Components;
using OneUI.Components.Reference;

var doc = new ReferenceDocument();
var ui = new UiContext(doc);
doc.Context = ui;
ui.Mount(new KeyBox());

Console.WriteLine("Sending Escape");
doc.Press("#keys", "Escape");
Console.WriteLine("Sending Enter");
doc.Press("#keys", "Enter");
```

`dotnet run` prints:

```text
Sending Escape
Sending Enter
Enter pressed
```

If the handler takes the wrong event type or doesn’t return `void`, the template compiler reports `OUI0022`.

## Variations

To react when the key is **released**, replace `KeyBox.html` with the following. Keep the same C# handler.

```html
<input id="keys" aria-label="Key demo"
       on:keyup="ReadKey" />
```

## Related recipes

- [Focus an input after a C# component updates](https://recursiveai.org/oneui/cookbook/focus-input-after-component-update/)
- [Bind an input value to C# state](https://recursiveai.org/oneui/cookbook/two-way-bind-input-value/)

## The short answer

Set `on:keydown="ReadKey"` on the input and write `void ReadKey(KeyboardEvent e)` in the matching C# partial class. Use `e.Key` to distinguish Enter from Escape or other keys. The generator connects the template to the handler at build time; no JavaScript listener is needed in the component.

## What is a keydown event?

[`keydown`](https://developer.mozilla.org/en-US/docs/Web/API/Element/keydown_event) fires when a key is pressed, including keys that don’t enter text. `keyup` fires when it’s released. Both directives deliver a `KeyboardEvent` in OneUI. Use keydown to act as soon as a key is pressed, and keyup when the action belongs on release. Pairing both with the same action would make one press trigger it twice. For text entered into a field, use `bind:value` instead: it listens for `input`, so the value can also change without a key press.

## How do you handle a specific key in HTML and C#?

Use the exact `Key` value for an action such as Enter or Escape. `Key` describes the meaning of the press, while `Code` identifies the physical key. Use `Key` for a command like Enter, rather than tying the action to one location on the keyboard. The payload also has `Modifiers` for keys held down with it and `Repeat` to distinguish an auto-repeating event from the first press.

In an interactive host, focus the input before pressing Enter: the keyboard event starts at the focused element. This console example passes `"#keys"` to `Press` to target the input directly. It demonstrates the handler and key filter, not browser or desktop keyboard routing. See [the event payloads](https://recursiveai.org/oneui/docs/markup/#template-syntax) and [the reference document’s key helpers](https://recursiveai.org/oneui/docs/change-sets/).
