forked from pibigstar/go-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.go
88 lines (76 loc) · 1.46 KB
/
robot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"time"
"github.com/go-vgo/robotgo"
)
// 需要安装 MinGW
// http://store.pibigstar.com/mingw-w64-install.exe
func main() {
go ListenScreen()
go ListenKeywords()
ShowMessage()
time.Sleep(10 * time.Second)
// 模拟按键
PressKeywords()
// 杀死进程
FindAndKillProcess("robot")
}
// 监听屏幕
func ListenScreen() {
ticker := time.NewTicker(time.Second * 2)
for {
select {
case <-ticker.C:
bitmap := robotgo.CaptureScreen(10, 20, 30, 40)
robotgo.FindBitmap(bitmap)
robotgo.SaveBitmap(bitmap, "test.png")
}
}
}
// 监听键盘事件
func ListenKeywords() {
keywords := []string{"A", "B", "C", "D"}
for _, key := range keywords {
go func(key string) {
ok := robotgo.AddEvent(key)
if ok {
fmt.Printf("press: %s \n", key)
}
}(key)
}
}
// 模拟键盘事件
func PressKeywords() {
// H
robotgo.KeyTap("H")
// 回车
robotgo.KeyTap("enter")
// 键入文字
robotgo.TypeStr("Hello World")
// 关掉程序
robotgo.KeyTap("c", "ctrl")
}
// 弹窗
func ShowMessage() {
btMsg := robotgo.ShowAlert("Alarm", "Hello Pibigstar", "Success", "Close")
if btMsg == 0 {
fmt.Println("确定")
} else {
fmt.Println("取消")
}
}
// 找到并杀死进程
func FindAndKillProcess(name string) {
ids, err := robotgo.FindIds(name)
if err != nil {
fmt.Println(err.Error())
return
}
for _, id := range ids {
err := robotgo.Kill(id)
if err == nil {
fmt.Printf("杀死进程: %d", id)
}
}
}