-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputDispatcher.go
201 lines (182 loc) · 5.2 KB
/
InputDispatcher.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
package main
import (
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/go-vgo/robotgo"
)
type InputRequest struct {
UUID string `json:"uuid"`
InputAction Action `json:"inputAction"`
}
type InputDispatcher struct {
ExecuteTimer *time.Timer
InputAction Action
Sustain bool
Running bool
MouseActive map[string]bool
KeyActive map[string]bool
mutex *sync.Mutex
}
func (inputDispatcher *InputDispatcher) startKeyboardExecute() {
for _, command := range inputDispatcher.InputAction.Commands {
keyStr := convertShortPanelKeyStr(command)
inputDispatcher.mutex.Lock()
if inputDispatcher.KeyActive[keyStr] != true {
keyHold(keyStr)
inputDispatcher.KeyActive[keyStr] = true
}
inputDispatcher.mutex.Unlock()
}
}
func (inputDispatcher *InputDispatcher) stopKeyboardExecute() {
for _, command := range inputDispatcher.InputAction.Commands {
keyStr := convertShortPanelKeyStr(command)
keyRelease(keyStr)
inputDispatcher.mutex.Lock()
inputDispatcher.KeyActive[keyStr] = false
inputDispatcher.mutex.Unlock()
}
}
func (inputDispatcher *InputDispatcher) startMouseExecute() {
for _, command := range inputDispatcher.InputAction.Commands {
if command == "lclick" {
inputDispatcher.mutex.Lock()
if inputDispatcher.MouseActive["left"] != true {
mouseHold("left")
inputDispatcher.MouseActive["left"] = true
inputDispatcher.mutex.Unlock()
robotgo.MilliSleep(GetInt("mouseDelay"))
} else {
inputDispatcher.mutex.Unlock()
}
}
if command == "rclick" {
inputDispatcher.mutex.Lock()
if inputDispatcher.MouseActive["right"] != true {
mouseHold("right")
inputDispatcher.MouseActive["right"] = true
inputDispatcher.mutex.Unlock()
robotgo.MilliSleep(GetInt("mouseDelay"))
} else {
inputDispatcher.mutex.Unlock()
}
}
if strings.Contains(command, "scroll") {
mouseScrollInputExecute(command)
}
if strings.Contains(command, "pointer") {
mousePointerInputExecute(command)
}
}
}
func (inputDispatcher *InputDispatcher) stopMouseExecute() {
for _, command := range inputDispatcher.InputAction.Commands {
if command == "lclick" {
mouseRelease("left")
inputDispatcher.mutex.Lock()
inputDispatcher.MouseActive["left"] = false
inputDispatcher.mutex.Unlock()
}
if command == "rclick" {
mouseRelease("right")
inputDispatcher.mutex.Lock()
inputDispatcher.MouseActive["right"] = false
inputDispatcher.mutex.Unlock()
}
}
}
func mouseScrollInputExecute(command string) {
components := strings.Split(command, "::")
// check scroll direction
direction := "down"
if strings.Contains(command, "up") {
direction = "up"
}
// get magnitude from command string
if magnitude, err := strconv.Atoi(components[1]); err == nil &&
len(components) == 2 {
robotgo.ScrollMouse(magnitude, direction)
robotgo.MilliSleep(GetInt("mouseDelay"))
} else {
fmt.Printf("Command <%s> not formatted properly.\n", command)
}
}
func mousePointerInputExecute(command string) {
components := strings.Split(command, "::")
direction := components[1]
magnitude, err := strconv.Atoi(components[2])
if err != nil || len(components) < 3 {
fmt.Printf("Command <%s> not formatted properly.\n", command)
return
}
if direction == "left" || direction == "up" {
magnitude *= -1
}
var pos MousePos
pos.X, pos.Y = robotgo.GetMousePos()
if direction == "up" || direction == "down" {
robotgo.MoveMouse(pos.X, pos.Y+magnitude)
} else {
robotgo.MoveMouse(pos.X+magnitude, pos.Y)
}
robotgo.MilliSleep(GetInt("mouseDelay"))
}
func (inputDispatcher *InputDispatcher) inputTimeout() {
if inputDispatcher.Sustain {
inputDispatcher.ExecuteTimer.Reset(2 * time.Second)
inputDispatcher.Sustain = false
<-inputDispatcher.ExecuteTimer.C
inputDispatcher.inputTimeout()
} else {
inputDispatcher.stopExecute()
}
}
func (inputDispatcher *InputDispatcher) sustainExecute() {
inputDispatcher.Sustain = true
}
func (inputDispatcher *InputDispatcher) startExecute() {
if inputDispatcher.Running {
return
}
inputDispatcher.ExecuteTimer = time.NewTimer(2 * time.Second) // 3 second timeout
inputDispatcher.Sustain = false
inputDispatcher.Running = true
inputDispatcher.MouseActive = make(map[string]bool)
inputDispatcher.KeyActive = make(map[string]bool)
inputDispatcher.mutex = &sync.Mutex{}
go func(inputDispatcher *InputDispatcher) {
for inputDispatcher.Running {
if inputDispatcher.InputAction.Type == "keyboard" {
inputDispatcher.startKeyboardExecute()
} else {
inputDispatcher.startMouseExecute()
}
}
}(inputDispatcher)
go func(inputDispatcher *InputDispatcher) {
<-inputDispatcher.ExecuteTimer.C
inputDispatcher.inputTimeout()
}(inputDispatcher)
}
func (inputDispatcher *InputDispatcher) stopExecute() {
inputDispatcher.Sustain = false
inputDispatcher.Running = false
inputDispatcher.ExecuteTimer.Stop()
if inputDispatcher.InputAction.Type == "keyboard" {
inputDispatcher.stopKeyboardExecute()
} else {
inputDispatcher.stopMouseExecute()
}
}
func testInputDispatcher() {
robotgo.Sleep(5)
inputDispatcher := &InputDispatcher{}
inputDispatcher.InputAction = Action{
Type: "keyboard",
Commands: []string{"w", "space"}, //"rclick","pointer::down::3","pointer::left::3"},
}
inputDispatcher.startExecute()
}