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