Golang 跨平台自动化系统,控制键盘鼠标位图和读取屏幕,窗口句柄以及全局事件监听
RobotGo 支持 Mac, Windows, and Linux(X11).
提 Issues 请到 Github, 便于统一管理和即时更新
Contents
- Docs
- Binding
- Requirements
- Installation
- Update
- Examples
- Cross-Compiling
- Authors
- Plans
- Donate
- Contributors
- License
Docs
Binding:
Robotn, binding JavaScript and other, support more language.
Requirements:
环境要求:
在安装 RobotGo 之前, 请确保 Golang、GCC
被正确安装
ALL:
Golang
GCC
For Mac OS X:
Xcode Command Line Tools
For Windows:
MinGW-w64 (推荐使用) or other GCC
For everything else (Linux 等其他系统):
GCC, libpng
X11 with the XTest extension (also known as the Xtst library)
事件:
xcb, xkb, libxkbcommon
Ubuntu:
sudo apt install gcc libc6-dev sudo apt install libx11-dev xorg-dev libxtst-dev libpng++-dev sudo apt install xcb libxcb-xkb-dev x11-xkb-utils libx11-xcb-dev libxkbcommon-x11-dev sudo apt install libxkbcommon-dev sudo apt install xsel xclip
Fedora:
sudo dnf install libxkbcommon-devel libXtst-devel libxkbcommon-x11-devel xorg-x11-xkb-utils-devel sudo dnf install libpng-devel sudo dnf install xsel xclip
Installation:
go get github.com/go-vgo/robotgo
It's that easy!
png.h: No such file or directory? Please see issues/47.
Update:
go get -u github.com/go-vgo/robotgo
注意 go1.10.x C 文件编译缓存问题, golang #24355.
go mod vendor
problem, golang #26366.
Examples:
鼠标
package main import ( "github.com/go-vgo/robotgo" ) func main() { robotgo.ScrollMouse(10, "up") robotgo.MouseClick("left", true) robotgo.MoveMouseSmooth(100, 200, 1.0, 100.0) }
键盘
package main import ( "fmt" "github.com/go-vgo/robotgo" ) func main() { robotgo.TypeStr("Hello World. Winter is coming!") robotgo.TypeStr("だんしゃり", 1.0) // robotgo.TypeString("テストする") robotgo.TypeStr("Hi galaxy. こんにちは世界.") robotgo.Sleep(1) // ustr := uint32(robotgo.CharCodeAt("テストする", 0)) // robotgo.UnicodeType(ustr) robotgo.KeyTap("enter") // robotgo.TypeString("en") robotgo.KeyTap("i", "alt", "command") arr := []string{"alt", "command"} robotgo.KeyTap("i", arr) robotgo.WriteAll("テストする") text, err := robotgo.ReadAll() if err == nil { fmt.Println(text) } }
屏幕
package main import ( "fmt" "github.com/go-vgo/robotgo" ) func main() { x, y := robotgo.GetMousePos() fmt.Println("pos: ", x, y) color := robotgo.GetPixelColor(100, 200) fmt.Println("color----", color) }
位图
package main import ( "fmt" "github.com/go-vgo/robotgo" ) func main() { bitmap := robotgo.CaptureScreen(10, 20, 30, 40) // use `defer robotgo.FreeBitmap(bit)` to free the bitmap defer robotgo.FreeBitmap(bitmap) fmt.Println("...", bitmap) fx, fy := robotgo.FindBitmap(bitmap) fmt.Println("FindBitmap------", fx, fy) robotgo.SaveBitmap(bitmap, "test.png") }
事件
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 ---") robotgo.EventHook(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) { fmt.Println("ctrl-shift-q") robotgo.EventEnd() }) fmt.Println("--- Please press w---") robotgo.EventHook(hook.KeyDown, []string{"w"}, func(e hook.Event) { fmt.Println("w") }) s := robotgo.EventStart() <-robotgo.EventProcess(s) } func low() { EvChan := hook.Start() defer hook.End() for ev := range EvChan { fmt.Println("hook: ", ev) } } func event() { ok := robotgo.AddEvents("q", "ctrl", "shift") if ok { fmt.Println("add events...") } keve := robotgo.AddEvent("k") if keve { fmt.Println("you press... ", "k") } mleft := robotgo.AddEvent("mleft") if mleft { fmt.Println("you press... ", "mouse left button") } }
窗口句柄
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.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.ShowAlert("test", "robotgo") if abool { fmt.Println("ok@@@ ", "ok") } title := robotgo.GetTitle() fmt.Println("title@@@ ", title) }
CrossCompiling
Windows64 to win32
SET CGO_ENABLED=1 SET GOARCH=386 go build main.go
Other to windows
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
Authors
Plans
- 更新 Find an image on screen, read pixels from an image
- 更新 Window Handle
- 尝试支持 Android, 也许支持 IOS
Contributors
- See contributors page for full list of contributors.
- See Contribution Guidelines.
License
Robotgo is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.
See LICENSE-APACHE, LICENSE-MIT.