I have a Python TUI application and I want it to save the result of a user selection from the keyboard.
It's pretty easy to find a Windows and Mac API to copy something to the user clipboard programmatically, but how to do so in Linux from Python?
It needs to:
- work accross desktop environments: at least Gnome Shell and KDE.
- work on X11 and Wayland.
- work without installing a third party package from the Linux repo (such as
xclip
) since you can'tpip install xclip
. This means the pypi clipboard package is excluded since it relies on that. - it works without loading a GUI widget that requires the user to select the content, since the copy may happen in the background or in a TUI. The text may not be selected with the mouse.
- it should not install GTK, QT, or other heavy dependency that wouldn't be used for anything else. Installing a pure python 3rd party package is ok.
xclip
does it for XWindows, so there muse be an API for that. And wl-clipboard
for Wayland allows it, so there must be some C lib I can call using ctypes
maybe.