Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneUI

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

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

In KeyBox.cs:

C#
using OneUI.Components;
 
public partial class KeyBox
{
    void ReadKey(KeyboardEvent e)
    {
        if (e.Key == "Enter")
            Console.WriteLine("Enter pressed");
    }
}

The example 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.

C#
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" />

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 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 and the reference document’s key helpers.

All oneui recipes