-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
225 lines (177 loc) · 5.67 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"fmt"
"strconv"
"time"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/go-vgo/robotgo"
hook "github.com/robotn/gohook"
)
const app_version = "v1.1.1"
// Autoclicker Related Functions
func autoClicker(keystate <-chan bool, delay_chan <-chan int64) {
var state bool
var delay int64
// var toggle bool
state = false
for {
// Do a non blocking read on toggle state
// select {
// case toggle_read := <-togglestate:
// toggle = toggle_read
// default:
// }
// fmt.Println(strconv.FormatBool(toggle))
select {
case state_read := <-keystate:
fmt.Println("Value found in keystate channel:")
state = state_read
fmt.Println(strconv.FormatBool(state))
default:
}
// Dont block on delay read
select {
case delay_read := <-delay_chan:
delay = delay_read
default:
}
if state {
robotgo.Click()
}
if delay != 0 {
time.Sleep(time.Duration(delay) * time.Millisecond)
}
}
}
func updateKeyState(keystate chan<- bool, state bool) func(e hook.Event) {
return func(e hook.Event) {
fmt.Println("Keystate Event")
fmt.Println(strconv.FormatBool(state))
if state {
// Dont block to add true's to the queue
select {
case keystate <- state:
default:
}
} else {
// Do block to add false to queue no matter what
fmt.Println("Block adding false to channel")
keystate <- state
}
}
}
func toggleKeyState(keystate chan<- bool, togglestate chan bool) func(e hook.Event) {
return func(e hook.Event) {
var toggle bool
fmt.Println("Toggling Keystate")
// toggle state tracker
toggle = !<-togglestate
togglestate <- toggle
// Send state update to autoclicker
keystate <- toggle
}
}
func eventHooks(keystate chan<- bool, keybind string, is_toggle bool, togglestate chan bool) {
fmt.Println("Registering key events for: " + keybind)
if !is_toggle {
// Register our keystate hooks
hook.Register(hook.KeyDown, []string{keybind}, updateKeyState(keystate, true))
// This seems like it is currently broken
// It never calls this function on a KeyUp event
// https://github.com/robotn/gohook/issues/31
// hook.Register(hook.KeyUp, []string{keybind}, updateKeyState(keystate, false))
} else {
hook.Register(hook.KeyDown, []string{keybind}, toggleKeyState(keystate, togglestate))
}
key_checker := hook.Start()
<-hook.Process(key_checker)
}
// UI Related Functions
func updateKeybind(new_key string, keybind *string) {
keys := [46]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "-", "="}
fmt.Println("Updating keybind...")
for _, key := range keys {
if key == new_key {
*keybind = new_key
fmt.Println("Keybind updated to: " + new_key)
}
}
}
func startEventHooks(keystate chan<- bool, keybind *string, status_label *widget.Label, is_toggle bool, togglestate chan bool) func() {
return func() {
if status_label.Text == "Off" {
go eventHooks(keystate, *keybind, is_toggle, togglestate)
status_label.SetText("On")
} else {
fmt.Println("Hooks already enabled...")
}
}
}
func stopEventHooks(status_label *widget.Label, keystate chan<- bool) func() {
return func() {
if status_label.Text == "On" {
keystate <- false
hook.End()
status_label.SetText("Off")
} else {
fmt.Println("Hooks already disabled...")
}
}
}
func updateTogglestate(togglestate chan<- bool) func(value bool) {
return func(value bool) {
fmt.Println("Toggling state")
togglestate <- value
}
}
func main() {
fmt.Println("Starting autoclicker...")
// Create channels
keystate := make(chan bool, 2)
keystate <- false
delay_chan := make(chan int64, 2)
delay_chan <- 0
// Toggle state tracker
togglestate := make(chan bool, 2)
togglestate <- false
var delay int64
delay = 0
keybind := "p"
// Run our key event thread
go eventHooks(keystate, keybind, true, togglestate)
// Run our autoclicker thread
go autoClicker(keystate, delay_chan)
// Create the window
application := app.New()
window := application.NewWindow("Autoclicker " + app_version)
// Status box
status_label := widget.NewLabel("Status: ")
clicker_status_label := widget.NewLabel("On")
status_text := container.New(layout.NewHBoxLayout(), status_label, clicker_status_label)
// Keybind box
keybind_label := widget.NewLabel("Button to auto-click:")
keybind_key := widget.NewEntry()
keybind_key.SetText(keybind)
keybind_key.OnChanged = func(input string) { updateKeybind(input, &keybind) }
//Toggle Box
// Because KeyUp is broken, setting toggle to the only option
// toggle_box := widget.NewCheck("Toggle instead of hold?", updateTogglestate(togglestate))
keybind_text := container.New(layout.NewHBoxLayout(), keybind_label, keybind_key)
// Delay box
delay_label := widget.NewLabel("Click Delay (Milliseconds):")
delay_key := widget.NewEntry()
delay_key.SetText(strconv.FormatInt(0, 10))
delay_key.OnChanged = func(input string) { delay, _ = strconv.ParseInt(input, 10, 64) }
delay_button := widget.NewButton("Update", func() { delay_chan <- delay })
delay_text := container.New(layout.NewHBoxLayout(), delay_label, delay_key, delay_button)
// Buttons
hooks_start_button := widget.NewButton("Start Autoclicker", startEventHooks(keystate, &keybind, clicker_status_label, true, togglestate))
hooks_stop_button := widget.NewButton("Stop Autoclicker", stopEventHooks(clicker_status_label, keystate))
// Entire window
content := container.New(layout.NewVBoxLayout(), status_text, keybind_text, delay_text, hooks_start_button, hooks_stop_button)
window.SetContent(content)
window.ShowAndRun()
}