https://github.com/karpeleslab/goclip
Clipboard manipulation with Go
https://github.com/karpeleslab/goclip
Last synced: 12 months ago
JSON representation
Clipboard manipulation with Go
- Host: GitHub
- URL: https://github.com/karpeleslab/goclip
- Owner: KarpelesLab
- Created: 2020-11-16T10:01:57.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-03-14T05:13:56.000Z (over 1 year ago)
- Last Synced: 2025-07-01T00:08:53.949Z (12 months ago)
- Language: Go
- Size: 43.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://godoc.org/github.com/KarpelesLab/goclip)
# GoClip
**WORK IN PROGRESS** This is not ready for use yet.
Manipulate clipboard from Go, using system libraries.
Most clipboard implementations for Go out there rely on external programs to handle clipboard which limits what can be done quite a bit, only support text and do not have monitoring support.
GoClip aims to provide a cross platform API that can be used easily without compromise on what can be done.
## Target features
* Easily read from or write to the clipboard
* Support for selection clipboard on X11
* Support for the following types of data:
* Unicode Text
* Images (returned as raw data in object with access methods to convert to `image.Image`)
* File lists
* Notifications on clipboard contents updated (Monitor)
* All operations are done using the appropriate libs (no execution of external commands)
* On Windows acquiring ownership of the clipboard can take time. Contexts allows setting a timeout and a cancel method allowing for fine control on the process.
## Code samples
**Warning**: this will not work. This code is only there to illustrate the goal for this project.
### Read from clipboard
```go
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
data, err := goclip.Paste(ctx)
if err != nil {
...
}
text, err := data.ToText(ctx)
if err != nil {
...
}
log.Printf("pasted text: %s", text)
```
Or
```go
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
data, _ := goclip.Paste(ctx)
switch data.Type() { // data.Type() will return goclip.Invalid if no data
case goclip.Image:
img, err := data.ToImage(ctx) // converts data into image
}
```
### Write to clipboard
```go
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
err := goclip.Copy(ctx, "Hello World") // copy text
err := goclip.Copy(ctx, image.NewRGBA(...)) // copy image
err := goclip.Copy(ctx, os.Open("...")) // file
```
### Monitoring
```go
monitor := goclip.NewMonitor()
monitor.Subscribe(func(d goclip.Data) error {
...
})
...
// call monitor.Poll() when gaining window focus, or on regular but slow-ish interval
```