-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobotgo.go
294 lines (237 loc) · 6.23 KB
/
robotgo.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*
Package robotgo Go native cross-platform system automation.
Please make sure Golang, GCC is installed correctly before installing RobotGo;
See Requirements:
https://github.com/go-vgo/robotgo#requirements
Installation:
go get -u github.com/go-vgo/robotgo
*/
package robotgo_mouse
/*
//#if defined(IS_MACOSX)
#cgo darwin CFLAGS: -x objective-c -Wno-deprecated-declarations
#cgo darwin LDFLAGS: -framework Cocoa -framework OpenGL -framework IOKit
#cgo darwin LDFLAGS: -framework Carbon -framework CoreFoundation
#cgo darwin LDFLAGS:-L${SRCDIR}/cdeps/mac -lpng -lz
//#elif defined(USE_X11)
// Drop -std=c11
#cgo linux CFLAGS: -I/usr/src
#cgo linux LDFLAGS: -L/usr/src -lpng -lz -lX11 -lXtst -lm
// #cgo linux LDFLAGS: -lX11-xcb -lxcb -lxcb-xkb -lxkbcommon -lxkbcommon-x11
//#endif
// #cgo windows LDFLAGS: -lgdi32 -luser32 -lpng -lz
#cgo windows LDFLAGS: -lgdi32 -luser32
#cgo windows,amd64 LDFLAGS: -L${SRCDIR}/cdeps/win64 -lz
#cgo windows,386 LDFLAGS: -L${SRCDIR}/cdeps/win32 -lz
// #include <AppKit/NSEvent.h>
#include "mouse/goMouse.h"
*/
import "C"
import (
"time"
"unsafe"
)
const (
// Version get the robotgo version
Version = "v0.91.0.1089, MT. Rainier!"
)
var screen_w, screen_h int
func InitScreenSize(w, h int) {
screen_w = w
screen_h = h
}
// GetVersion get the robotgo version
func GetVersion() string {
return Version
}
// MilliSleep sleep tm milli second
func MilliSleep(tm int) {
time.Sleep(time.Duration(tm) * time.Millisecond)
}
/*
.___ ___. ______ __ __ _______. _______
| \/ | / __ \ | | | | / || ____|
| \ / | | | | | | | | | | (----`| |__
| |\/| | | | | | | | | | \ \ | __|
| | | | | `--' | | `--' | .----) | | |____
|__| |__| \______/ \______/ |_______/ |_______|
*/
// CheckMouse check the mouse button
func CheckMouse(btn string) C.MMMouseButton {
// button = args[0].(C.MMMouseButton)
if btn == "left" {
return C.LEFT_BUTTON
}
if btn == "center" {
return C.CENTER_BUTTON
}
if btn == "right" {
return C.RIGHT_BUTTON
}
return C.LEFT_BUTTON
}
// MoveMouse move the mouse
func MoveMouse(x, y int) {
// C.size_t int
Move(x, y)
}
// Move move the mouse
func Move(x, y int) {
cx := C.int32_t(x)
cy := C.int32_t(y)
C.move_mouse(cx, cy)
}
// DragMouse drag the mouse
func DragMouse(x, y int, args ...string) {
Drag(x, y, args...)
}
// Drag drag the mouse
func Drag(x, y int, args ...string) {
var button C.MMMouseButton = C.LEFT_BUTTON
cx := C.int32_t(x)
cy := C.int32_t(y)
if len(args) > 0 {
button = CheckMouse(args[0])
}
C.drag_mouse(cx, cy, button)
}
// DragSmooth drag the mouse smooth
func DragSmooth(x, y int, args ...interface{}) {
MouseToggle("down")
MoveSmooth(x, y, args...)
MouseToggle("up")
}
// MoveMouseSmooth move the mouse smooth,
// moves mouse to x, y human like, with the mouse button up.
func MoveMouseSmooth(x, y int, args ...interface{}) bool {
return MoveSmooth(x, y, args...)
}
// MoveSmooth move the mouse smooth,
// moves mouse to x, y human like, with the mouse button up.
//
// robotgo.MoveSmooth(x, y, w, h int, low, high float64, mouseDelay int)
func MoveSmooth(x, y int, args ...interface{}) bool {
cx := C.int32_t(x)
cy := C.int32_t(y)
cw := C.int32_t(screen_w)
ch := C.int32_t(screen_h)
var (
mouseDelay = 10
low C.double
high C.double
)
if len(args) > 2 {
mouseDelay = args[2].(int)
}
if len(args) > 1 {
low = C.double(args[0].(float64))
high = C.double(args[1].(float64))
} else {
low = 1.0
high = 3.0
}
cbool := C.move_mouse_smooth(cx, cy, cw, ch, low, high, C.int(mouseDelay))
return bool(cbool)
}
// MoveArgs move mose relative args
func MoveArgs(x, y int) (int, int) {
mx, my := GetMousePos()
mx = mx + x
my = my + y
return mx, my
}
// MoveRelative move mose relative
func MoveRelative(x, y int) {
Move(MoveArgs(x, y))
}
// MoveSmoothRelative move mose smooth relative
func MoveSmoothRelative(x, y, w, h int, args ...interface{}) {
mx, my := MoveArgs(x, y)
MoveSmooth(mx, my, args...)
}
// GetMousePos get mouse's portion
func GetMousePos() (int, int) {
pos := C.get_mouse_pos()
x := int(pos.x)
y := int(pos.y)
return x, y
}
// MouseClick click the mouse
//
// robotgo.MouseClick(button string, double bool)
func MouseClick(args ...interface{}) {
Click(args...)
}
// Click click the mouse
//
// robotgo.Click(button string, double bool)
func Click(args ...interface{}) {
var (
button C.MMMouseButton = C.LEFT_BUTTON
double C.bool
)
if len(args) > 0 {
button = CheckMouse(args[0].(string))
}
if len(args) > 1 {
double = C.bool(args[1].(bool))
}
C.mouse_click(button, double)
}
// MoveClick move and click the mouse
//
// robotgo.MoveClick(x, y int, button string, double bool)
func MoveClick(x, y int, args ...interface{}) {
MoveMouse(x, y)
MouseClick(args...)
}
// MovesClick move smooth and click the mouse
func MovesClick(x, y, w, h int, args ...interface{}) {
MoveSmooth(x, y)
MouseClick(args...)
}
// MouseToggle toggle the mouse
func MouseToggle(togKey string, args ...interface{}) int {
var button C.MMMouseButton = C.LEFT_BUTTON
if len(args) > 0 {
button = CheckMouse(args[0].(string))
}
down := C.CString(togKey)
i := C.mouse_toggle(down, button)
C.free(unsafe.Pointer(down))
return int(i)
}
// ScrollMouse scroll the mouse
func ScrollMouse(x int, direction string) {
cx := C.size_t(x)
cy := C.CString(direction)
C.scroll_mouse(cx, cy)
C.free(unsafe.Pointer(cy))
}
// Scroll scroll the mouse with x, y
//
// robotgo.Scroll(x, y, msDelay int)
func Scroll(x, y int, args ...int) {
var msDelay = 10
if len(args) > 0 {
msDelay = args[0]
}
cx := C.int(x)
cy := C.int(y)
cz := C.int(msDelay)
C.scroll(cx, cy, cz)
}
// SetMouseDelay set mouse delay
func SetMouseDelay(delay int) {
cdelay := C.size_t(delay)
C.set_mouse_delay(cdelay)
}