-
-
Notifications
You must be signed in to change notification settings - Fork 350
/
keyboard.go
86 lines (67 loc) · 1.62 KB
/
keyboard.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
package rod
import (
"sync"
"github.com/go-rod/rod/lib/input"
"github.com/go-rod/rod/lib/proto"
)
// Keyboard represents the keyboard on a page, it's always related the main frame
type Keyboard struct {
lock *sync.Mutex
page *Page
// modifiers are currently beening pressed
modifiers int64
}
// DownE doc is similar to the method Down
func (k *Keyboard) DownE(key rune) error {
k.lock.Lock()
defer k.lock.Unlock()
actions := input.Encode(key)
err := actions[0].Call(k.page)
if err != nil {
return err
}
k.modifiers = actions[0].Modifiers
return nil
}
// UpE doc is similar to the method Up
func (k *Keyboard) UpE(key rune) error {
k.lock.Lock()
defer k.lock.Unlock()
actions := input.Encode(key)
err := actions[len(actions)-1].Call(k.page)
if err != nil {
return err
}
k.modifiers = 0
return nil
}
// PressE doc is similar to the method Press
func (k *Keyboard) PressE(key rune) error {
k.lock.Lock()
defer k.lock.Unlock()
if k.page.browser.trace {
defer k.page.Overlay(0, 0, 200, 0, "press "+input.Keys[key].Key)()
}
k.page.browser.trySlowmotion()
actions := input.Encode(key)
k.modifiers = actions[0].Modifiers
defer func() { k.modifiers = 0 }()
for _, action := range actions {
err := action.Call(k.page)
if err != nil {
return err
}
}
return nil
}
// InsertTextE doc is similar to the method InsertText
func (k *Keyboard) InsertTextE(text string) error {
k.lock.Lock()
defer k.lock.Unlock()
if k.page.browser.trace {
defer k.page.Overlay(0, 0, 200, 0, "insert text "+text)()
}
k.page.browser.trySlowmotion()
err := proto.InputInsertText{Text: text}.Call(k.page)
return err
}