forked from shibukawa/gotomation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse_darwin.go
143 lines (118 loc) · 3.9 KB
/
mouse_darwin.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
/*
Copyright 2017, Yoshiki Shibukawa
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// build! darwin
package gotomation
/*
#cgo LDFLAGS: -framework Carbon
#include <ApplicationServices/ApplicationServices.h>
// to avoid this: https://github.com/golang/go/issues/975
CGEventRef createScrollEvent(int32_t x, int32_t y) {
return CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine, 2, x, y);
}
*/
import "C"
import "time"
type Mouse struct{}
func rawMousePos() C.CGPoint {
event := C.CGEventCreate(nil)
defer C.CFRelease(C.CFTypeRef(event))
return C.CGEventGetLocation(event)
}
func (m *Mouse) GetPosition() (x, y int) {
point := rawMousePos()
x = int(point.x)
y = int(point.y)
return
}
func calculateDeltas(event *C.CGEventRef, x, y int) {
pos := rawMousePos()
C.CGEventSetIntegerValueField(*event, C.kCGMouseEventDeltaX, C.int64_t(x)-C.int64_t(pos.x))
C.CGEventSetIntegerValueField(*event, C.kCGMouseEventDeltaY, C.int64_t(y)-C.int64_t(pos.y))
}
func (m *Mouse) MoveQuickly(x, y int) error {
move := C.CGEventCreateMouseEvent(nil, C.kCGEventMouseMoved,
C.CGPointMake((C.CGFloat)(x), (C.CGFloat)(y)),
C.kCGMouseButtonLeft)
defer C.CFRelease(C.CFTypeRef(move))
calculateDeltas(&move, x, y)
C.CGEventPost(C.kCGSessionEventTap, move)
return nil
}
func mouseType(down bool, button MouseButton) (mouseType C.CGEventType) {
if down {
switch button {
case MouseLeft:
mouseType = C.kCGEventLeftMouseDown
case MouseRight:
mouseType = C.kCGEventRightMouseDown
case MouseCenter:
mouseType = C.kCGEventOtherMouseDown
}
} else {
switch button {
case MouseLeft:
mouseType = C.kCGEventLeftMouseUp
case MouseRight:
mouseType = C.kCGEventRightMouseUp
case MouseCenter:
mouseType = C.kCGEventOtherMouseUp
}
}
return
}
func mouseToggleButton(down bool, button MouseButton) {
event := C.CGEventCreateMouseEvent(nil, mouseType(down, button), rawMousePos(), (C.CGMouseButton)(button))
defer C.CFRelease(C.CFTypeRef(event))
C.CGEventPost(C.kCGSessionEventTap, event)
}
func (m *Mouse) ClickWith(button MouseButton) error {
mouseToggleButton(true, button)
time.Sleep(time.Millisecond * 10)
mouseToggleButton(false, button)
time.Sleep(time.Millisecond * 10)
return nil
}
func (m *Mouse) DoubleClickWith(button MouseButton) error {
event := C.CGEventCreateMouseEvent(nil, mouseType(true, MouseLeft), rawMousePos(), C.kCGMouseButtonLeft)
defer C.CFRelease(C.CFTypeRef(event))
C.CGEventSetIntegerValueField(event, C.kCGMouseEventClickState, 2)
C.CGEventPost(C.kCGHIDEventTap, event)
C.CGEventSetType(event, mouseType(false, MouseLeft))
C.CGEventPost(C.kCGHIDEventTap, event)
time.Sleep(time.Millisecond * 100)
return nil
}
func (m *Mouse) ScrollQuickly(x, y int) error {
event := C.createScrollEvent(C.int32_t(x), C.int32_t(y))
defer C.CFRelease(C.CFTypeRef(event))
C.CGEventPost(C.kCGHIDEventTap, event)
return nil
}
func (m *Mouse) DragWith(button MouseButton, x, y int) error {
var dragType C.CGEventType
switch button {
case MouseLeft:
dragType = C.kCGEventLeftMouseDragged
case MouseRight:
dragType = C.kCGEventRightMouseDragged
case MouseCenter:
dragType = C.kCGEventOtherMouseDragged
}
point := C.CGPointMake((C.CGFloat)(x), (C.CGFloat)(y))
drag := C.CGEventCreateMouseEvent(nil, dragType, point, (C.CGMouseButton)(button))
defer C.CFRelease(C.CFTypeRef(drag))
calculateDeltas(&drag, x, y)
C.CGEventPost(C.kCGSessionEventTap, drag)
time.Sleep(time.Millisecond * 100)
return nil
}