mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-06-01 14:43:55 +00:00
Add clipboard and TypeStr
This commit is contained in:
parent
a5173b5b54
commit
861396b054
18
clipboard/.travis.yml
Normal file
18
clipboard/.travis.yml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- go1.2.2
|
||||||
|
- go1.3.3
|
||||||
|
- go1.4.3
|
||||||
|
- go1.5.3
|
||||||
|
- go1.6
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- export DISPLAY=:99.0
|
||||||
|
- sh -e /etc/init.d/xvfb start
|
||||||
|
|
||||||
|
script:
|
||||||
|
- sudo apt-get install xsel
|
||||||
|
- go test -v .
|
||||||
|
- sudo apt-get install xclip
|
||||||
|
- go test -v .
|
27
clipboard/LICENSE
Normal file
27
clipboard/LICENSE
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
Copyright (c) 2013 Ato Araki. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following disclaimer
|
||||||
|
in the documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
* Neither the name of @atotto. nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
50
clipboard/README.md
Normal file
50
clipboard/README.md
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
[](https://travis-ci.org/atotto/clipboard) [](https://drone.io/github.com/atotto/clipboard/latest)
|
||||||
|
|
||||||
|
[](http://godoc.org/github.com/atotto/clipboard)
|
||||||
|
|
||||||
|
# Clipboard for Go
|
||||||
|
|
||||||
|
Provide copying and pasting to the Clipboard for Go.
|
||||||
|
|
||||||
|
Download shell commands at https://drone.io/github.com/atotto/clipboard/files
|
||||||
|
|
||||||
|
Build:
|
||||||
|
|
||||||
|
$ go get github.com/atotto/clipboard
|
||||||
|
|
||||||
|
Platforms:
|
||||||
|
|
||||||
|
* OSX
|
||||||
|
* Windows 7 (probably work on other Windows)
|
||||||
|
* Linux, Unix (requires 'xclip' or 'xsel' command to be installed)
|
||||||
|
|
||||||
|
|
||||||
|
Document:
|
||||||
|
|
||||||
|
* http://godoc.org/github.com/atotto/clipboard
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
* Text string only
|
||||||
|
* UTF-8 text encoding only (no conversion)
|
||||||
|
|
||||||
|
TODO:
|
||||||
|
|
||||||
|
* Clipboard watcher(?)
|
||||||
|
|
||||||
|
## Commands:
|
||||||
|
|
||||||
|
paste shell command:
|
||||||
|
|
||||||
|
$ go get github.com/atotto/clipboard/cmd/gopaste
|
||||||
|
$ # example:
|
||||||
|
$ gopaste > document.txt
|
||||||
|
|
||||||
|
copy shell command:
|
||||||
|
|
||||||
|
$ go get github.com/atotto/clipboard/cmd/gocopy
|
||||||
|
$ # example:
|
||||||
|
$ cat document.txt | gocopy
|
||||||
|
|
||||||
|
|
||||||
|
|
22
clipboard/clipboard.go
Normal file
22
clipboard/clipboard.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Copyright 2013 @atotto. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package clipboard read/write on clipboard
|
||||||
|
package clipboard
|
||||||
|
|
||||||
|
// import ()
|
||||||
|
|
||||||
|
// ReadAll read string from clipboard
|
||||||
|
func ReadAll() (string, error) {
|
||||||
|
return readAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteAll write string to clipboard
|
||||||
|
func WriteAll(text string) error {
|
||||||
|
return writeAll(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsupported might be set true during clipboard init, to help callers decide
|
||||||
|
// whether or not to offer clipboard options.
|
||||||
|
var Unsupported bool
|
52
clipboard/clipboard_darwin.go
Normal file
52
clipboard/clipboard_darwin.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// Copyright 2013 @atotto. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build darwin
|
||||||
|
|
||||||
|
package clipboard
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
pasteCmdArgs = "pbpaste"
|
||||||
|
copyCmdArgs = "pbcopy"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getPasteCommand() *exec.Cmd {
|
||||||
|
return exec.Command(pasteCmdArgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCopyCommand() *exec.Cmd {
|
||||||
|
return exec.Command(copyCmdArgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func readAll() (string, error) {
|
||||||
|
pasteCmd := getPasteCommand()
|
||||||
|
out, err := pasteCmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(out), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeAll(text string) error {
|
||||||
|
copyCmd := getCopyCommand()
|
||||||
|
in, err := copyCmd.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := copyCmd.Start(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := in.Write([]byte(text)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := in.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return copyCmd.Wait()
|
||||||
|
}
|
73
clipboard/clipboard_test.go
Normal file
73
clipboard/clipboard_test.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
// Copyright 2013 @atotto. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package clipboard_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/atotto/clipboard"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCopyAndPaste(t *testing.T) {
|
||||||
|
expected := "日本語"
|
||||||
|
|
||||||
|
err := WriteAll(expected)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual, err := ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf("want %s, got %s", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMultiCopyAndPaste(t *testing.T) {
|
||||||
|
expected1 := "French: éèêëàùœç"
|
||||||
|
expected2 := "Weird UTF-8: 💩☃"
|
||||||
|
|
||||||
|
err := WriteAll(expected1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual1, err := ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if actual1 != expected1 {
|
||||||
|
t.Errorf("want %s, got %s", expected1, actual1)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = WriteAll(expected2)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual2, err := ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if actual2 != expected2 {
|
||||||
|
t.Errorf("want %s, got %s", expected2, actual2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkReadAll(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
ReadAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkWriteAll(b *testing.B) {
|
||||||
|
text := "いろはにほへと"
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
WriteAll(text)
|
||||||
|
}
|
||||||
|
}
|
90
clipboard/clipboard_unix.go
Normal file
90
clipboard/clipboard_unix.go
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
// Copyright 2013 @atotto. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build freebsd linux netbsd openbsd solaris
|
||||||
|
|
||||||
|
package clipboard
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
xsel = "xsel"
|
||||||
|
xclip = "xclip"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
pasteCmdArgs []string
|
||||||
|
copyCmdArgs []string
|
||||||
|
|
||||||
|
xselPasteArgs = []string{xsel, "--output", "--clipboard"}
|
||||||
|
xselCopyArgs = []string{xsel, "--input", "--clipboard"}
|
||||||
|
|
||||||
|
xclipPasteArgs = []string{xclip, "-out", "-selection", "clipboard"}
|
||||||
|
xclipCopyArgs = []string{xclip, "-in", "-selection", "clipboard"}
|
||||||
|
|
||||||
|
missingCommands = errors.New("No clipboard utilities available. Please install xsel or xclip.")
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
pasteCmdArgs = xclipPasteArgs
|
||||||
|
copyCmdArgs = xclipCopyArgs
|
||||||
|
|
||||||
|
if _, err := exec.LookPath(xclip); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pasteCmdArgs = xselPasteArgs
|
||||||
|
copyCmdArgs = xselCopyArgs
|
||||||
|
|
||||||
|
if _, err := exec.LookPath(xsel); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Unsupported = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPasteCommand() *exec.Cmd {
|
||||||
|
return exec.Command(pasteCmdArgs[0], pasteCmdArgs[1:]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCopyCommand() *exec.Cmd {
|
||||||
|
return exec.Command(copyCmdArgs[0], copyCmdArgs[1:]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func readAll() (string, error) {
|
||||||
|
if Unsupported {
|
||||||
|
return "", missingCommands
|
||||||
|
}
|
||||||
|
pasteCmd := getPasteCommand()
|
||||||
|
out, err := pasteCmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(out), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeAll(text string) error {
|
||||||
|
if Unsupported {
|
||||||
|
return missingCommands
|
||||||
|
}
|
||||||
|
copyCmd := getCopyCommand()
|
||||||
|
in, err := copyCmd.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := copyCmd.Start(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := in.Write([]byte(text)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := in.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return copyCmd.Wait()
|
||||||
|
}
|
101
clipboard/clipboard_windows.go
Normal file
101
clipboard/clipboard_windows.go
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
// Copyright 2013 @atotto. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build windows
|
||||||
|
|
||||||
|
package clipboard
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
cfUnicodetext = 13
|
||||||
|
gmemFixed = 0x0000
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
user32 = syscall.MustLoadDLL("user32")
|
||||||
|
openClipboard = user32.MustFindProc("OpenClipboard")
|
||||||
|
closeClipboard = user32.MustFindProc("CloseClipboard")
|
||||||
|
emptyClipboard = user32.MustFindProc("EmptyClipboard")
|
||||||
|
getClipboardData = user32.MustFindProc("GetClipboardData")
|
||||||
|
setClipboardData = user32.MustFindProc("SetClipboardData")
|
||||||
|
|
||||||
|
kernel32 = syscall.NewLazyDLL("kernel32")
|
||||||
|
globalAlloc = kernel32.NewProc("GlobalAlloc")
|
||||||
|
globalFree = kernel32.NewProc("GlobalFree")
|
||||||
|
globalLock = kernel32.NewProc("GlobalLock")
|
||||||
|
globalUnlock = kernel32.NewProc("GlobalUnlock")
|
||||||
|
lstrcpy = kernel32.NewProc("lstrcpyW")
|
||||||
|
)
|
||||||
|
|
||||||
|
func readAll() (string, error) {
|
||||||
|
r, _, err := openClipboard.Call(0)
|
||||||
|
if r == 0 {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer closeClipboard.Call()
|
||||||
|
|
||||||
|
h, _, err := getClipboardData.Call(cfUnicodetext)
|
||||||
|
if r == 0 {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
l, _, err := globalLock.Call(h)
|
||||||
|
if l == 0 {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
text := syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(l))[:])
|
||||||
|
|
||||||
|
r, _, err = globalUnlock.Call(h)
|
||||||
|
if r == 0 {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return text, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeAll(text string) error {
|
||||||
|
r, _, err := openClipboard.Call(0)
|
||||||
|
if r == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer closeClipboard.Call()
|
||||||
|
|
||||||
|
r, _, err = emptyClipboard.Call(0)
|
||||||
|
if r == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
data := syscall.StringToUTF16(text)
|
||||||
|
|
||||||
|
h, _, err := globalAlloc.Call(gmemFixed, uintptr(len(data)*int(unsafe.Sizeof(data[0]))))
|
||||||
|
if h == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
l, _, err := globalLock.Call(h)
|
||||||
|
if l == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
r, _, err = lstrcpy.Call(l, uintptr(unsafe.Pointer(&data[0])))
|
||||||
|
if r == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
r, _, err = globalUnlock.Call(h)
|
||||||
|
if r == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
r, _, err = setClipboardData.Call(cfUnicodetext, h)
|
||||||
|
if r == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
20
clipboard/cmd/gocopy/gocopy.go
Normal file
20
clipboard/cmd/gocopy/gocopy.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/atotto/clipboard"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
out, err := ioutil.ReadAll(os.Stdin)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := clipboard.WriteAll(string(out)); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
16
clipboard/cmd/gopaste/gopaste.go
Normal file
16
clipboard/cmd/gopaste/gopaste.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/atotto/clipboard"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
text, err := clipboard.ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print(text)
|
||||||
|
}
|
16
clipboard/example_test.go
Normal file
16
clipboard/example_test.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package clipboard_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/atotto/clipboard"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Example() {
|
||||||
|
clipboard.WriteAll("日本語")
|
||||||
|
text, _ := clipboard.ReadAll()
|
||||||
|
fmt.Println(text)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 日本語
|
||||||
|
}
|
18
robotgo.go
18
robotgo.go
@ -47,6 +47,8 @@ import (
|
|||||||
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/go-vgo/robotgo/clipboard"
|
||||||
// "runtime"
|
// "runtime"
|
||||||
// "syscall"
|
// "syscall"
|
||||||
)
|
)
|
||||||
@ -498,6 +500,22 @@ func TypeString(x string) {
|
|||||||
defer C.free(unsafe.Pointer(cx))
|
defer C.free(unsafe.Pointer(cx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TypeStr type string, support UTF-8
|
||||||
|
func TypeStr(str string) {
|
||||||
|
clipboard.WriteAll(str)
|
||||||
|
KeyTap("v", "command")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadAll read string from clipboard
|
||||||
|
func ReadAll() (string, error) {
|
||||||
|
return clipboard.ReadAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteAll write string to clipboard
|
||||||
|
func WriteAll(text string) {
|
||||||
|
clipboard.WriteAll(text)
|
||||||
|
}
|
||||||
|
|
||||||
// TypeStrDelay type string delayed
|
// TypeStrDelay type string delayed
|
||||||
func TypeStrDelay(x string, y int) {
|
func TypeStrDelay(x string, y int) {
|
||||||
cx := C.CString(x)
|
cx := C.CString(x)
|
||||||
|
Loading…
Reference in New Issue
Block a user