Skip to content

Clipboard

Hermes provides a simple static API for reading and writing text to the system clipboard. It works across all platforms with no external dependencies — except Linux, which requires xclip.

Quick Reference

csharp
using Hermes;

// Copy text to clipboard
Clipboard.SetText("Hello from Hermes!");

// Read text from clipboard
string? text = Clipboard.GetText();

// Convenience wrappers on HermesApplication
HermesApplication.SetClipboardText("Hello!");
string? current = HermesApplication.GetClipboardText();

Setting Text

Clipboard.SetText(string text) copies the given string to the system clipboard.

csharp
Clipboard.SetText("Some text to copy");

The method validates its input — passing null, an empty string, or whitespace throws an ArgumentException. This prevents accidentally clearing the clipboard with blank content.

Getting Text

Clipboard.GetText() returns the current clipboard contents as a string?. If the clipboard is empty or doesn't contain text, it returns null.

csharp
var text = Clipboard.GetText();
if (text is not null)
{
    Console.WriteLine($"Clipboard contains: {text}");
}

Reading the clipboard never modifies its contents.

Platform Details

PlatformMechanismNotes
macOSpbcopy / pbpasteShips with macOS, no dependencies
WindowsWin32 Clipboard APIDirect API calls, fastest performance
LinuxxclipMust be installed separately

Linux requires xclip

On Linux, clipboard operations use xclip. If it isn't installed, Hermes throws a PlatformNotSupportedException with a message prompting you to install it:

bash
sudo apt install xclip

Error Handling

ExceptionCause
ArgumentExceptionSetText called with null, empty, or whitespace text
PlatformNotSupportedExceptionUnsupported platform, or xclip missing on Linux

Blazor Example

A simple copy-to-clipboard button in a Blazor component:

razor
@using Hermes

<button @onclick="CopyToClipboard">Copy Link</button>
<button @onclick="PasteFromClipboard">Paste</button>

<p>@pastedText</p>

@code {
    private string? pastedText;

    private void CopyToClipboard()
    {
        Clipboard.SetText("https://mythetech.com");
    }

    private void PasteFromClipboard()
    {
        pastedText = Clipboard.GetText();
    }
}

IClipboard Interface (Dependency Injection)

For Blazor applications, Hermes also provides an IClipboard interface that integrates with dependency injection. This is the preferred approach in Blazor components since it supports async operations and is easy to mock in tests.

csharp
public interface IClipboard
{
    Task SetTextAsync(string text, CancellationToken ct = default);
    Task<string?> GetTextAsync(CancellationToken ct = default);
}

IClipboard is automatically registered in the DI container when using HermesBlazorAppBuilder, so no additional setup is needed.

Blazor Example (DI)

razor
@inject IClipboard Clipboard

<button @onclick="CopyToClipboard">Copy Link</button>
<button @onclick="PasteFromClipboard">Paste</button>

<p>@pastedText</p>

@code {
    private string? pastedText;

    private async Task CopyToClipboard()
    {
        await Clipboard.SetTextAsync("https://mythetech.com");
    }

    private async Task PasteFromClipboard()
    {
        pastedText = await Clipboard.GetTextAsync();
    }
}

When to Use Which

ApproachWhen to use
IClipboard (DI)Blazor components, anywhere with DI access, testable code
Clipboard (static)Non-DI contexts, console apps, quick scripts

Next Steps