Excuse me, I can't find the relevant documents.
I want to realize such a function:
sendKey: ctrl+a delete ,
To delete all text
Have you seen https://github.com/chromedp/examples?
If you want to just send the key in a sequence, as if the user had pressed it independently, build a string using chromedp/kb.Control, and use it with chromedp.SendKeys:
str := kb.Control+"a"
if err := chromedp.Run(ctx, chromedp.SendKeys(`#thing`, str)); err != nil {
panic(err)
}
Note: SendKeys synthesizes a user "typing" the string. It doesn't try to do any kind of advanced logic such as holding down keys, as it would be impossible to model this in a single string.
If you need to send multiple pressed keys at the same time (ie, holding Control + Alt while also sending the A key), you'll need to use the direct API calls, which would be roughly:
KeyDown(Control)
KeyDown(Alt)
KeyPress(A)
KeyUp(Alt)
KeyUp(Control)
The Chrome API to send individual key events is in the github.com/chromedp/cdproto/input package:
if err := chromedp.Run(ctx, input.DispatchKeyEvent(input.KeyDown).WithKey(kb.Control)); err != nil {
panic(err)
}
The above would need to be done for the sequence I described above. Additionally, since Alt is a modifier, you'd need to set it as correctly to the DispatchKeyEvent command. If in doubt about exactly _which_ Chrome key events to send, and in what order, I would suggest using chromedp-proxy and recording a session from Chrome's DevTools and looking at the wireline protocol commands that DevTools sends.
Most helpful comment
If you want to just send the key in a sequence, as if the user had pressed it independently, build a string using
chromedp/kb.Control, and use it withchromedp.SendKeys:Note:
SendKeyssynthesizes a user "typing" the string. It doesn't try to do any kind of advanced logic such as holding down keys, as it would be impossible to model this in a single string.If you need to send multiple pressed keys at the same time (ie, holding Control + Alt while also sending the A key), you'll need to use the direct API calls, which would be roughly:
The Chrome API to send individual key events is in the
github.com/chromedp/cdproto/inputpackage:The above would need to be done for the sequence I described above. Additionally, since
Altis a modifier, you'd need to set it as correctly to the DispatchKeyEvent command. If in doubt about exactly _which_ Chrome key events to send, and in what order, I would suggest usingchromedp-proxyand recording a session from Chrome's DevTools and looking at the wireline protocol commands that DevTools sends.