dotnet add package RecursiveAI.OnePlatformRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePlatform; using var app = PlatformApp.Create(new() { Backend = PlatformBackendKind.Headless }); var id = app.CreateWindow().Id; app.Injector.PointerMove(id, 40, 25); app.Injector.PointerButton(id, MouseButton.Left, true); app.Injector.PointerButton(id, MouseButton.Left, false); app.Injector.KeyPress(id, Key.A); while (app.TryGetEvent(out var evt)) { if (evt.Kind == PlatformEventKind.PointerButtonDown) Console.WriteLine($"Click: {evt.X}, {evt.Y}"); if (evt.Kind == PlatformEventKind.KeyDown) Console.WriteLine($"Key: {evt.Key}"); }
The pointer coordinates are window pixels. Move first: the button event takes its position from the last pointer move. With no real display, nothing appears on screen; the output comes from the same event queue your application reads. If your test expects a typed character rather than a physical key, use Text; KeyPress queues key down and up, not a text event.
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
The click lands at (40, 25), and the event queue reports the A key:
Click: 40, 25 Key: A
Variations
To simulate typing instead of the A key, replace KeyPress with Text, and handle the TextInput event in the loop instead of KeyDown:
app.Injector.Text(id, "café");if (evt.Kind == PlatformEventKind.TextInput) Console.WriteLine(evt.Text);
Assert a key press in xUnit. Run these commands in a new directory to create a .NET 10 test project:
dotnet new classlib -f net10.0 dotnet add package RecursiveAI.OnePlatform dotnet add package Microsoft.NET.Test.Sdk dotnet add package xunit dotnet add package xunit.runner.visualstudio
Save this as WindowInputTests.cs and run dotnet test:
using OnePlatform; using Xunit; public class WindowInputTests { [Fact] public void KeyPressQueuesKeyDownThenKeyUp() { using var app = PlatformApp.Create(new() { Backend = PlatformBackendKind.Headless }); var id = app.CreateWindow().Id; app.Injector.KeyPress(id, Key.A); Assert.True(app.TryGetEvent(out var down)); Assert.Equal(PlatformEventKind.KeyDown, down.Kind); Assert.Equal(id, down.WindowId); Assert.Equal(Key.A, down.Key); Assert.True(app.TryGetEvent(out var up)); Assert.Equal(PlatformEventKind.KeyUp, up.Kind); } }
Related recipes
The short answer
Use PlatformBackendKind.Headless to create an in-process OnePlatform window. Pass its ID to app.Injector to move the pointer, press a mouse button, or send a keyboard event. Drain TryGetEvent and feed the events to the same code your app uses on the desktop. In a test, assert what that code did with the input, for example that a click selected the expected item.
Can C# tests simulate input without moving the real mouse?
Yes, for code reading events from your OnePlatform app. The headless backend creates no operating-system window; InputInjector posts to the app’s queue and updates its held-key and button state. It doesn’t send keystrokes to another application or require desktop focus, so the test isn’t targeting whichever window happens to be on top. It also doesn’t check how a real desktop maps hardware input, changes focus, or routes an event to your app. Use a native window for those checks.
An injector call has already posted its event when it returns; the example doesn’t need a native event pump before it drains the queue. When testing an app that already has a pump, drive the app through its normal handler so you test the response, not just the queue. With multiple headless windows, route events by WindowId the same way you do on the desktop.
What’s the difference between simulating a key press and typing text?
Test shortcuts and navigation with physical key events. KeyPress posts a down and an up together. If your application polls whether a key is held, inject the down and up in separate steps, with your app’s update between them. After both events have been posted, the live state already shows the key released even if your handler has not yet read the key-down event.
Test text entry with Text. A physical key isn’t a character: typed text depends on layout and modifier state, and can also come from an input method that composes characters before committing text. The snippet in Variations inserts the string directly as a TextInput event, without assuming a keyboard layout.