Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DispatchKeyEvent example #52

Open
varashellov opened this issue Apr 4, 2018 · 6 comments
Open

DispatchKeyEvent example #52

varashellov opened this issue Apr 4, 2018 · 6 comments

Comments

@varashellov
Copy link

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,
	})
@mafredri
Copy link
Owner

mafredri commented Apr 4, 2018

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

@djmally
Copy link

djmally commented Apr 16, 2018

@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
Copy link

djmally commented Apr 16, 2018

And here's the gist!

@mafredri
Copy link
Owner

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.

@clanstyles
Copy link

	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

@mafredri
Copy link
Owner

mafredri commented Nov 14, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants