-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (90 loc) · 2.14 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
_ "embed"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"text/template"
"time"
hook "github.com/robotn/gohook"
)
type Export struct {
Code string
}
//go:embed template.go.tmpl
var tmpl string
func main() {
evChan := hook.Start()
defer hook.End()
tmpCode := []string{}
f, err := os.Create(fmt.Sprintf("%d.go", time.Now().Unix()))
if err != nil {
panic(err)
}
defer f.Close()
c := make(chan os.Signal, 5)
signal.Notify(c, os.Interrupt, syscall.SIGTERM|syscall.SIGABRT)
t := template.Must(template.New("template.go.tmpl").Parse(tmpl))
go func() {
<-c
err := t.Execute(f, Export{Code: strings.Join(tmpCode, "\n")})
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}()
prevTime, currTime := time.Now(), time.Now()
tm := int64(0)
addSleep := func() {
tm = currTime.Sub(prevTime).Abs().Milliseconds()
if int(tm) != 0 {
tmpCode = append(tmpCode, "\t\n")
tmpCode = append(tmpCode, fmt.Sprintf("\ttime.Sleep(time.Millisecond*%d)", tm))
}
}
pressed := map[string]bool{}
allPressed := func() string {
out := ""
for k, v := range pressed {
if v {
out += "\"" + k + "\","
}
}
if len(out) > 0 {
return out[:len(out)-1]
}
return out
}
for ev := range evChan {
if ev.Kind == hook.KeyDown {
pressed[keyNames[uint32(ev.Rawcode)]] = true
// addSleep()
// fmt.Println(keyNames[uint32(ev.Rawcode)], string(ev.Keychar), ev.Rawcode, ev.Mask)
currTime = ev.When
prevTime = currTime
} else if ev.Kind == hook.KeyUp {
currTime = ev.When
addSleep()
tmpCode = append(tmpCode, fmt.Sprintf("\trobotgo.KeyTap(%s)", allPressed()))
pressed[keyNames[uint32(ev.Rawcode)]] = false
prevTime = currTime
} else if ev.Kind == hook.MouseDown {
currTime = ev.When
addSleep()
tmpCode = append(tmpCode, fmt.Sprintf("\trobotgo.Move(%d,%d)", ev.X, ev.Y))
button := "left"
if ev.Button == 1 {
button = "left"
} else if ev.Button == 2 {
button = "middle"
} else if ev.Button == 3 {
button = "right"
}
tmpCode = append(tmpCode, fmt.Sprintf("\trobotgo.Click(\"%s\")", button))
// fmt.Println(ev.Button, ev.X, ev.Y)
prevTime = currTime
}
}
}