From b9cb452594153a2892de72c2678c45aeca0ae089 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Wed, 24 Nov 2021 15:42:08 -0400 Subject: [PATCH 1/2] optimize code, add ps.Run() function and update godoc --- README.md | 4 +-- README_zh.md | 4 +-- bitmap.go | 40 +++++++++++++++++++---------- hook.go | 4 ++- ps.go | 5 ++++ robotgo.go | 72 ++++++++++++++++++++++++++++------------------------ 6 files changed, 77 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index bf74a8b..5c0b9b5 100644 --- a/README.md +++ b/README.md @@ -259,8 +259,8 @@ func main() { fmt.Println("FindBitmap------ ", fx, fy) robotgo.Move(fx, fy) - arr := robotgo.FindEveryBitmap(bit2) - fmt.Println("Find every bitmap: ", arr) + arr := robotgo.FindAllBitmap(bit2) + fmt.Println("Find all bitmap: ", arr) robotgo.SaveBitmap(bitmap, "test.png") fx, fy = robotgo.FindBitmap(bitmap) diff --git a/README_zh.md b/README_zh.md index 350248b..9bc5454 100644 --- a/README_zh.md +++ b/README_zh.md @@ -251,8 +251,8 @@ func main() { fmt.Println("FindBitmap------ ", fx, fy) robotgo.Move(fx, fy) - arr := robotgo.FindEveryBitmap(bit2) - fmt.Println("Find every bitmap: ", arr) + arr := robotgo.FindAllBitmap(bit2) + fmt.Println("Find all bitmap: ", arr) robotgo.SaveBitmap(bitmap, "test.png") fx, fy = robotgo.FindBitmap(bitmap) diff --git a/bitmap.go b/bitmap.go index ef3e8a4..cd86c1d 100644 --- a/bitmap.go +++ b/bitmap.go @@ -49,6 +49,13 @@ func SaveCapture(spath string, args ...int) string { return err } +// FreeBitmapArr free and dealloc the C bitmap array +func FreeBitmapArr(bit ...C.MMBitmapRef) { + for i := 0; i < len(bit); i++ { + FreeBitmap(bit[i]) + } +} + // ToCBitmap trans Bitmap to C.MMBitmapRef func ToCBitmap(bit Bitmap) C.MMBitmapRef { cbitmap := C.createMMBitmap( @@ -108,8 +115,8 @@ func FindCBitmap(bmp CBitmap, args ...interface{}) (int, int) { // // robotgo.FindBitmap(bitmap, source_bitamp C.MMBitmapRef, tolerance float64) // -// |tolerance| should be in the range 0.0f - 1.0f, denoting how closely the -// colors in the bitmaps need to match, with 0 being exact and 1 being any. +// |tolerance| should be in the range 0.0f - 1.0f, denoting how closely the +// colors in the bitmaps need to match, with 0 being exact and 1 being any. // // This method only automatically free the internal bitmap, // use `defer robotgo.FreeBitmap(bit)` to free the bitmap @@ -176,8 +183,14 @@ func FreeMMPointArr(pointArray C.MMPointArrayRef) { C.destroyMMPointArray(pointArray) } -// FindEveryBitmap find the every bitmap -func FindEveryBitmap(bit C.MMBitmapRef, args ...interface{}) (posArr []Point) { +// Deprecated: use the FindAllBitmap() +// FindEveryBitmap find the every bitmap, same with the FindAllBitmap() +func FindEveryBitmap(bit C.MMBitmapRef, args ...interface{}) []Point { + return FindAllBitmap(bit, args...) +} + +// FindAllBitmap find the all bitmap +func FindAllBitmap(bit C.MMBitmapRef, args ...interface{}) (posArr []Point) { var ( sbit C.MMBitmapRef tolerance C.float = 0.01 @@ -332,13 +345,6 @@ func Convert(opath, spath string, args ...int) string { return SaveBitmap(bitmap, spath, mtype) } -// FreeBitmapArr free and dealloc the C bitmap array -func FreeBitmapArr(bit ...C.MMBitmapRef) { - for i := 0; i < len(bit); i++ { - FreeBitmap(bit[i]) - } -} - // ReadBitmap returns false and sets error if |bitmap| is NULL func ReadBitmap(bitmap C.MMBitmapRef) bool { abool := C.bitmap_ready(bitmap) @@ -368,7 +374,7 @@ func DeepCopyBit(bitmap C.MMBitmapRef) C.MMBitmapRef { return bit } -// GetColor get bitmap color +// GetColor get the bitmap color func GetColor(bitmap C.MMBitmapRef, x, y int) C.MMRGBHex { color := C.bitmap_get_color(bitmap, C.size_t(x), C.size_t(y)) @@ -427,8 +433,14 @@ func FindColorCS(color CHex, x, y, w, h int, args ...float64) (int, int) { return rx, ry } -// FindEveryColor find every color -func FindEveryColor(color CHex, args ...interface{}) (posArr []Point) { +// Deprecated: use the FindAllColor() +// FindEveryColor find the every color, same with the FindAllColor() +func FindEveryColor(color CHex, args ...interface{}) []Point { + return FindAllColor(color, args...) +} + +// FindAllColor find the all color +func FindAllColor(color CHex, args ...interface{}) (posArr []Point) { var ( bitmap C.MMBitmapRef tolerance C.float = 0.01 diff --git a/hook.go b/hook.go index f059c89..4f645aa 100644 --- a/hook.go +++ b/hook.go @@ -36,18 +36,20 @@ func EventEnd() { hook.End() } +// Deprecated: use the EventStart() // Start start global event hook // return event channel func Start() chan hook.Event { return hook.Start() } +// Deprecated: use the EventEnd() // End removes global event hook func End() { hook.End() } -// StopEvent stop event listener +// StopEvent stop event listener, use by AddEvent() func StopEvent() { hook.StopEvent() } diff --git a/ps.go b/ps.go index e6eddc1..9a45386 100644 --- a/ps.go +++ b/ps.go @@ -66,6 +66,11 @@ func FindPath(pid int32) (string, error) { return ps.FindPath(pid) } +// Run run a cmd shell +func Run(path string) ([]byte, error) { + return ps.Run(path) +} + // Kill kill the process by PID func Kill(pid int32) error { return ps.Kill(pid) diff --git a/robotgo.go b/robotgo.go index 7d319e8..33915fb 100644 --- a/robotgo.go +++ b/robotgo.go @@ -205,14 +205,7 @@ func GetPxColor(x, y int) C.MMRGBHex { // GetPixelColor get the pixel color return string func GetPixelColor(x, y int) string { - cx := C.int32_t(x) - cy := C.int32_t(y) - - color := C.get_pixel_color(cx, cy) - gcolor := C.GoString(color) - C.free(unsafe.Pointer(color)) - - return gcolor + return PadHex(GetPxColor(x, y)) } // GetMouseColor get the mouse pos's color @@ -229,7 +222,8 @@ func SysScale() float64 { // Scaled get the screen scaled size func Scaled(x int) int { - return Scaled0(x, ScaleF()) + f := ScaleF() + return Scaled0(x, f) } // Scaled0 return int(x * f) @@ -244,7 +238,7 @@ func GetScreenSize() (int, int) { return int(size.w), int(size.h) } -// GetScreenRect get the screen rect +// GetScreenRect get the screen rect (x, y, w, h) func GetScreenRect(displayId ...int) Rect { display := 0 if len(displayId) > 0 { @@ -285,6 +279,7 @@ func CaptureScreen(args ...int) C.MMBitmapRef { w = C.int32_t(args[2]) h = C.int32_t(args[3]) } else { + // Get the main screen rect. rect := GetScreenRect() x = C.int32_t(rect.X) @@ -364,19 +359,19 @@ func GetXDisplayName() string { return gname } -// Deprecated: +// Deprecated: use the ScaledF() // ScaleX get the primary display horizontal DPI scale factor, drop func ScaleX() int { return int(C.scale_x()) } -// Deprecated: +// Deprecated: use the ScaledF() // ScaleY get primary display vertical DPI scale factor, drop func ScaleY() int { return int(C.scale_y()) } -// Deprecated: +// Deprecated: use the ScaledF() // Scale get the screen scale (only windows old), drop func Scale() int { dpi := map[int]int{ @@ -399,13 +394,13 @@ func Scale() int { return dpi[x] } -// Deprecated: +// Deprecated: use the ScaledF() // Scale0 return ScaleX() / 0.96, drop func Scale0() int { return int(float64(ScaleX()) / 0.96) } -// Deprecated: +// Deprecated: use the ScaledF() // Mul mul the scale, drop func Mul(x int) int { s := Scale() @@ -441,7 +436,7 @@ func CheckMouse(btn string) C.MMMouseButton { return C.LEFT_BUTTON } -// Deprecated: +// Deprecated: use the Move() // MoveMouse move the mouse func MoveMouse(x, y int) { Move(x, y) @@ -465,7 +460,7 @@ func Move(x, y int) { MilliSleep(MouseSleep) } -// Deprecated: +// Deprecated: use the DragSmooth() // DragMouse drag the mouse to (x, y), // It's same with the DragSmooth() now func DragMouse(x, y int, args ...interface{}) { @@ -476,7 +471,7 @@ func DragMouse(x, y int, args ...interface{}) { Toggle("left", "up") } -// Deprecated: +// Deprecated: use the DragSmooth() // Drag drag the mouse to (x, y), // It's not valid now, use the DragSmooth() func Drag(x, y int, args ...string) { @@ -503,7 +498,7 @@ func DragSmooth(x, y int, args ...interface{}) { Toggle("left", "up") } -// Deprecated: +// Deprecated: use the MoveSmooth() // MoveMouseSmooth move the mouse smooth, // moves mouse to x, y human like, with the mouse button up. func MoveMouseSmooth(x, y int, args ...interface{}) bool { @@ -528,7 +523,7 @@ func MoveSmooth(x, y int, args ...interface{}) bool { cy := C.int32_t(y) var ( - mouseDelay = 10 + mouseDelay = 5 low C.double high C.double ) @@ -571,17 +566,16 @@ func MoveSmoothRelative(x, y int, args ...interface{}) { MoveSmooth(mx, my, args...) } -// GetMousePos get mouse's portion return x, y +// GetMousePos get the mouse's portion return x, y func GetMousePos() (int, int) { pos := C.get_mouse_pos() - x := int(pos.x) y := int(pos.y) return x, y } -// Deprecated: +// Deprecated: use the Click() // MouseClick click the mouse // // robotgo.MouseClick(button string, double bool) @@ -618,14 +612,22 @@ func Click(args ...interface{}) { // MoveClick move and click the mouse // // robotgo.MoveClick(x, y int, button string, double bool) +// +// Examples: +// robotgo.MouseSleep = 100 +// robotgo.MoveClick(10, 10) func MoveClick(x, y int, args ...interface{}) { Move(x, y) + MilliSleep(50) Click(args...) } // MovesClick move smooth and click the mouse +// +// use the `robotgo.MouseSleep = 100` func MovesClick(x, y int, args ...interface{}) { MoveSmooth(x, y) + MilliSleep(50) Click(args...) } @@ -651,7 +653,7 @@ func Toggle(key ...string) int { return int(i) } -// Deprecated: +// Deprecated: use the Toggle() // MouseToggle toggle the mouse // // Examples: @@ -672,7 +674,7 @@ func MouseToggle(togKey string, args ...interface{}) int { return int(i) } -// Deprecated: +// Deprecated: use the Scroll() // ScrollMouse scroll the mouse to (x, "up") // // Examples: @@ -1072,7 +1074,8 @@ func PasteStr(str string) string { return KeyTap("v", "control") } -// Deprecated: TypeString send a string, support unicode(no linux support) +// Deprecated: use the TypeStr() +// TypeString send a string, support unicode(no linux support) // TypeStr(string: The string to send), Wno-deprecated // // This function will be removed in version v1.0.0 @@ -1095,7 +1098,8 @@ func TypeStrDelay(str string, delay int) { Sleep(delay) } -// Deprecated: TypeStringDelayed type string delayed, Wno-deprecated +// Deprecated: use the TypeStr() +// TypeStringDelayed type string delayed, Wno-deprecated // // This function will be removed in version v1.0.0 func TypeStringDelayed(str string, delay int) { @@ -1108,7 +1112,8 @@ func SetKeyDelay(delay int) { C.set_keyboard_delay(C.size_t(delay)) } -// Deprecated: SetKeyboardDelay set keyboard delay, Wno-deprecated, +// Deprecated: use the SetKeyDelay() +// SetKeyboardDelay set keyboard delay, Wno-deprecated, // // This function will be removed in version v1.0.0 func SetKeyboardDelay(delay int) { @@ -1142,7 +1147,7 @@ ____ __ ____ __ .__ __. _______ ______ ____ __ ____ // If cancel button is not given, only the default button is displayed // // Examples: -// robogo.ShowAlert("hi", "window", "ok", "cancel") +// robotgo.ShowAlert("hi", "window", "ok", "cancel") func ShowAlert(title, msg string, args ...string) bool { var ( // title string @@ -1283,7 +1288,8 @@ func GetHandle() int { return ghwnd } -// Deprecated: GetBHandle get the window handle, Wno-deprecated +// Deprecated: use the GetHandle() +// GetBHandle get the window handle, Wno-deprecated // // This function will be removed in version v1.0.0 func GetBHandle() int { @@ -1304,10 +1310,10 @@ func cgetTitle(hwnd, isHwnd int32) string { // GetTitle get the window title return string // // Examples: -// fmt.Println(robogo.GetTitle()) +// fmt.Println(robotgo.GetTitle()) // -// ids, _ := robogo.FindIds() -// robogo.GetTitle(ids[0]) +// ids, _ := robotgo.FindIds() +// robotgo.GetTitle(ids[0]) func GetTitle(args ...int32) string { if len(args) <= 0 { title := C.get_main_title() From f8539139b79b1290d116679e4f24cd725a12d4db Mon Sep 17 00:00:00 2001 From: vcaesar Date: Wed, 24 Nov 2021 15:49:00 -0400 Subject: [PATCH 2/2] Update examples and README.md --- examples/README.md | 5 +++-- examples/bitmap/imgToBitmap/main.go | 2 +- examples/bitmap/main.go | 4 ++-- examples/mouse/main.go | 14 +++++++------- test/main.go | 8 ++++---- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/examples/README.md b/examples/README.md index e7ef069..af5c8ab 100644 --- a/examples/README.md +++ b/examples/README.md @@ -17,9 +17,10 @@ import ( ) func main() { - robotgo.ScrollMouse(10, "up") + // robotgo.ScrollMouse(10, "up") + robotgo.Scroll(0, 10) robotgo.MouseClick("left", true) - robotgo.MoveMouseSmooth(100, 200, 1.0, 100.0) + robotgo.MoveSmooth(100, 200, 1.0, 100.0) } ``` diff --git a/examples/bitmap/imgToBitmap/main.go b/examples/bitmap/imgToBitmap/main.go index 0d6d8a4..17a3df6 100644 --- a/examples/bitmap/imgToBitmap/main.go +++ b/examples/bitmap/imgToBitmap/main.go @@ -46,7 +46,7 @@ func test() { fx, fy := robotgo.FindBitmap(bit2) fmt.Println("FindBitmap------ ", fx, fy) - arr := robotgo.FindEveryBitmap(bit2) + arr := robotgo.FindAllBitmap(bit2) fmt.Println("Find every bitmap: ", arr) robotgo.SaveBitmap(bitmap, "test.png") } diff --git a/examples/bitmap/main.go b/examples/bitmap/main.go index 9c3662d..b9ae1ca 100644 --- a/examples/bitmap/main.go +++ b/examples/bitmap/main.go @@ -59,7 +59,7 @@ func findColor(bmp robotgo.CBitmap) { cnt1 := robotgo.CountColorCS(0xAADCDC, 10, 20, 30, 40) fmt.Println("count...", cnt1) - arr := robotgo.FindEveryColor(0xAADCDC) + arr := robotgo.FindAllColor(0xAADCDC) fmt.Println("find all color: ", arr) for i := 0; i < len(arr); i++ { fmt.Println("pos is: ", arr[i].X, arr[i].Y) @@ -160,7 +160,7 @@ func findBitmap(bmp robotgo.CBitmap) { fx, fy = robotgo.FindPic("test.tif") fmt.Println("FindPic------", fx, fy) - arr := robotgo.FindEveryBitmap(openbit) + arr := robotgo.FindAllBitmap(openbit) fmt.Println("find all bitmap: ", arr) for i := 0; i < len(arr); i++ { fmt.Println("pos is: ", arr[i].X, arr[i].Y) diff --git a/examples/mouse/main.go b/examples/mouse/main.go index 9380fb6..f797727 100644 --- a/examples/mouse/main.go +++ b/examples/mouse/main.go @@ -33,7 +33,7 @@ func move() { // smooth move the mouse to 100, 200 robotgo.MoveSmooth(100, 200) - robotgo.MoveMouseSmooth(100, 200, 1.0, 100.0) + robotgo.MoveSmooth(100, 200, 1.0, 100.0) robotgo.MoveSmoothRelative(10, -100, 1.0, 30.0) for i := 0; i < 1080; i += 1000 { @@ -68,13 +68,13 @@ func get() { func toggleAndScroll() { // scrolls the mouse either up - robotgo.ScrollMouse(10, "up") - robotgo.Scroll(100, 200) + // robotgo.ScrollMouse(10, "up") + robotgo.Scroll(100, 10) + robotgo.Scroll(0, -10) - // toggles right mouse button - robotgo.MouseToggle("down", "right") - - robotgo.MouseToggle("up") + // toggles the right mouse button + robotgo.Toggle("right") + robotgo.Toggle("right", "up") } func mouse() { diff --git a/test/main.go b/test/main.go index 4640216..27a6180 100644 --- a/test/main.go +++ b/test/main.go @@ -26,14 +26,14 @@ func aRobotgo() { x, y := robotgo.GetMousePos() fmt.Println("pos:", x, y) - robotgo.MoveMouse(x, y) - robotgo.MoveMouse(100, 200) + robotgo.Move(x, y) + robotgo.Move(100, 200) robotgo.MouseToggle("up") for i := 0; i < 1080; i += 1000 { fmt.Println(i) - robotgo.MoveMouse(800, i) + robotgo.Move(800, i) } fmt.Println(robotgo.GetPixelColor(x, y)) @@ -65,7 +65,7 @@ func aRobotgo() { fmt.Println("...type", reflect.TypeOf(bitTest), reflect.TypeOf(bitmapTest)) // robotgo.MouseClick() - robotgo.ScrollMouse(10, "up") + robotgo.Scroll(0, 10) } func main() {