Clipboard
This guide describes how to access the system clipboard to read and write data.
Overview
The system clipboard is a temporary storage area used by most operating systems to transfer data between applications, via copy and paste operations. The clipboard is usually temporary and unnamed, and its contents reside in the computer’s RAM. The clipboard is sometimes called the paste buffer.
Molybden allows you to programmatically read and write text and other kinds of data to and from the system clipboard. The system clipboard can contain multiple data types at the same time.
Writing to the clipboard
You can write different kinds of data to the system clipboard.
The following example shows how to write plain text to the clipboard:
app->clipboard()->write(
ClipboardData::create(ClipboardDataType::plainText(), "Hello, world!"));
To write data of multiple types to the clipboard, use the following approach:
std::vector data = {
ClipboardData::create(ClipboardDataType::plainText(), "Hello!"),
ClipboardData::create(ClipboardDataType::html(), "<h1>Hello!</h1>"),
ClipboardData::create(ClipboardDataType::rtf(),
"{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss "
"Helvetica;}\\f0\\pard\nHello, "
"{\\b bold} world!\\par\n}")};
app->clipboard()->write(data);
Reading from the clipboard
To read data from the clipboard, use the following approach:
for (const auto& item : app->clipboard()->read()) {
std::cout << "Type: " << item->type()->mimeType() << std::endl;
std::cout << "Data: " << item->data() << std::endl;
}
You can check the data type and read only the data you need. For example, the following code shows how to read only plain text from the clipboard:
for (const auto& item : app->clipboard()->read()) {
if (item->type() == ClipboardDataType::plainText()) {
std::cout << "Plain text: " << item->data() << std::endl;
}
}
Clearing the clipboard
You can clear the system clipboard content using the following approach:
app->clipboard()->clear();
If the system clipboard is already empty, then this method does nothing.