Skip to content

Clipboard

github-actions[bot] edited this page Oct 15, 2025 · 2 revisions

Perform copy and paste operations on the system clipboard.

Overview

The Electron.Clipboard API provides comprehensive access to the system clipboard, supporting multiple data formats including text, HTML, RTF, images, and custom data. It enables reading from and writing to the clipboard with platform-specific behavior.

Key Methods

Text Operations

ReadTextAsync(string)

Parameters:

  • type (optional) - Clipboard type

Returns: The content in the clipboard as plain text.

Description: Read the content in the clipboard as plain text.

WriteText(string, string)

Parameters:

  • text - Text content to write
  • type (optional) - Clipboard type

Description: Writes the text into the clipboard as plain text.

Rich Text Operations

ReadHTMLAsync(string)

Parameters:

  • type (optional) - Clipboard type

Returns: The content in the clipboard as markup.

Description: Read the content in the clipboard as HTML markup.

WriteHTML(string, string)

Parameters:

  • markup - HTML markup to write
  • type (optional) - Clipboard type

Description: Writes markup to the clipboard.

ReadRTFAsync(string)

Parameters:

  • type (optional) - Clipboard type

Returns: The content in the clipboard as RTF.

Description: Read the content in the clipboard as RTF.

WriteRTF(string, string)

Parameters:

  • text - RTF content to write
  • type (optional) - Clipboard type

Description: Writes the text into the clipboard in RTF.

Bookmark Operations

ReadBookmarkAsync()

Returns: Object containing title and url keys representing the bookmark in the clipboard.

Description: Returns an Object containing title and url keys representing the bookmark in the clipboard. The title and url values will be empty strings when the bookmark is unavailable.

WriteBookmark(string, string, string)

Parameters:

  • title - Bookmark title
  • url - Bookmark URL
  • type (optional) - Clipboard type

Description: Writes the title and url into the clipboard as a bookmark.

macOS-Specific Operations

ReadFindTextAsync()

Returns: The text on the find pasteboard.

Description: macOS: The text on the find pasteboard. This method uses synchronous IPC when called from the renderer process.

WriteFindText(string)

Parameters:

  • text - Text to write to find pasteboard

Description: macOS: Writes the text into the find pasteboard as plain text.

Clipboard Management

Clear(string)

Parameters:

  • type (optional) - Clipboard type

Description: Clears the clipboard content.

AvailableFormatsAsync(string)

Parameters:

  • type (optional) - Clipboard type

Returns: An array of supported formats for the clipboard type.

Description: Get an array of supported formats for the clipboard type.

Binary Data Operations

Write(Data, string)

Parameters:

  • data - Data object to write
  • type (optional) - Clipboard type

Description: Writes data to the clipboard.

ReadImageAsync(string)

Parameters:

  • type (optional) - Clipboard type

Returns: An image from the clipboard.

Description: Read an image from the clipboard.

WriteImage(NativeImage, string)

Parameters:

  • image - Image to write to clipboard
  • type (optional) - Clipboard type

Description: Writes an image to the clipboard.

Usage Examples

Basic Text Operations

// Read text from clipboard
var text = await Electron.Clipboard.ReadTextAsync();
Console.WriteLine($"Clipboard text: {text}");

// Write text to clipboard
Electron.Clipboard.WriteText("Hello, Electron.NET!");

// Read with specific type
var html = await Electron.Clipboard.ReadHTMLAsync("public.main");

Rich Content Handling

// Copy formatted text
var htmlContent = "<h1>Title</h1><p>Some <strong>bold</strong> text</p>";
Electron.Clipboard.WriteHTML(htmlContent);

// Read RTF content
var rtf = await Electron.Clipboard.ReadRTFAsync();
Console.WriteLine($"RTF content: {rtf}");

Image Operations

// Read image from clipboard
var image = await Electron.Clipboard.ReadImageAsync();
if (image != null)
{
    Console.WriteLine($"Image size: {image.Size.Width}x{image.Size.Height}");
}

// Write image to clipboard
var nativeImage = NativeImage.CreateFromPath("screenshot.png");
Electron.Clipboard.WriteImage(nativeImage);

Bookmark Management

// Read bookmark from clipboard
var bookmark = await Electron.Clipboard.ReadBookmarkAsync();
if (!string.IsNullOrEmpty(bookmark.Title))
{
    Console.WriteLine($"Bookmark: {bookmark.Title} -> {bookmark.Url}");
}

// Write bookmark to clipboard
Electron.Clipboard.WriteBookmark("Electron.NET", "https://github.com/ElectronNET/Electron.NET");

Advanced Clipboard Operations

// Check available formats
var formats = await Electron.Clipboard.AvailableFormatsAsync();
Console.WriteLine($"Available formats: {string.Join(", ", formats)}");

// Clear clipboard
Electron.Clipboard.Clear();

// Write custom data
var data = new Data
{
    Text = "Custom data",
    Html = "<p>Custom HTML</p>",
    Image = nativeImage
};
Electron.Clipboard.Write(data);

macOS Find Pasteboard

// macOS specific find pasteboard operations
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
    // Read find text
    var findText = await Electron.Clipboard.ReadFindTextAsync();
    Console.WriteLine($"Find text: {findText}");

    // Write find text
    Electron.Clipboard.WriteFindText("search term");
}

Related APIs

Additional Resources

Clone this wiki locally