mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-05-31 06:13:55 +00:00
Merge pull request #452 from go-vgo/bitmap-pr
Refactor error return to clear type, fixed some sleep
This commit is contained in:
commit
b6ef01a929
@ -45,7 +45,7 @@ func keyTap() {
|
||||
|
||||
// hide window
|
||||
err := robotgo.KeyTap("h", "cmd")
|
||||
if err != "" {
|
||||
if err != nil {
|
||||
fmt.Println("robotgo.KeyTap run error is: ", err)
|
||||
}
|
||||
|
||||
@ -82,7 +82,7 @@ func keyToggle() {
|
||||
robotgo.KeyToggle("q", "up", "alt", "cmd", "shift")
|
||||
|
||||
err := robotgo.KeyToggle("enter", "down")
|
||||
if err != "" {
|
||||
if err != nil {
|
||||
fmt.Println("robotgo.KeyToggle run error is: ", err)
|
||||
}
|
||||
}
|
||||
|
54
robotgo.go
54
robotgo.go
@ -48,7 +48,7 @@ package robotgo
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"image"
|
||||
|
||||
// "os"
|
||||
@ -697,7 +697,7 @@ func MovesClick(x, y int, args ...interface{}) {
|
||||
// Examples:
|
||||
// robotgo.Toggle("left") // default is down
|
||||
// robotgo.Toggle("left", "up")
|
||||
func Toggle(key ...string) int {
|
||||
func Toggle(key ...string) error {
|
||||
var button C.MMMouseButton = C.LEFT_BUTTON
|
||||
if len(key) > 0 {
|
||||
button = CheckMouse(key[0])
|
||||
@ -710,7 +710,10 @@ func Toggle(key ...string) int {
|
||||
i := C.mouse_toggle(down, button)
|
||||
C.free(unsafe.Pointer(down))
|
||||
MilliSleep(MouseSleep)
|
||||
return int(i)
|
||||
if int(i) == 0 {
|
||||
return nil
|
||||
}
|
||||
return errors.New("Undefined params.")
|
||||
}
|
||||
|
||||
// Deprecated: use the Toggle(),
|
||||
@ -830,6 +833,14 @@ func SetMouseDelay(delay int) {
|
||||
|
||||
*/
|
||||
|
||||
func toErr(str *C.char) error {
|
||||
gstr := C.GoString(str)
|
||||
if gstr == "" {
|
||||
return nil
|
||||
}
|
||||
return errors.New(gstr)
|
||||
}
|
||||
|
||||
// KeyTap tap the keyboard code;
|
||||
//
|
||||
// See keys:
|
||||
@ -843,7 +854,7 @@ func SetMouseDelay(delay int) {
|
||||
// arr := []string{"alt", "command"}
|
||||
// robotgo.KeyTap("i", arr)
|
||||
//
|
||||
func KeyTap(tapKey string, args ...interface{}) string {
|
||||
func KeyTap(tapKey string, args ...interface{}) error {
|
||||
var (
|
||||
akey string
|
||||
keyT = "null"
|
||||
@ -876,7 +887,7 @@ func KeyTap(tapKey string, args ...interface{}) string {
|
||||
str := C.key_Taps(zkey,
|
||||
(**C.char)(unsafe.Pointer(&ckeyArr[0])), C.int(num), 0)
|
||||
MilliSleep(KeySleep)
|
||||
return C.GoString(str)
|
||||
return toErr(str)
|
||||
}
|
||||
|
||||
// key delay
|
||||
@ -917,7 +928,7 @@ func KeyTap(tapKey string, args ...interface{}) string {
|
||||
C.int(num), C.int(keyDelay))
|
||||
|
||||
MilliSleep(KeySleep)
|
||||
return C.GoString(str)
|
||||
return toErr(str)
|
||||
}
|
||||
|
||||
amod := C.CString(akey)
|
||||
@ -928,7 +939,7 @@ func KeyTap(tapKey string, args ...interface{}) string {
|
||||
C.free(unsafe.Pointer(amodt))
|
||||
|
||||
MilliSleep(KeySleep)
|
||||
return C.GoString(str)
|
||||
return toErr(str)
|
||||
}
|
||||
|
||||
// KeyToggle toggle the keyboard, if there not have args default is "down"
|
||||
@ -942,7 +953,7 @@ func KeyTap(tapKey string, args ...interface{}) string {
|
||||
//
|
||||
// robotgo.KeyToggle("a", "up", "alt", "cmd")
|
||||
//
|
||||
func KeyToggle(key string, args ...string) string {
|
||||
func KeyToggle(key string, args ...string) error {
|
||||
if len(args) <= 0 {
|
||||
args = append(args, "down")
|
||||
}
|
||||
@ -966,7 +977,7 @@ func KeyToggle(key string, args ...string) string {
|
||||
|
||||
str := C.key_Toggles(ckey, (**C.char)(unsafe.Pointer(&ckeyArr[0])), C.int(num))
|
||||
MilliSleep(KeySleep)
|
||||
return C.GoString(str)
|
||||
return toErr(str)
|
||||
}
|
||||
|
||||
// use key_toggle()
|
||||
@ -998,24 +1009,27 @@ func KeyToggle(key string, args ...string) string {
|
||||
C.free(unsafe.Pointer(cmKeyT))
|
||||
|
||||
MilliSleep(KeySleep)
|
||||
return C.GoString(str)
|
||||
return toErr(str)
|
||||
}
|
||||
|
||||
// KeyPress press key string
|
||||
func KeyPress(key string) {
|
||||
KeyDown(key)
|
||||
Sleep(1 + rand.Intn(3))
|
||||
KeyUp(key)
|
||||
func KeyPress(key string) error {
|
||||
err := KeyDown(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
MilliSleep(1 + rand.Intn(3))
|
||||
return KeyUp(key)
|
||||
}
|
||||
|
||||
// KeyDown press down a key
|
||||
func KeyDown(key string) {
|
||||
KeyToggle(key, "down")
|
||||
func KeyDown(key string) error {
|
||||
return KeyToggle(key)
|
||||
}
|
||||
|
||||
// KeyUp press up a key
|
||||
func KeyUp(key string) {
|
||||
KeyToggle(key, "up")
|
||||
func KeyUp(key string) error {
|
||||
return KeyToggle(key, "up")
|
||||
}
|
||||
|
||||
// ReadAll read string from clipboard
|
||||
@ -1123,10 +1137,10 @@ func TypeStr(str string, args ...float64) {
|
||||
|
||||
// PasteStr paste a string, support UTF-8,
|
||||
// write the string to clipboard and tap `cmd + v`
|
||||
func PasteStr(str string) string {
|
||||
func PasteStr(str string) error {
|
||||
err := clipboard.WriteAll(str)
|
||||
if err != nil {
|
||||
return fmt.Sprint(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if runtime.GOOS == "darwin" {
|
||||
|
@ -102,21 +102,21 @@ func TestMoveSmoothRelative(t *testing.T) {
|
||||
|
||||
func TestMouseToggle(t *testing.T) {
|
||||
e := Toggle("right")
|
||||
tt.Zero(t, e)
|
||||
tt.Nil(t, e)
|
||||
|
||||
e = Toggle("right", "up")
|
||||
tt.Zero(t, e)
|
||||
tt.Nil(t, e)
|
||||
}
|
||||
|
||||
func TestKey(t *testing.T) {
|
||||
e := KeyTap("v", "cmd")
|
||||
tt.Empty(t, e)
|
||||
tt.Nil(t, e)
|
||||
|
||||
e = KeyTap("enter")
|
||||
tt.Empty(t, e)
|
||||
tt.Nil(t, e)
|
||||
|
||||
e = KeyToggle("v", "up")
|
||||
tt.Empty(t, e)
|
||||
tt.Nil(t, e)
|
||||
}
|
||||
|
||||
func TestClip(t *testing.T) {
|
||||
@ -133,7 +133,7 @@ func TestTypeStr(t *testing.T) {
|
||||
tt.Equal(t, 115, c)
|
||||
|
||||
e := PasteStr("s")
|
||||
tt.Empty(t, e)
|
||||
tt.Nil(t, e)
|
||||
|
||||
s1 := "abc\\\\cd/s@世界"
|
||||
uc := ToUC(s1)
|
||||
@ -210,5 +210,5 @@ func TestPs(t *testing.T) {
|
||||
// }()
|
||||
|
||||
// i := Alert("t", "msg")
|
||||
// tt.Zero(t, i)
|
||||
// tt.True(t, i)
|
||||
// }
|
||||
|
Loading…
Reference in New Issue
Block a user