linux clipboard read/write in C
Maybe you can look at xclip and see how they have done it.
It provides an interface to X
selections ("the clipboard") from the
command line. It can read data from
standard in or a file and place it in
an X selection for pasting into other
X applications. xclip can also print
an X selection to standard out, which
can then be redirected to a file or
another program.
C++ Get string from Clipboard on Linux
Did you try to find not a code first but a program with an implementation ? I did it for you and found a lot of implementations which use direct X11 calls. I think the most valuable is this but also you may read this. Just find any program and look for the sources. Try to look on wikipedia what applications use x11 clipboard/selection system.
The following programs specifically operate on data transfer
mechanisms:
xcutsel
transfers data from selections to cut buffers or vice versa
xclipboard
,glipper
(Gnome),parcellite
(LXDE), andklipper
(KDE) are
clipboard managers, maybewmcliphist
as wellxcb
shows the content of
the cut buffers and allows the user to manipulate them xselection,
xclip
,xsel
andxcopy
are command line programs that copy data to or
from the X selection. xcopy has a verbosity option that helps debug X
selection issues. parcellite also has the ability to read from and
write to specific X selections from the command line.
synergy
is a cross platform tool that allows you to share a clipboard across
multiple computers running multiple operating systems
xfce4-clipman-plugin
is a "clipboard history plugin for the Xfce4
panel" and also a clipboard manager xtranslate looks up words in the
Xselection in a multi-lingual dictionary autocutsel syncs cut buffer
and selection buffer
Shortly, in theory, X11 has 2 "clipboards": actually a keyboard and for selections - the text you selected immediately can be pasted anywhere you want by pressing middle-mouse button while actual "keyboard" is made for main/default clipboard purposes as exchange by different kind of objects.
P.S. I'd not work with x11 anymore after my experience. Enjoy :)
OS independent clipboard copy/paste text in C
If you're not using a cross platform UI library (like wx or something), then it sounds like you're just going to have to write native clipboard code for each platform you want to support.
Remember, on Macintoshes, you copy with Command-C, not Ctrl+C :)
How can I copy the output of a command directly into my clipboard?
One way of doing it follows:
Install
xclip
, such as:sudo apt-get install xclip
Pipe the output into
xclip
to be copied into the clipboard:cat file | xclip
Paste the text you just copied into a
X
application:xclip -o
To paste somewhere else other than an X
application, such as a text area of a web page in a browser window, use:
cat file | xclip -selection clipboard
Consider creating an alias:
alias "c=xclip"
alias "v=xclip -o"
To see how useful this is, imagine I want to open my current path in a new terminal window (there may be other ways of doing it like Ctrl+T on some systems, but this is just for illustration purposes):
Terminal 1:
pwd | c
Terminal 2:
cd `v`
Notice the ` `
around v
. This executes v
as a command first and then substitutes it in-place for cd
to use.
Only copy the content to the X
clipboard
cat file | xclip
Pipe to/from the clipboard in a Bash script
2018 answer
Use clipboard-cli. It works with macOS, Windows, Linux, OpenBSD, FreeBSD, and Android without any real issues.
Install it with:
npm install -g clipboard-cli
Then you can do:
echo foo | clipboard
If you want, you can alias to cb
by putting the following in your .bashrc
, .bash_profile
, or .zshrc
:
alias cb=clipboard
Related Topics
Run a Script When a New Veth Interface Is Added
How to Make a Cross Compiler Using Gcc
Linux Kconfig Command Line Interface
How to Find Grid Points Nearest to Given Location Using Shell Script
Implementation of Syscall() on Arm-Oabi. What Is "Svc #0X900071"
Search Ip from a Text File in .Csv Log File, If Found Add New Column Next to It
Container Running in Privileged Mode
How to Run The Linux/X86/Shell_Bind_Tcp Payload Stand Alone
How Does Bash Script Command Substitution Work
How to Extract Value from JSON Contained in a Variable Using Jq in Bash
Why Would Vim Create a New File Every Time You Save a File
Keep Ssh Sessions Running After Disconnection
Giving Linux User Git Access But Not Shell Access
Relative-To-Executable Path to Ld-Linux Dynamic Linker/Interpreter
Why Does Printf Still Work with Rax Lower Than The Number of Fp Args in Xmm Registers