mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-14 11:23:02 +02:00
eee801fb5b
* Normalized and externalized input logic * Fixed linting issue * Removed redundant mutex * Added missed locks in Page * Fixed deadlock
53 lines
929 B
Go
53 lines
929 B
Go
package input
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/mafredri/cdp"
|
|
"github.com/mafredri/cdp/protocol/input"
|
|
)
|
|
|
|
type Keyboard struct {
|
|
client *cdp.Client
|
|
}
|
|
|
|
func NewKeyboard(client *cdp.Client) *Keyboard {
|
|
return &Keyboard{client}
|
|
}
|
|
|
|
func (k *Keyboard) Down(ctx context.Context, char string) error {
|
|
return k.client.Input.DispatchKeyEvent(
|
|
ctx,
|
|
input.NewDispatchKeyEventArgs("keyDown").
|
|
SetText(char),
|
|
)
|
|
}
|
|
|
|
func (k *Keyboard) Up(ctx context.Context, char string) error {
|
|
return k.client.Input.DispatchKeyEvent(
|
|
ctx,
|
|
input.NewDispatchKeyEventArgs("keyUp").
|
|
SetText(char),
|
|
)
|
|
}
|
|
|
|
func (k *Keyboard) Type(ctx context.Context, text string, delay int) error {
|
|
for _, ch := range text {
|
|
ch := string(ch)
|
|
|
|
if err := k.Down(ctx, ch); err != nil {
|
|
return err
|
|
}
|
|
|
|
releaseDelay := randomDuration(delay)
|
|
time.Sleep(releaseDelay)
|
|
|
|
if err := k.Up(ctx, ch); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|