Documentation ¶
Overview ¶
Package clipboard provides three major APIs for manipulating the clipboard: `Read`, `Write`, and `Watch`. The most common operations are `Read` and `Write`. To use them:
// write/read text format data of the clipboard, and // the byte buffer regarding the text are UTF8 encoded. clipboard.Write(clipboard.FmtText, []byte("text data")) clipboard.Read(clipboard.FmtText) // write/read image format data of the clipboard, and // the byte buffer regarding the image are PNG encoded. clipboard.Write(clipboard.FmtImage, []byte("image data")) clipboard.Read(clipboard.FmtImage)
Note that read/write regarding image format assumes that the bytes are PNG encoded since it serves the alpha blending purpose that might be used in other graphical software.
In addition, `clipboard.Write` returns a channel that can receive an empty struct as a signal, which indicates the corresponding write call to the clipboard is outdated, meaning the clipboard has been overwritten by others and the previously written data is lost. For instance:
changed := clipboard.Write(clipboard.FmtText, []byte("text data")) select { case <-changed: println(`"text data" is no longer available from clipboard.`) }
You can ignore the returning channel if you don't need this type of notification. Furthermore, when you need more than just knowing whether clipboard data is changed, use the watcher API:
ch := clipboard.Watch(context.TODO(), clipboard.FmtText) for data := range ch { // print out clipboard data whenever it is changed println(string(data)) }
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Read ¶
Read returns a chunk of bytes of the clipboard data if it presents in the desired format t presents. Otherwise, it returns nil.
Example ¶
package main import ( "fmt" "golang.design/x/clipboard" ) func main() { fmt.Printf(string(clipboard.Read(clipboard.FmtText))) }
Output: Hello, 世界
func Watch ¶ added in v0.3.0
Watch returns a receive-only channel that received the clipboard data whenever any change of clipboard data in the desired format happens.
The returned channel will be closed if the given context is canceled.
Example ¶
package main import ( "context" "fmt" "time" "golang.design/x/clipboard" ) func main() { ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) defer cancel() changed := clipboard.Watch(context.Background(), clipboard.FmtText) go func(ctx context.Context) { clipboard.Write(clipboard.FmtText, []byte("你好,world")) }(ctx) fmt.Printf(string(<-changed)) }
Output: 你好,world
func Write ¶
Write writes a given buffer to the clipboard in a specified format. Write returned a receive-only channel can receive an empty struct as a signal, which indicates the clipboard has been overwritten from this write. If format t indicates an image, then the given buf assumes the image data is PNG encoded.
Example ¶
package main import ( "golang.design/x/clipboard" ) func main() { clipboard.Write(clipboard.FmtText, []byte("Hello, 世界")) }
Output: