cdp icon indicating copy to clipboard operation
cdp copied to clipboard

DispatchKeyEvent example

Open varashellov opened this issue 6 years ago • 7 comments

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,
	})

varashellov avatar Apr 04 '18 15:04 varashellov

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 😄.

mafredri avatar Apr 04 '18 16:04 mafredri

@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.

djmally avatar Apr 16 '18 18:04 djmally

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.

mafredri avatar Apr 18 '18 17:04 mafredri

	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 avatar Nov 14 '18 18:11 clanstyles

@clanstyles what are you setting as text value? You can only use single characters like say SetText("a") or SetText("\r").

mafredri avatar Nov 14 '18 19:11 mafredri