01GetCoordinate
获取坐标
package main
import (
"github.com/go-vgo/robotgo"
)
func main() {
// 获取当前鼠标所在的位置
x, y := robotgo.GetMousePos()
println(`x:`, x, ` y:`, y)
}
02GetColor
获取坐标颜色
package main
import (
"fmt"
"image/color"
"github.com/kbinani/screenshot"
)
func main() {
// 定义目标坐标
targetX := 100
targetY := 200
// 获取屏幕边界
bounds := screenshot.GetDisplayBounds(0)
width := bounds.Dx()
height := bounds.Dy()
// 捕获屏幕
img, err := screenshot.Capture(0, 0, width, height)
if err != nil {
panic(err)
}
// 获取目标坐标的颜色
c := img.At(targetX, targetY).(color.RGBA)
// 打印颜色信息
fmt.Printf("坐标(%d, %d)的颜色为 RGB(%d, %d, %d)\n", targetX, targetY, c.R, c.G, c.B)
}
03 操作案例
package main
import (
"fmt"
"github.com/go-vgo/robotgo"
"math/rand"
"time"
)
func main() {
//两个随机数的代码示例,一个生成3秒至6秒之间的随机数,另一个生成70毫秒至200毫秒之间的随机数:
min1 := 3 // 最小休息时间(秒)
max1 := 10 // 最大休息时间(秒)
min2 := 70 // 最小休息时间(毫秒)
max2 := 200 // 最大休息时间(毫秒)
rand.Seed(time.Now().UnixNano())
// 生成随机的休息时间(秒)
sleepTime1 := rand.Intn(max1-min1+1) + min1
fmt.Printf("开始休息 %d 秒钟...\n", sleepTime1)
time.Sleep(time.Duration(sleepTime1) * time.Second)
fmt.Println("第一个休息结束!")
// 生成随机的休息时间(毫秒)
sleepTime2 := rand.Intn(max2-min2+1) + min2
fmt.Printf("开始休息 %d 毫秒...\n", sleepTime2)
time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
fmt.Println("第二个休息结束!")
// 获取当前鼠标所在的位置
x, y := robotgo.GetMousePos()
println(`x:`, x, ` y:`, y)
// 第一个客户端角色 坐标,登入游戏
time.Sleep(time.Duration(sleepTime1) * time.Second)
robotgo.MoveClick(72, 448, `left`, true)
time.Sleep(time.Duration(sleepTime1) * time.Second)
robotgo.MoveClick(72, 448, `left`, true)
time.Sleep(time.Duration(sleepTime1) * time.Second)
robotgo.KeyTap("space")
time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
// 第二个客户端角色 坐标,登入游戏
time.Sleep(time.Duration(sleepTime1) * time.Second)
robotgo.MoveClick(868, 434, `left`, true)
time.Sleep(time.Duration(sleepTime1) * time.Second)
robotgo.MoveClick(868, 434, `left`, true)
time.Sleep(time.Duration(sleepTime1) * time.Second)
robotgo.KeyTap("space")
time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
// 第一个客户端角色 坐标,开启同步
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
robotgo.MoveClick(401, 451, `left`, true)
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
robotgo.KeyTap("space")
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
robotgo.KeyTap("f3", "alt")
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
fmt.Println("第一个客户端角色 坐标,开启同步")
// 第二个客户端角色 坐标,开启同步
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
robotgo.MoveClick(1219, 458, `left`, true)
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
robotgo.KeyTap("space")
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
robotgo.KeyTap("f3", "alt")
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
fmt.Println("第二个客户端角色 坐标,开启同步")
// 第一个客户端角色 坐标,开启同步
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
robotgo.MoveClick(401, 451, `left`, true)
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
robotgo.KeyTap("space")
time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
// 模拟按下end
robotgo.KeyDown("end")
// 延迟1秒钟
time.Sleep(1 * time.Second)
// 松开end
robotgo.KeyUp("end")
time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
// 模拟按下end
robotgo.KeyDown("pagedown")
// 延迟9秒钟
time.Sleep(9 * time.Second)
// 松开end
robotgo.KeyUp("pagedown")
time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
robotgo.KeyTap("delete")
time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
robotgo.KeyTap("delete")
time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
robotgo.KeyTap("delete")
// 模拟按下Delete键
robotgo.KeyToggle("delete", "down")
// 延迟0.5秒
time.Sleep(500 * time.Millisecond)
// 松开Delete键
robotgo.KeyToggle("delete", "up")
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
// 模拟按下end键
robotgo.KeyToggle("home", "down")
// 延迟0.8秒
time.Sleep(800 * time.Millisecond)
// 松开end键
robotgo.KeyToggle("home", "up")
time.Sleep(time.Duration(sleepTime1) * time.Millisecond)
//进入副本
//robotgo.KeyTap("space")
//time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
//robotgo.KeyTap("space")
//time.Sleep(time.Duration(sleepTime2) * time.Millisecond)
}