Skip to content

clipboard

stable

System clipboard plugin providing a Clipboard class to read and write text and images, check clipboard contents, and clear the clipboard across Windows, macOS, and Linux.

use plugin clipboard::{Clipboard.new, get_text, set_text, …}
8 functions Systems
/ filter jk navigate Esc clear
Functions (8)
  1. Clipboard.new Open a handle to the system clipboard
  2. get_text Read the current clipboard text
  3. set_text Write a string to the clipboard
  4. get_image Read the current clipboard image as RGBA pixels
  5. set_image Write an RGBA image to the clipboard
  6. has_text Check whether the clipboard holds non-empty text
  7. has_image Check whether the clipboard holds an image
  8. clear Clear all clipboard contents

Open a handle to the system clipboard

Opens a connection to the system clipboard and returns a Clipboard handle. Errors if the clipboard cannot be accessed (for example, on a headless Linux session without a display server).

use plugin clipboard::{Clipboard}

let cb = Clipboard.new()
cb.set_text("hello from Zolo")
print(cb.get_text())

Read the current clipboard text

Returns the text currently stored on the clipboard. Errors if the clipboard is empty or holds non-text content.

use plugin clipboard::{Clipboard}

let cb = Clipboard.new()
if cb.has_text() {
  let text = cb.get_text()
  print("clipboard says: {text}")
}

Write a string to the clipboard

Replaces the clipboard contents with the given string.

use plugin clipboard::{Clipboard}

let cb = Clipboard.new()
cb.set_text("copied at runtime")
print(cb.get_text())  // "copied at runtime"

Read the current clipboard image as RGBA pixels

Returns the image currently stored on the clipboard as a table with width and height integers and bytes containing the raw RGBA pixel data (width * height * 4 bytes). Errors if the clipboard does not hold an image.

use plugin clipboard::{Clipboard}

let cb = Clipboard.new()
if cb.has_image() {
  let img = cb.get_image()
  print("image is {img["width"]}x{img["height"]}")
}

Write an RGBA image to the clipboard

Places an image on the clipboard. bytes must be raw RGBA pixel data with exactly width * height * 4 bytes.

use plugin clipboard::{Clipboard}

let cb = Clipboard.new()
// a 1x1 opaque red pixel (RGBA)
cb.set_image(1, 1, [255, 0, 0, 255])
print(cb.has_image())  // true

Check whether the clipboard holds non-empty text

Returns true if the clipboard currently holds non-empty text, false otherwise. Never errors, making it a safe guard before calling get_text.

use plugin clipboard::{Clipboard}

let cb = Clipboard.new()
if cb.has_text() {
  print(cb.get_text())
} else {
  print("clipboard has no text")
}

Check whether the clipboard holds an image

Returns true if the clipboard currently holds an image, false otherwise. Never errors, making it a safe guard before calling get_image.

use plugin clipboard::{Clipboard}

let cb = Clipboard.new()
print(cb.has_image())

Clear all clipboard contents

Removes all contents from the clipboard.

use plugin clipboard::{Clipboard}

let cb = Clipboard.new()
cb.set_text("temporary secret")
cb.clear()
print(cb.has_text())  // false
enespt-br