github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/input/keyboard.go (about) 1 package input 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/mafredri/cdp" 8 "github.com/mafredri/cdp/protocol/input" 9 ) 10 11 type ( 12 KeyboardModifier int 13 14 KeyboardLocation int 15 16 KeyboardKey struct { 17 KeyCode int 18 Key string 19 Code string 20 Modifier KeyboardModifier 21 Location KeyboardLocation 22 } 23 24 Keyboard struct { 25 client *cdp.Client 26 } 27 ) 28 29 const ( 30 KeyboardModifierNone KeyboardModifier = 0 31 KeyboardModifierAlt KeyboardModifier = 1 32 KeyboardModifierCtrl KeyboardModifier = 2 33 KeyboardModifierCmd KeyboardModifier = 4 34 KeyboardModifierShift KeyboardModifier = 8 35 36 // 1=Left, 2=Right 37 KeyboardLocationNone KeyboardLocation = 0 38 KeyboardLocationLeft KeyboardLocation = 1 39 KeyboardLocationRight KeyboardLocation = 2 40 ) 41 42 func NewKeyboard(client *cdp.Client) *Keyboard { 43 return &Keyboard{client} 44 } 45 46 func (k *Keyboard) Down(ctx context.Context, char string) error { 47 return k.client.Input.DispatchKeyEvent( 48 ctx, 49 input.NewDispatchKeyEventArgs("keyDown"). 50 SetText(char), 51 ) 52 } 53 54 func (k *Keyboard) Up(ctx context.Context, char string) error { 55 return k.client.Input.DispatchKeyEvent( 56 ctx, 57 input.NewDispatchKeyEventArgs("keyUp"). 58 SetText(char), 59 ) 60 } 61 62 func (k *Keyboard) Type(ctx context.Context, text string, delay time.Duration) error { 63 for _, ch := range text { 64 ch := string(ch) 65 66 if err := k.Down(ctx, ch); err != nil { 67 return err 68 } 69 70 releaseDelay := randomDuration(int(delay)) 71 time.Sleep(releaseDelay) 72 73 if err := k.Up(ctx, ch); err != nil { 74 return err 75 } 76 } 77 78 return nil 79 } 80 81 func (k *Keyboard) Press(ctx context.Context, keys []string, count int, delay time.Duration) error { 82 for i := 0; i < count; i++ { 83 if i > 0 { 84 downDelay := randomDuration(int(delay)) 85 time.Sleep(downDelay) 86 } 87 88 if err := k.press(ctx, keys, delay); err != nil { 89 return err 90 } 91 } 92 93 return nil 94 } 95 96 func (k *Keyboard) press(ctx context.Context, keys []string, delay time.Duration) error { 97 for i, key := range keys { 98 if i > 0 { 99 downDelay := randomDuration(int(delay)) 100 time.Sleep(downDelay) 101 } 102 103 if err := k.client.Input.DispatchKeyEvent( 104 ctx, 105 k.createPressEvent("keyDown", key), 106 ); err != nil { 107 return err 108 } 109 } 110 111 for _, key := range keys { 112 upDelay := randomDuration(int(delay)) 113 time.Sleep(upDelay) 114 115 if err := k.client.Input.DispatchKeyEvent( 116 ctx, 117 k.createPressEvent("keyUp", key), 118 ); err != nil { 119 return err 120 } 121 } 122 123 return nil 124 } 125 126 func (k *Keyboard) createPressEvent(event string, chars string) *input.DispatchKeyEventArgs { 127 args := input.NewDispatchKeyEventArgs(event) 128 129 key, found := usKeyboardLayout[chars] 130 131 if found { 132 args. 133 SetCode(key.Code). 134 SetKey(key.Key). 135 SetModifiers(int(key.Modifier)). 136 SetWindowsVirtualKeyCode(key.KeyCode) 137 } 138 139 return args 140 }