cdp
cdp copied to clipboard
DispatchKeyEvent example
Hello! Please provide an example of keystrokes. My code does not work
key := "Space"
c.Input.DispatchKeyEvent(ctx, &input.DispatchKeyEventArgs{
Type: "keyDown",
Key: &key,
})
c.Input.DispatchKeyEvent(ctx, &input.DispatchKeyEventArgs{
Type: "keyUp",
Key: &key,
})
I believe you might also need to dispatch an event of type "char"
, not sure. And you might need to provide additional fields. I recommend taking a look at how puppeteer does it:
https://github.com/GoogleChrome/puppeteer/blob/master/lib/Input.js
Also, take a look at the resources listen in the readme, both the devtools repo and mailing list are excellent resources.
I'd be happy to accept PRs with examples! Unfortunately I don't have time to create an example for every possible use case 😄.
@varashellov @mafredri I have used this in my code, I will write up a short Go example & post. I'm sure others will find it useful as well.
Thanks @djmally, didn't realize it was enough to just send keyDown, good to know. Perhaps in some cases it mandates to send more of them.
Anywho, sending key events is something that can be improved in this library, so I'll keep this open for now as a reminder.
if err := ctx.Input.DispatchKeyEvent(ctx, input.NewDispatchKeyEventArgs("keyDown").SetText(text)); err != nil {
return errors.Wrap(err, "failed to type into the input")
}
cdp.Input: DispatchKeyEvent: rpc error: Invalid 'text' parameter (code = -32602)
I'm running stable Chrome & Chromium
@clanstyles what are you setting as text value? You can only use single characters like say SetText("a")
or SetText("\r")
.