There are several C++ libraries to do this. But each one has its own peculiarities, however, we will mention the majority at the end of this article.
In this example we will see how to use Clip.
Clip is a cross-platform C++ library (Windows, macOS and GNU/Linux) for copying/pasting content from the clipboard.
The simplest way to use is:
mkdir -p myproject
cd myproject
git clone https://github.com/dacap/clip
string
:
main.cpp
#include "clip/clip.h"
int main(){
clip::set_text("This text will be copied to the clipboard");
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(Example)
add_executable(a.out main.cpp)
add_subdirectory(clip)
target_link_libraries(a.out clip)
Change the CMake version to your version, I put this version for compatibility reasons, but there will probably be a warning when running it, but it will work!
cmake . -B build
cd build && make
./a.out
When running the binary, try typing: Ctrl + v
somewhere else and notice that the content was copied!
NOTE: This may not work in window managers, you need to configure files for compatibility, despite the xclip command work normally!
But, I tested it on GNOME and Windows and it worked perfectly!
For more information, visit the Clip repository.
And as I said, there are many options for it, including: