Skip to content

Commit fe0a2e7

Browse files
committed
更改robotgo的引用版本,添加屏幕截图、保存和加载功能
1 parent 0be7f6b commit fe0a2e7

File tree

14 files changed

+274
-44
lines changed

14 files changed

+274
-44
lines changed

Diff for: GokeyLua/Tool/CallGo/CallGo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func showalert(L *lua.LState) int {
3838
// 将第二个参数转换为string类型
3939
msg := L.ToString(2)
4040
// 调用robotgo.ShowAlert函数,弹出一个提示框,返回值为bool类型
41-
abool := robotgo.ShowAlert(title, msg)
41+
abool := robotgo.Alert(title, msg)
4242
// 将返回值转换为lua.LBool类型,并压入栈中
4343
L.Push(lua.LBool(abool))
4444
// 将弹出的Alert窗口置顶

Diff for: GokeyLua/Tool/SuScreen/SuScreen.go

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package SuScreen
2+
3+
import (
4+
KeyTool "GoRobotScript/GokeyLog/Tool"
5+
"fmt"
6+
"github.com/go-vgo/robotgo" // 导入robotgo库
7+
lua "github.com/yuin/gopher-lua" // 导入gopher-lua库
8+
"os"
9+
"path/filepath"
10+
"strconv" // 导入strconv库
11+
"strings" // 导入strings库
12+
)
13+
14+
var SuBitmap = make(map[string]robotgo.CBitmap) /*创建集合 */
15+
16+
func Loader(L *lua.LState) int {
17+
// register functions to the table
18+
mod := L.SetFuncs(L.NewTable(), exports) // 将函数注册到table中
19+
// register other stuff
20+
L.SetField(mod, "name", lua.LString("value")) // 将name字段设置为value
21+
22+
// returns the module
23+
L.Push(mod) // 将mod压入栈中
24+
return 1 // 返回1
25+
}
26+
27+
// 定义一个map类型的变量exports,其中key为string类型,value为lua.LGFunction类型
28+
var exports = map[string]lua.LGFunction{
29+
"CaptureScreen": captureScreen, // 将captureScreen函数注册到exports中
30+
"SaveBitmap": saveBitmap, // 将saveBitmap函数注册到exports中
31+
"ReadBitmap": readBitmap,
32+
"ReadAllBitmap": readAllBitmap,
33+
}
34+
35+
func readAllBitmap(L *lua.LState) int {
36+
path := L.ToString(1)
37+
var files []string
38+
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
39+
if err != nil {
40+
return err
41+
}
42+
if !info.IsDir() {
43+
switch filepath.Ext(path) {
44+
case ".png", ".jpg", "":
45+
files = append(files, strings.TrimSuffix(path, filepath.Ext(path)))
46+
}
47+
}
48+
return nil
49+
})
50+
if err != nil {
51+
panic(err)
52+
}
53+
for _, file := range files {
54+
GreadBitmap(strings.ReplaceAll(file, "\\", "/")) // 把添加到files的反斜杠路径改为正斜杠路径
55+
}
56+
return 0
57+
}
58+
59+
// captureScreen函数接收一个LState类型的参数L,返回一个int类型的值
60+
// 从L中获取第一个参数,转换为table类型
61+
func captureScreen(L *lua.LState) int {
62+
name := L.ToString(1) // 获取第一个参数并转换为string类型
63+
tbl := L.ToTable(2) // 获取第二个参数并转换为table类型
64+
var arr []int // 定义一个int类型的数组
65+
tbl.ForEach(func(i lua.LValue, j lua.LValue) {
66+
// 将table中的值转换为int类型
67+
if num, err := strconv.Atoi(j.String()); err == nil {
68+
arr = append(arr, num) // 将转换后的值添加到数组中
69+
}
70+
})
71+
//bit := robotgo.GoCaptureScreen(arr[0], arr[1], arr[2], arr[3])
72+
SuBitmap[name] = robotgo.CaptureScreen(arr[0], arr[1], arr[2], arr[3]) // 将截图保存到SuBitmap中
73+
L.SetGlobal("SuBitmap", L.NewUserData()) // 将SuBitmap设置为全局变量
74+
return 0 // 返回0
75+
}
76+
77+
// saveBitmap函数接收一个LState类型的参数L,返回一个int类型的值
78+
func saveBitmap(L *lua.LState) int {
79+
haven := false
80+
name := L.ToString(1) // 获取第一个参数并转换为string类型
81+
_, ok := SuBitmap[name]
82+
if ok {
83+
haven = true
84+
//fmt.Println(GetDir(name))
85+
path, _ := GetDir(name)
86+
filename, _ := GetFileName(name)
87+
if L.GetTop() == 1 {
88+
89+
robotgo.Save(robotgo.ToImage(SuBitmap[name]), filepath.Join(path, filename)) // 保存截图
90+
return 0 // 返回0
91+
}
92+
end := strings.ToLower(L.ToString(2))
93+
switch end {
94+
case ".png":
95+
robotgo.SavePng(robotgo.ToImage(SuBitmap[name]), filepath.Join(path, filename)+".png") // 保存为png格式
96+
case ".jpg":
97+
robotgo.SaveJpeg(robotgo.ToImage(SuBitmap[name]), filepath.Join(path, filename)+".jpg") // 保存为jpg格式
98+
default:
99+
robotgo.Save(robotgo.ToImage(SuBitmap[name]), filepath.Join(path, filename)+end) // 保存为默认格式
100+
}
101+
}
102+
103+
L.Push(lua.LBool(haven))
104+
return 1 // 返回1
105+
}
106+
func readBitmap(L *lua.LState) int {
107+
name := L.ToString(1) // 获取第一个参数并转换为string
108+
L.Push(lua.LBool(GreadBitmap(name)))
109+
return 1 // 返回1
110+
}
111+
func GreadBitmap(name string) bool {
112+
haven := false
113+
dir, _ := GetDir("")
114+
end := []string{".png", ".jpg", ""}
115+
var file1 = ""
116+
for _, s := range end {
117+
file1 = filepath.Join(dir, name+s)
118+
if _, err := os.Stat(file1); err == nil {
119+
file1 = filepath.Join(dir, name+s)
120+
break
121+
}
122+
}
123+
124+
println(file1)
125+
img, err := robotgo.Read(file1)
126+
if err != nil {
127+
fmt.Println("Error: ", err.Error()+" "+name) // 提示错误信息
128+
} else {
129+
SuBitmap[name] = robotgo.ImgToCBitmap(img)
130+
haven = true
131+
}
132+
return haven // 返回1
133+
}
134+
135+
func GetDir(name string) (path string, err error) {
136+
scriptpath, err := KeyTool.GetAppPath() // 获取当前应用程序的路径 Get the path of the current application
137+
if err != nil {
138+
return "", err // 如果获取失败,返回错误 Return error if failed to get
139+
}
140+
// 获取文件的父路径
141+
dir := filepath.Dir(name)
142+
path = filepath.Join(scriptpath, dir) // 将应用程序路径与相对路径拼接 Join the application path with the relative path
143+
_, err = os.Stat(path) // 获取脚本文件夹的信息 Get information about the script folder
144+
if os.IsNotExist(err) { // 如果脚本文件夹不存在 If the script folder does not exist
145+
err = os.MkdirAll(path, os.ModePerm) // 创建脚本文件夹 Create the script folder
146+
if err != nil {
147+
return "", err // 如果创建失败,返回错误 Return error if failed to create
148+
}
149+
}
150+
return path, nil // 返回脚本文件夹路径 Return the path of the script folder
151+
}
152+
func GetFileName(name string) (path string, err error) {
153+
_, fileName := filepath.Split(name) // 获取文件名
154+
return fileName, nil // 返回文件名
155+
}
156+
157+
// GetFileEnd GetFileEnd函数接收一个string类型的参数name,返回一个string类型的值
158+
// 仅获取文件拓展名
159+
func GetFileEnd(name string) (path string, err error) {
160+
_, fileName := filepath.Split(name) // 获取文件名
161+
return filepath.Ext(fileName), nil // 返回文件拓展名
162+
}

Diff for: GokeyLua/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
CallGo "GoRobotScript/GokeyLua/Tool/CallGo"
55
SuKey "GoRobotScript/GokeyLua/Tool/SuKey"
6+
SuScreen "GoRobotScript/GokeyLua/Tool/SuScreen"
67
"flag"
78
"fmt"
89
lua "github.com/yuin/gopher-lua"
@@ -20,6 +21,7 @@ func main() {
2021
defer L.Close()
2122
L.PreloadModule("CallGo", CallGo.Loader)
2223
L.PreloadModule("SuKey", SuKey.Loader)
24+
L.PreloadModule("SuScreen", SuScreen.Loader)
2325
scriptpath := flag.String("script", "path", "Script Path")
2426
//scriptpatht := *scriptpath
2527
Simkeyrun := "main"

Diff for: GokeyRun/Tool/Event2Exe.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ func Scriptexe(e *hook.Event) string {
4141
return fmt.Sprintf("Event: {Kind: KeyDown, Rawcode: %v, Keychar: %v}", e.Rawcode, e.Keychar)
4242
case hook.MouseUp:
4343
robotgo.MilliSleep(int(Delay.Milliseconds()))
44-
robotgo.MouseToggle("up", string(e.Button))
44+
robotgo.MouseUp(string(e.Button)) //.MouseToggle("up", string(e.Button))
4545
return fmt.Sprintf("Event: {Kind: MouseUp, Button: %v, X: %v, Y: %v, Clicks: %v}", e.Button, e.X, e.Y, e.Clicks)
4646
case hook.MouseHold:
4747
robotgo.MilliSleep(int(Delay.Milliseconds()))
4848
return fmt.Sprintf("Event: {Kind: MouseHold, Button: %v, X: %v, Y: %v, Clicks: %v}", e.Button, e.X, e.Y, e.Clicks)
4949
case hook.MouseDown:
5050
robotgo.MilliSleep(int(Delay.Milliseconds()))
51-
robotgo.MouseToggle("down", string(e.Button))
51+
robotgo.MouseDown(string(e.Button)) //MouseToggle("down", string(e.Button))
5252
return fmt.Sprintf("Event: {Kind: MouseDown, Button: %v, X: %v, Y: %v, Clicks: %v}", e.Button, e.X, e.Y, e.Clicks)
5353
case hook.MouseMove:
5454

Diff for: bin/GokeyLog.exe

-718 KB
Binary file not shown.

Diff for: bin/GokeyLua.exe

-1.56 MB
Binary file not shown.

Diff for: bin/GokeyRun.exe

-1.59 MB
Binary file not shown.

Diff for: bin/main.lua

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
print("Hello World!")
22
local m = require("CallGo")
33
local key = require("SuKey")
4-
key.KeyTap({"esc", "ctrl", "shift"})
4+
local scr = require("SuScreen")
5+
scr.CaptureScreen("sl/s",{0,0,1920,1080})
6+
print(scr.SaveBitmap("sl/s",".png"))
7+
print(scr.ReadBitmap("sl/s1"))
8+
--SuBitmap["sl/s3"]=SuBitmap["sl/s1"]
9+
print(scr.SaveBitmap("sl/s3",".png"))
10+
scr.ReadAllBitmap("sl")
11+
--[[key.KeyTap({"esc", "ctrl", "shift"})
512
key.TypeStr("0000000")
613
m.myfunc()
714
print(m.name)
815
print(m.showalert("AreU","Quit"))
16+
]]
17+
print(m.showalert("AreU","Quit"))
918
function max(num1, num2)
1019

1120
if (num1 > num2) then

Diff for: bin/sl/s.png

154 KB
Loading

Diff for: bin/sl/s.txt

Whitespace-only changes.

Diff for: bin/sl/s1.png

184 KB
Loading

Diff for: bin/sl/t

Whitespace-only changes.

Diff for: go.mod

+16-11
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@ module GoRobotScript
33
go 1.19
44

55
require (
6-
github.com/go-vgo/robotgo v0.100.10
7-
github.com/robotn/gohook v0.40.0
8-
github.com/yuin/gopher-lua v1.1.0
6+
github.com/go-vgo/robotgo v1.0.0-rc1.0.20230409194401-8ce3a5dff00d
7+
github.com/robotn/gohook v0.40.1-0.20230303013706-885d45d4bd6a
8+
github.com/yuin/gopher-lua v1.1.1-0.20230406101634-d212719c8304
99
)
1010

1111
require (
12-
github.com/StackExchange/wmi v1.2.1 // indirect
12+
github.com/gen2brain/shm v0.0.0-20221026125803-c33c9e32b1c8 // indirect
1313
github.com/go-ole/go-ole v1.2.6 // indirect
14+
github.com/jezek/xgb v1.1.0 // indirect
15+
github.com/kbinani/screenshot v0.0.0-20210720154843-7d3a670d8329 // indirect
16+
github.com/lufia/plan9stats v0.0.0-20230110061619-bbe2e5e100de // indirect
1417
github.com/lxn/win v0.0.0-20210218163916-a377121e959e // indirect
1518
github.com/otiai10/gosseract v2.2.1+incompatible // indirect
19+
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
1620
github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934 // indirect
1721
github.com/robotn/xgbutil v0.0.0-20190912154524-c861d6f87770 // indirect
18-
github.com/shirou/gopsutil v3.21.10+incompatible // indirect
19-
github.com/tklauser/go-sysconf v0.3.9 // indirect
20-
github.com/tklauser/numcpus v0.3.0 // indirect
21-
github.com/vcaesar/gops v0.21.3 // indirect
22-
github.com/vcaesar/imgo v0.30.0 // indirect
22+
github.com/shirou/gopsutil/v3 v3.23.1 // indirect
23+
github.com/tklauser/go-sysconf v0.3.11 // indirect
24+
github.com/tklauser/numcpus v0.6.0 // indirect
25+
github.com/vcaesar/gops v0.30.1 // indirect
26+
github.com/vcaesar/imgo v0.30.2 // indirect
2327
github.com/vcaesar/keycode v0.10.0 // indirect
2428
github.com/vcaesar/tt v0.20.0 // indirect
25-
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
26-
golang.org/x/sys v0.0.0-20211123173158-ef496fb156ab // indirect
29+
github.com/yusufpapurcu/wmi v1.2.2 // indirect
30+
golang.org/x/image v0.5.0 // indirect
31+
golang.org/x/sys v0.5.0 // indirect
2732
)

0 commit comments

Comments
 (0)