-
Notifications
You must be signed in to change notification settings - Fork 6
/
input.go
197 lines (175 loc) · 4.26 KB
/
input.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
package control
import (
"sync"
"time"
"github.com/ecwid/control/key"
"github.com/ecwid/control/protocol"
"github.com/ecwid/control/protocol/input"
)
const (
MouseNone input.MouseButton = "none"
MouseLeft input.MouseButton = "left"
MouseRight input.MouseButton = "right"
MouseMiddle input.MouseButton = "middle"
MouseBack input.MouseButton = "back"
MouseForward input.MouseButton = "forward"
)
func NewMouse(caller protocol.Caller) Mouse {
return Mouse{
caller: caller,
mutex: &sync.Mutex{},
}
}
type Mouse struct {
caller protocol.Caller
mutex *sync.Mutex
}
func (m Mouse) Move(button input.MouseButton, point Point) error {
return input.DispatchMouseEvent(m.caller, input.DispatchMouseEventArgs{
X: point.X,
Y: point.Y,
Type: "mouseMoved",
Button: button,
})
}
func (m Mouse) Press(button input.MouseButton, point Point) error {
return input.DispatchMouseEvent(m.caller, input.DispatchMouseEventArgs{
X: point.X,
Y: point.Y,
Type: "mousePressed",
Button: button,
ClickCount: 1,
})
}
func (m Mouse) Release(button input.MouseButton, point Point) error {
return input.DispatchMouseEvent(m.caller, input.DispatchMouseEventArgs{
X: point.X,
Y: point.Y,
Type: "mouseReleased",
Button: button,
ClickCount: 1,
})
}
func (m Mouse) Down(button input.MouseButton, point Point) (err error) {
m.mutex.Lock()
defer m.mutex.Unlock()
if err = m.Move(MouseNone, point); err != nil {
return err
}
if err = m.Press(button, point); err != nil {
return err
}
return
}
func (m Mouse) Click(button input.MouseButton, point Point, delay time.Duration) (err error) {
m.mutex.Lock()
defer m.mutex.Unlock()
if err = m.Move(MouseNone, point); err != nil {
return err
}
if err = m.Press(button, point); err != nil {
return err
}
time.Sleep(delay)
if err = m.Release(button, point); err != nil {
return err
}
return
}
type Keyboard struct {
caller protocol.Caller
}
func NewKeyboard(caller protocol.Caller) Keyboard {
return Keyboard{caller: caller}
}
func (k Keyboard) Down(key key.Definition) error {
if key.Text == "" && len(key.Key) == 1 {
key.Text = key.Key
}
return input.DispatchKeyEvent(k.caller, input.DispatchKeyEventArgs{
Type: "keyDown",
WindowsVirtualKeyCode: key.KeyCode,
Code: key.Code,
Key: key.Key,
Text: key.Text,
Location: key.Location,
})
}
func (k Keyboard) Up(key key.Definition) error {
return input.DispatchKeyEvent(k.caller, input.DispatchKeyEventArgs{
Type: "keyUp",
WindowsVirtualKeyCode: key.KeyCode,
Code: key.Code,
Key: key.Key,
})
}
func (k Keyboard) Insert(text string) error {
return input.InsertText(k.caller, input.InsertTextArgs{Text: text})
}
func (k Keyboard) Press(key key.Definition, delay time.Duration) (err error) {
if err = k.Down(key); err != nil {
return err
}
if delay > 0 {
time.Sleep(delay)
}
return k.Up(key)
}
type Touch struct {
caller protocol.Caller
mutex *sync.Mutex
}
func NewTouch(caller protocol.Caller) Touch {
return Touch{
caller: caller,
mutex: &sync.Mutex{},
}
}
func (t Touch) Start(x, y, radiusX, radiusY, force float64) error {
return input.DispatchTouchEvent(t.caller, input.DispatchTouchEventArgs{
Type: "touchStart",
TouchPoints: []*input.TouchPoint{
{
X: x,
Y: y,
RadiusX: radiusX,
RadiusY: radiusY,
Force: force,
},
},
})
}
func (t Touch) Swipe(from, to Point) (err error) {
t.mutex.Lock()
defer t.mutex.Unlock()
if err = t.Start(from.X, from.Y, 1, 1, 1); err != nil {
return err
}
if err = t.Move(to.X, to.Y, 1, 1, 1); err != nil {
return err
}
if err = t.End(); err != nil {
return err
}
return nil
}
func (t Touch) Move(x, y, radiusX, radiusY, force float64) error {
return input.DispatchTouchEvent(t.caller, input.DispatchTouchEventArgs{
Type: "touchMove",
TouchPoints: []*input.TouchPoint{
{
X: x,
Y: y,
RadiusX: radiusX,
RadiusY: radiusY,
Force: force,
},
},
})
}
func (t Touch) End() error {
return input.DispatchTouchEvent(t.caller, input.DispatchTouchEventArgs{
Type: "touchEnd",
TouchPoints: []*input.TouchPoint{},
})
}