Loading... > Golang 桌面自动化。控制鼠标、键盘、读取屏幕、进程、窗口句柄、图像和位图以及全局事件监听器。 RobotGo支持Mac、Windows、Linux(X11);并且 robotgo 支持 arm64 和 x86-amd64。 官方库地址: https://github.com/go-vgo/robotgo 文档地址: https://github.com/go-vgo/robotgo/blob/master/docs/doc.md https://pkg.go.dev/github.com/go-vgo/robotgo ## 内容 * [文档](https://github.com/go-vgo/robotgo#docs) * [捆绑](https://github.com/go-vgo/robotgo#binding) * [要求](https://github.com/go-vgo/robotgo#requirements) * [安装](https://github.com/go-vgo/robotgo#installation) * [更新](https://github.com/go-vgo/robotgo#update) * [例子](https://github.com/go-vgo/robotgo#examples) * [交叉编译](https://github.com/go-vgo/robotgo#crosscompiling) * [作者](https://github.com/go-vgo/robotgo#authors) * [计划](https://github.com/go-vgo/robotgo#plans) * [捐](https://github.com/go-vgo/robotgo#donate) * [贡献者](https://github.com/go-vgo/robotgo#contributors) * [执照](https://github.com/go-vgo/robotgo#license) ## 文档 * [GoDoc](https://godoc.org/github.com/go-vgo/robotgo) * [API 文档](https://github.com/go-vgo/robotgo/blob/master/docs/doc.md) (已弃用,未更新) ## 捆绑: [ADB](https://github.com/vcaesar/adb),打包android adb API。 [Robotn](https://github.com/vcaesar/robotn),绑定JavaScript等,支持更多语言。 ## 要求: 现在,请确保`Golang, GCC`在安装 RobotGo 之前已正确安装。 ### 全部: ``` Golang GCC ``` #### 对于苹果操作系统: Xcode 命令行工具(和隐私设置:[#277](https://github.com/go-vgo/robotgo/issues/277)) ``` xcode-select --install ``` #### 对于 Windows: [MinGW-w64](https://sourceforge.net/projects/mingw-w64/files)(推荐使用) 下载Mingw,然后设置系统环境变量`C:\mingw64\bin`到Path。 [设置环境变量以从命令行运行 GCC](https://www.youtube.com/results?search_query=Set+environment+variables+to+run+GCC+from+command+line)。 `Or the other GCC (But you should compile the "libpng" with yourself when use the bitmap.)` #### 对于其他一切: ``` GCC X11 with the XTest extension (the Xtst library) "Bitmap": libpng (Just used by bitmap) "Event": xcb, xkb, libxkbcommon "Clipboard": xsel xclip ``` ##### Ubuntu: ```yaml notranslate position-relative overflow-auto # gcc sudo apt install gcc libc6-dev # x11 sudo apt install libx11-dev xorg-dev libxtst-dev # Bitmap sudo apt install libpng++-dev # Hook sudo apt install xcb libxcb-xkb-dev x11-xkb-utils libx11-xcb-dev libxkbcommon-x11-dev libxkbcommon-dev # Clipboard sudo apt install xsel xclip ``` ##### 软呢帽: ```yaml notranslate position-relative overflow-auto sudo dnf install libXtst-devel # Bitmap sudo dnf install libpng-devel # Hook sudo dnf install libxkbcommon-devel libxkbcommon-x11-devel xorg-x11-xkb-utils-devel # Clipboard sudo dnf install xsel xclip ``` ## 安装: 使用 Go 模块支持(Go 1.11+),只需导入: ```go notranslate position-relative overflow-auto import "github.com/go-vgo/robotgo" ``` 否则,要安装 robotgo 包,请运行命令: ``` go get github.com/go-vgo/robotgo ``` png.h: 没有这样的文件或目录?请参阅[问题/47](https://github.com/go-vgo/robotgo/issues/47)。 ## 更新: ``` go get -u github.com/go-vgo/robotgo ``` 注意 go1.10.x C 文件编译缓存问题,[golang #24355](https://github.com/golang/go/issues/24355)。 `go mod vendor`问题,[golang #26366](https://github.com/golang/go/issues/26366)。 ## [例子:](https://github.com/go-vgo/robotgo/blob/master/examples) #### [老鼠](https://github.com/go-vgo/robotgo/blob/master/examples/mouse/main.go) ```go notranslate position-relative overflow-auto package main import ( "github.com/go-vgo/robotgo" ) func main() { robotgo.MouseSleep = 100 robotgo.ScrollDir(10, "up") robotgo.ScrollDir(20, "right") robotgo.Scroll(0, -10) robotgo.Scroll(100, 0) robotgo.MilliSleep(100) robotgo.ScrollSmooth(-10, 6) // robotgo.ScrollRelative(10, -100) robotgo.Move(10, 20) robotgo.MoveRelative(0, -10) robotgo.DragSmooth(10, 10) robotgo.Click("wheelRight") robotgo.Click("left", true) robotgo.MoveSmooth(100, 200, 1.0, 10.0) robotgo.Toggle("left") robotgo.Toggle("left", "up") } ``` #### [键盘](https://github.com/go-vgo/robotgo/blob/master/examples/key/main.go) ```go notranslate position-relative overflow-auto package main import ( "fmt" "github.com/go-vgo/robotgo" ) func main() { robotgo.TypeStr("Hello World") robotgo.TypeStr("だんしゃり", 0, 1) // robotgo.TypeStr("テストする") robotgo.TypeStr("Hi, Seattle space needle, Golden gate bridge, One world trade center.") robotgo.TypeStr("Hi galaxy, hi stars, hi MT.Rainier, hi sea. こんにちは世界.") robotgo.Sleep(1) // ustr := uint32(robotgo.CharCodeAt("Test", 0)) // robotgo.UnicodeType(ustr) robotgo.KeySleep = 100 robotgo.KeyTap("enter") // robotgo.TypeStr("en") robotgo.KeyTap("i", "alt", "cmd") arr := []string{"alt", "cmd"} robotgo.KeyTap("i", arr) robotgo.MilliSleep(100) robotgo.KeyToggle("a") robotgo.KeyToggle("a", "up") robotgo.WriteAll("Test") text, err := robotgo.ReadAll() if err == nil { fmt.Println(text) } } ``` #### [屏幕](https://github.com/go-vgo/robotgo/blob/master/examples/screen/main.go) ```go notranslate position-relative overflow-auto package main import ( "fmt" "github.com/go-vgo/robotgo" "github.com/vcaesar/imgo" ) func main() { x, y := robotgo.Location() fmt.Println("pos: ", x, y) color := robotgo.GetPixelColor(100, 200) fmt.Println("color---- ", color) sx, sy := robotgo.GetScreenSize() fmt.Println("get screen size: ", sx, sy) bit := robotgo.CaptureScreen(10, 10, 30, 30) defer robotgo.FreeBitmap(bit) img := robotgo.ToImage(bit) imgo.Save("test.png", img) num := robotgo.DisplaysNum() for i := 0; i < num; i++ { robotgo.DisplayID = i img1 := robotgo.CaptureImg() path1 := "save_" + strconv.Itoa(i) robotgo.Save(img1, path1+".png") robotgo.SaveJpeg(img1, path1+".jpeg", 50) img2 := robotgo.CaptureImg(10, 10, 20, 20) robotgo.Save(img2, "test_"+strconv.Itoa(i)+".png") } } ``` #### [位图](https://github.com/vcaesar/bitmap/blob/main/examples/main.go) ```go notranslate position-relative overflow-auto package main import ( "fmt" "github.com/go-vgo/robotgo" "github.com/vcaesar/bitmap" ) func main() { bit := robotgo.CaptureScreen(10, 20, 30, 40) // use `defer robotgo.FreeBitmap(bit)` to free the bitmap defer robotgo.FreeBitmap(bit) fmt.Println("bitmap...", bit) img := robotgo.ToImage(bit) // robotgo.SavePng(img, "test_1.png") robotgo.Save(img, "test_1.png") bit2 := robotgo.ToCBitmap(robotgo.ImgToBitmap(img)) fx, fy := bitmap.Find(bit2) fmt.Println("FindBitmap------ ", fx, fy) robotgo.Move(fx, fy) arr := bitmap.FindAll(bit2) fmt.Println("Find all bitmap: ", arr) fx, fy = bitmap.Find(bit) fmt.Println("FindBitmap------ ", fx, fy) bitmap.Save(bit, "test.png") } ``` #### [打开简历](https://github.com/vcaesar/gcv) ```go notranslate position-relative overflow-auto package main import ( "fmt" "math/rand" "github.com/go-vgo/robotgo" "github.com/vcaesar/gcv" "github.com/vcaesar/bitmap" ) func main() { opencv() } func opencv() { name := "test.png" name1 := "test_001.png" robotgo.SaveCapture(name1, 10, 10, 30, 30) robotgo.SaveCapture(name) fmt.Print("gcv find image: ") fmt.Println(gcv.FindImgFile(name1, name)) fmt.Println(gcv.FindAllImgFile(name1, name)) bit := bitmap.Open(name1) defer robotgo.FreeBitmap(bit) fmt.Print("find bitmap: ") fmt.Println(bitmap.Find(bit)) // bit0 := robotgo.CaptureScreen() // img := robotgo.ToImage(bit0) // bit1 := robotgo.CaptureScreen(10, 10, 30, 30) // img1 := robotgo.ToImage(bit1) // defer robotgo.FreeBitmapArr(bit0, bit1) img := robotgo.CaptureImg() img1 := robotgo.CaptureImg(10, 10, 30, 30) fmt.Print("gcv find image: ") fmt.Println(gcv.FindImg(img1, img)) fmt.Println() res := gcv.FindAllImg(img1, img) fmt.Println(res[0].TopLeft.Y, res[0].Rects.TopLeft.X, res) x, y := res[0].TopLeft.X, res[0].TopLeft.Y robotgo.Move(x, y-rand.Intn(5)) robotgo.MilliSleep(100) robotgo.Click() res = gcv.FindAll(img1, img) // use find template and sift fmt.Println("find all: ", res) res1 := gcv.Find(img1, img) fmt.Println("find: ", res1) img2, _, _ := robotgo.DecodeImg("test_001.png") x, y = gcv.FindX(img2, img) fmt.Println(x, y) } ``` #### [事件](https://github.com/robotn/gohook/blob/master/examples/main.go) ```go notranslate position-relative overflow-auto package main import ( "fmt" // "github.com/go-vgo/robotgo" hook "github.com/robotn/gohook" ) func main() { add() low() event() } func add() { fmt.Println("--- Please press ctrl + shift + q to stop hook ---") hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) { fmt.Println("ctrl-shift-q") hook.End() }) fmt.Println("--- Please press w---") hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) { fmt.Println("w") }) s := hook.Start() <-hook.Process(s) } func low() { evChan := hook.Start() defer hook.End() for ev := range evChan { fmt.Println("hook: ", ev) } } func event() { ok := hook.AddEvents("q", "ctrl", "shift") if ok { fmt.Println("add events...") } keve := hook.AddEvent("k") if keve { fmt.Println("you press... ", "k") } mleft := hook.AddEvent("mleft") if mleft { fmt.Println("you press... ", "mouse left button") } } ``` #### [窗户](https://github.com/go-vgo/robotgo/blob/master/examples/window/main.go) ```go notranslate position-relative overflow-auto package main import ( "fmt" "github.com/go-vgo/robotgo" ) func main() { fpid, err := robotgo.FindIds("Google") if err == nil { fmt.Println("pids... ", fpid) if len(fpid) > 0 { robotgo.TypeStr("Hi galaxy!", fpid[0]) robotgo.KeyTap("a", fpid[0], "cmd") robotgo.KeyToggle("a", fpid[0]) robotgo.KeyToggle("a", fpid[0], "up") robotgo.ActivePid(fpid[0]) robotgo.Kill(fpid[0]) } } robotgo.ActiveName("chrome") isExist, err := robotgo.PidExists(100) if err == nil && isExist { fmt.Println("pid exists is", isExist) robotgo.Kill(100) } abool := robotgo.Alert("test", "robotgo") if abool { fmt.Println("ok@@@ ", "ok") } title := robotgo.GetTitle() fmt.Println("title@@@ ", title) } ``` ## 交叉编译 ##### Windows64 到 Windows32 ```go notranslate position-relative overflow-auto SET CGO_ENABLED=1 SET GOARCH=386 go build main.go ``` #### 窗户以外 安装要求(Ubuntu,仅供位图使用。): ```shell notranslate position-relative overflow-auto sudo apt install gcc-multilib sudo apt install gcc-mingw-w64 # fix err: zlib.h: No such file or directory sudo apt install libz-mingw-w64-dev ``` 构建二进制文件: ```go notranslate position-relative overflow-auto GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ go build -x ./ ``` ``` // CC=mingw-w64\x86_64-7.2.0-win32-seh-rt_v5-rev1\mingw64\bin\gcc.exe // CXX=mingw-w64\x86_64-7.2.0-win32-seh-rt_v5-rev1\mingw64\bin\g++.exe ``` 最后修改:2022 年 12 月 25 日 © 允许规范转载 打赏 赞赏作者 微信 赞 1 如果觉得我的文章对你有用,请随意赞赏