-
-
Notifications
You must be signed in to change notification settings - Fork 352
/
input.go
457 lines (375 loc) · 10.2 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package rod
import (
"fmt"
"sync"
"github.com/go-rod/rod/lib/input"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/gson"
)
// Keyboard represents the keyboard on a page, it's always related the main frame.
type Keyboard struct {
sync.Mutex
page *Page
// pressed keys must be released before it can be pressed again
pressed map[input.Key]struct{}
}
func (p *Page) newKeyboard() *Page {
p.Keyboard = &Keyboard{page: p, pressed: map[input.Key]struct{}{}}
return p
}
func (k *Keyboard) getModifiers() int {
k.Lock()
defer k.Unlock()
return k.modifiers()
}
func (k *Keyboard) modifiers() int {
ms := 0
for key := range k.pressed {
ms |= key.Modifier()
}
return ms
}
// Press the key down.
// To input characters that are not on the keyboard, such as Chinese or Japanese, you should
// use method like [Page.InsertText].
func (k *Keyboard) Press(key input.Key) error {
defer k.page.tryTrace(TraceTypeInput, "press key: "+key.Info().Code)()
k.page.browser.trySlowMotion()
k.Lock()
defer k.Unlock()
k.pressed[key] = struct{}{}
return key.Encode(proto.InputDispatchKeyEventTypeKeyDown, k.modifiers()).Call(k.page)
}
// Release the key.
func (k *Keyboard) Release(key input.Key) error {
defer k.page.tryTrace(TraceTypeInput, "release key: "+key.Info().Code)()
k.Lock()
defer k.Unlock()
if _, has := k.pressed[key]; !has {
return nil
}
delete(k.pressed, key)
return key.Encode(proto.InputDispatchKeyEventTypeKeyUp, k.modifiers()).Call(k.page)
}
// Type releases the key after the press.
func (k *Keyboard) Type(keys ...input.Key) (err error) {
for _, key := range keys {
err = k.Press(key)
if err != nil {
return
}
err = k.Release(key)
if err != nil {
return
}
}
return
}
// KeyActionType enum.
type KeyActionType int
// KeyActionTypes.
const (
KeyActionPress KeyActionType = iota
KeyActionRelease
KeyActionTypeKey
)
// KeyAction to perform.
type KeyAction struct {
Type KeyActionType
Key input.Key
}
// KeyActions to simulate.
type KeyActions struct {
keyboard *Keyboard
Actions []KeyAction
}
// KeyActions simulates the type actions on a physical keyboard.
// Useful when input shortcuts like ctrl+enter .
func (p *Page) KeyActions() *KeyActions {
return &KeyActions{keyboard: p.Keyboard}
}
// Press keys is guaranteed to have a release at the end of actions.
func (ka *KeyActions) Press(keys ...input.Key) *KeyActions {
for _, key := range keys {
ka.Actions = append(ka.Actions, KeyAction{KeyActionPress, key})
}
return ka
}
// Release keys.
func (ka *KeyActions) Release(keys ...input.Key) *KeyActions {
for _, key := range keys {
ka.Actions = append(ka.Actions, KeyAction{KeyActionRelease, key})
}
return ka
}
// Type will release the key immediately after the pressing.
func (ka *KeyActions) Type(keys ...input.Key) *KeyActions {
for _, key := range keys {
ka.Actions = append(ka.Actions, KeyAction{KeyActionTypeKey, key})
}
return ka
}
// Do the actions.
func (ka *KeyActions) Do() (err error) {
for _, a := range ka.balance() {
switch a.Type {
case KeyActionPress:
err = ka.keyboard.Press(a.Key)
case KeyActionRelease:
err = ka.keyboard.Release(a.Key)
case KeyActionTypeKey:
err = ka.keyboard.Type(a.Key)
}
if err != nil {
return
}
}
return
}
// Make sure there's at least one release after the presses, such as:
//
// p1,p2,p1,r1 => p1,p2,p1,r1,r2
func (ka *KeyActions) balance() []KeyAction {
actions := ka.Actions
h := map[input.Key]bool{}
for _, a := range actions {
switch a.Type {
case KeyActionPress:
h[a.Key] = true
case KeyActionRelease, KeyActionTypeKey:
h[a.Key] = false
}
}
for key, needRelease := range h {
if needRelease {
actions = append(actions, KeyAction{KeyActionRelease, key})
}
}
return actions
}
// InsertText is like pasting text into the page.
func (p *Page) InsertText(text string) error {
defer p.tryTrace(TraceTypeInput, "insert text "+text)()
p.browser.trySlowMotion()
err := proto.InputInsertText{Text: text}.Call(p)
return err
}
// Mouse represents the mouse on a page, it's always related the main frame.
type Mouse struct {
sync.Mutex
page *Page
id string // mouse svg dom element id
pos proto.Point
// the buttons is currently being pressed, reflects the press order
buttons []proto.InputMouseButton
}
func (p *Page) newMouse() *Page {
p.Mouse = &Mouse{page: p, id: utils.RandString(8)}
return p
}
// Position of current cursor.
func (m *Mouse) Position() proto.Point {
m.Lock()
defer m.Unlock()
return m.pos
}
// MoveTo the absolute position.
func (m *Mouse) MoveTo(p proto.Point) error {
m.Lock()
defer m.Unlock()
button, buttons := input.EncodeMouseButton(m.buttons)
m.page.browser.trySlowMotion()
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseMoved,
X: p.X,
Y: p.Y,
Button: button,
Buttons: gson.Int(buttons),
Modifiers: m.page.Keyboard.getModifiers(),
}.Call(m.page)
if err != nil {
return err
}
// to make sure set only when call is successful
m.pos = p
if m.page.browser.trace {
if !m.updateMouseTracer() {
m.initMouseTracer()
m.updateMouseTracer()
}
}
return nil
}
// MoveAlong the guide function.
// Every time the guide function is called it should return the next mouse position, return true to stop.
// Read the source code of [Mouse.MoveLinear] as an example to use this method.
func (m *Mouse) MoveAlong(guide func() (proto.Point, bool)) error {
for {
p, stop := guide()
if stop {
return m.MoveTo(p)
}
err := m.MoveTo(p)
if err != nil {
return err
}
}
}
// MoveLinear to the absolute position with the given steps.
// Such as move from (0,0) to (6,6) with 3 steps, the mouse will first move to (2,2) then (4,4) then (6,6).
func (m *Mouse) MoveLinear(to proto.Point, steps int) error {
p := m.Position()
step := to.Minus(p).Scale(1 / float64(steps))
count := 0
return m.MoveAlong(func() (proto.Point, bool) {
count++
if count == steps {
return to, true
}
p = p.Add(step)
return p, false
})
}
// Scroll the relative offset with specified steps.
func (m *Mouse) Scroll(offsetX, offsetY float64, steps int) error {
m.Lock()
defer m.Unlock()
defer m.page.tryTrace(TraceTypeInput, fmt.Sprintf("scroll (%.2f, %.2f)", offsetX, offsetY))()
m.page.browser.trySlowMotion()
if steps < 1 {
steps = 1
}
button, buttons := input.EncodeMouseButton(m.buttons)
stepX := offsetX / float64(steps)
stepY := offsetY / float64(steps)
for i := 0; i < steps; i++ {
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseWheel,
Button: button,
Buttons: gson.Int(buttons),
Modifiers: m.page.Keyboard.getModifiers(),
DeltaX: stepX,
DeltaY: stepY,
X: m.pos.X,
Y: m.pos.Y,
}.Call(m.page)
if err != nil {
return err
}
}
return nil
}
// Down holds the button down.
func (m *Mouse) Down(button proto.InputMouseButton, clickCount int) error {
m.Lock()
defer m.Unlock()
toButtons := append(append([]proto.InputMouseButton{}, m.buttons...), button)
_, buttons := input.EncodeMouseButton(toButtons)
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMousePressed,
Button: button,
Buttons: gson.Int(buttons),
ClickCount: clickCount,
Modifiers: m.page.Keyboard.getModifiers(),
X: m.pos.X,
Y: m.pos.Y,
}.Call(m.page)
if err != nil {
return err
}
m.buttons = toButtons
return nil
}
// Up releases the button.
func (m *Mouse) Up(button proto.InputMouseButton, clickCount int) error {
m.Lock()
defer m.Unlock()
toButtons := []proto.InputMouseButton{}
for _, btn := range m.buttons {
if btn == button {
continue
}
toButtons = append(toButtons, btn)
}
_, buttons := input.EncodeMouseButton(toButtons)
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseReleased,
Button: button,
Buttons: gson.Int(buttons),
ClickCount: clickCount,
Modifiers: m.page.Keyboard.getModifiers(),
X: m.pos.X,
Y: m.pos.Y,
}.Call(m.page)
if err != nil {
return err
}
m.buttons = toButtons
return nil
}
// Click the button. It's the combination of [Mouse.Down] and [Mouse.Up].
func (m *Mouse) Click(button proto.InputMouseButton, clickCount int) error {
m.page.browser.trySlowMotion()
err := m.Down(button, clickCount)
if err != nil {
return err
}
return m.Up(button, clickCount)
}
// Touch presents a touch device, such as a hand with fingers, each finger is a [proto.InputTouchPoint].
// Touch events is stateless, we use the struct here only as a namespace to make the API style unified.
type Touch struct {
page *Page
}
func (p *Page) newTouch() *Page {
p.Touch = &Touch{page: p}
return p
}
// Start a touch action.
func (t *Touch) Start(points ...*proto.InputTouchPoint) error {
// TODO: https://crbug.com/613219
_ = t.page.WaitRepaint()
_ = t.page.WaitRepaint()
return proto.InputDispatchTouchEvent{
Type: proto.InputDispatchTouchEventTypeTouchStart,
TouchPoints: points,
Modifiers: t.page.Keyboard.getModifiers(),
}.Call(t.page)
}
// Move touch points. Use the [proto.InputTouchPoint.ID] (Touch.identifier) to track points.
// Doc: https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
func (t *Touch) Move(points ...*proto.InputTouchPoint) error {
return proto.InputDispatchTouchEvent{
Type: proto.InputDispatchTouchEventTypeTouchMove,
TouchPoints: points,
Modifiers: t.page.Keyboard.getModifiers(),
}.Call(t.page)
}
// End touch action.
func (t *Touch) End() error {
return proto.InputDispatchTouchEvent{
Type: proto.InputDispatchTouchEventTypeTouchEnd,
TouchPoints: []*proto.InputTouchPoint{},
Modifiers: t.page.Keyboard.getModifiers(),
}.Call(t.page)
}
// Cancel touch action.
func (t *Touch) Cancel() error {
return proto.InputDispatchTouchEvent{
Type: proto.InputDispatchTouchEventTypeTouchCancel,
TouchPoints: []*proto.InputTouchPoint{},
Modifiers: t.page.Keyboard.getModifiers(),
}.Call(t.page)
}
// Tap dispatches a touchstart and touchend event.
func (t *Touch) Tap(x, y float64) error {
defer t.page.tryTrace(TraceTypeInput, "touch")()
t.page.browser.trySlowMotion()
p := &proto.InputTouchPoint{X: x, Y: y}
err := t.Start(p)
if err != nil {
return err
}
return t.End()
}