-
Notifications
You must be signed in to change notification settings - Fork 1
/
mouse.go
89 lines (80 loc) · 2.08 KB
/
mouse.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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/go-vgo/robotgo"
)
type MousePos struct {
X int `json:"x"`
Y int `json:"y"`
}
func mouseEventSequence(commands []string) {
for _, command := range commands {
if command == "lclick" {
robotgo.MouseClick("left")
robotgo.MilliSleep(GetInt("mouseDelay"))
}
if command == "rclick" {
robotgo.MouseClick("right")
robotgo.MilliSleep(GetInt("mouseDelay"))
}
if strings.Contains(command, "scroll") {
mouseScrollParse(command)
}
if strings.Contains(command, "pointer") {
mousePointerParse(command)
}
if strings.Contains(command, "offset") {
mouseOffsetParse(command)
}
}
}
func mouseScrollParse(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 mousePointerParse(command string) {
components := strings.Split(command, "::")
posX, err := strconv.Atoi(components[1])
posY, err := strconv.Atoi(components[2])
if err != nil || len(components) < 3 {
fmt.Printf("Command <%s> not formatted properly.\n", command)
return
}
robotgo.MoveMouse(posX, posY)
robotgo.MilliSleep(GetInt("mouseDelay"))
}
func mouseOffsetParse(command string) {
components := strings.Split(command, "::")
offX, err := strconv.Atoi(components[1])
offY, err := strconv.Atoi(components[2])
if err != nil || len(components) < 3 {
fmt.Printf("Command <%s> not formatted properly.\n", command)
return
}
posX, posY := robotgo.GetMousePos()
posX += offX
posY += offY
robotgo.MoveMouse(posX, posY)
robotgo.MilliSleep(GetInt("mouseDelay"))
}
func mouseHold(button string) {
robotgo.MouseToggle("down", button)
}
func mouseRelease(button string) {
robotgo.MouseToggle("up", button)
robotgo.MilliSleep(GetInt("mouseDelay"))
}