Chromedp: May I know how to send key combinations

Created on 26 Jun 2019  路  2Comments  路  Source: chromedp/chromedp

Excuse me, I can't find the relevant documents.

I want to realize such a function:

sendKey: ctrl+a delete ,

To delete all text

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

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

orlangure picture orlangure  路  3Comments

botsphp picture botsphp  路  3Comments

AmrAlfoly picture AmrAlfoly  路  3Comments

zhaobingss picture zhaobingss  路  4Comments

youshy picture youshy  路  5Comments