Read and write clipboard text on the thread that runs your desktop app; this API doesn’t handle images or files. OnePlatform supports the system clipboard on Windows, macOS, and Linux (X11 and Wayland); the snippet works unchanged on Windows, macOS, and X11, while Wayland needs a key press or click.
dotnet add package RecursiveAI.OnePlatformRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePlatform; using var app = PlatformApp.Create(); var clipboard = app.Clipboard; if (!clipboard.SetText("Café 世界")) throw new InvalidOperationException("Copy failed."); Console.WriteLine(clipboard.GetText() ?? "No text");
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
The snippet reads back its own write and then exits. Use the download to paste between apps or to copy on Wayland in response to a key press. On X11, the snippet printed:
Café 世界Full program
using OnePlatform; using var app = PlatformApp.Create(); if (app.Backend == PlatformBackendKind.Headless) throw new InvalidOperationException("No desktop session found."); app.CreateWindow(new() { Title = "Clipboard demo" }); Console.WriteLine("Focus the window. C copies; V pastes."); while (true) { app.PumpEvents(100); while (app.TryGetEvent(out var evt)) { if (evt.Kind is PlatformEventKind.WindowCloseRequested or PlatformEventKind.QuitRequested) return; if (evt.Kind != PlatformEventKind.KeyDown || evt.IsRepeat) continue; var clipboard = app.Clipboard; if (evt.Key == Key.C) Console.WriteLine(clipboard.SetText("Café 世界") ? "Copied" : "Copy failed"); if (evt.Key == Key.V) Console.WriteLine(clipboard.GetText() ?? "No text"); } }
Focus Clipboard demo, press C to copy Café 世界, and switch to another app to paste it while the demo stays open. Copy Déjà vu from that other app, return to Clipboard demo, and press V to print what you copied. Close the window to quit.
Focus the window. C copies; V pastes. Copied Déjà vu
If SetText returns false, the write didn’t succeed. GetText returns null for absent, non-text, or oversized content, or when the clipboard owner doesn’t answer in time; the example prints No text.
Variations
On X11, read highlighted text (the middle-click selection) instead of the ordinary copy/paste clipboard. Replace clipboard.GetText() in the paste handler with:
clipboard.GetText(ClipboardSelection.Primary)Related recipes
The short answer
For cross-platform clipboard text in C#, call app.Clipboard.SetText(text) to copy and app.Clipboard.GetText() to paste. Call both synchronously on the thread that runs PlatformApp. Keep pumping the desktop event loop after copying so the backend can serve paste requests. A headless app has a private clipboard, so a successful read in a headless test doesn’t mean text reached the system clipboard.
Why can’t another app paste the text I copied?
On X11 the source keeps ownership of the selection and serves the text when another app asks for it. If the owner quits or stops pumping events, don’t count on the text remaining available. A clipboard manager can affect what persists. The snippet at the top is only a quick read-back; the full program leaves the owning app running.
On Wayland, copy only in response to a key press or click in the focused window, not at startup. Some Wayland desktops don’t offer clipboard access to the app; check app.Clipboard.IsAvailable before offering clipboard actions, and still check the result of a write.
What happens when the clipboard doesn’t contain text?
GetText returns null; it doesn’t turn an image, file list, or other non-text payload into a string. Treat null differently from an empty string. The API is for UTF-8 text, with a limit of 4 MiB measured in UTF-8 bytes, not the C# string’s character count. When another program owns the clipboard, a synchronous read may wait for that owner to answer, so read in response to a paste rather than polling every frame.
Use the default selection for normal copy and paste. Use ClipboardSelection.Primary only when integrating with X11’s separate selection for highlighted text. Reading the primary selection doesn’t tell you what the user last copied into the ordinary clipboard.