chromedp / chromedp

A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.
MIT License
10.82k stars 783 forks source link

How to send key ctrl+a on Windows #1384

Open tuwibu opened 10 months ago

tuwibu commented 10 months ago

What versions are you running?

$ go list -m github.com/chromedp/chromedp
v0.9.2
$ google-chrome --version
116
$ go version
1.21.3
$ platform
windows 11

What did you do? Include clear steps.

// youtube.com
// Test 1: push a into input
if err := chromedp.Run(ctx,
    chromedp.SendKeys(`input#search`, kb.Control+"a", chromedp.ByQuery),
); err != nil {
    return err
}
// Test 2: not work
if err := chromedp.Run(ctx,
    input.DispatchKeyEvent(input.KeyDown).WithModifiers(input.ModifierCtrl).WithKey("a"),
); err != nil {
    return err
}
// Test 3: not work
if err := chromedp.Run(ctx,
    chromedp.Tasks{
        input.DispatchKeyEvent(input.KeyDown).WithModifiers(input.ModifierCtrl).WithKey("a"),
        input.DispatchKeyEvent(input.KeyUp).WithModifiers(input.ModifierCtrl).WithKey("a"),
    },
); err != nil {
    return err
}
// Test 4: work but ctrl + a and replace a??
if err := chromedp.Run(ctx,
    chromedp.KeyEvent("a", chromedp.KeyModifiers(input.ModifierCtrl)),
); err != nil {
    return err
}

What did you expect to see?

What did you see instead?

ZekeLu commented 10 months ago

What's the problem? Have you checked #1357?

tuwibu commented 10 months ago

Proof error: bandicam 2023-10-14 19-37-00-198 This is example:

page1, _ := chromedp.NewContext(browserCtx)
if err := chromedp.Run(page1,
    chromedp.Navigate("https://www.youtube.com/"),
); err != nil {
    logger.Error(err.Error())
}
time.Sleep(1 * time.Second)
if err := chromedp.Run(page1,
    chromedp.SendKeys("input#search", "anime", chromedp.NodeVisible),
); err != nil {
    logger.Error(err.Error())
}
time.Sleep(1 * time.Second)
if err := chromedp.Run(page1,
    chromedp.KeyEvent("a", chromedp.KeyModifiers(input.ModifierCtrl)),
); err != nil {
    logger.Error(err.Error())
}
ZekeLu commented 10 months ago

It works on my system (linux/amd64) correctly. I don't have Windows 11 to test the issue. Does it work if you start the browser and press Ctrl+a manually?

executable demo ```go package main import ( "context" "time" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" ) func main() { opts := []chromedp.ExecAllocatorOption{ chromedp.Flag("headless", false), } ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...) defer cancel() ctx, cancel = chromedp.NewContext(ctx) defer cancel() if err := chromedp.Run(ctx); err != nil { panic(err) } page1, _ := chromedp.NewContext(ctx) if err := chromedp.Run(page1, chromedp.Navigate("https://www.youtube.com/"), ); err != nil { panic(err) } time.Sleep(1 * time.Second) if err := chromedp.Run(page1, chromedp.SendKeys("input#search", "anime", chromedp.NodeVisible), ); err != nil { panic(err) } time.Sleep(1 * time.Second) if err := chromedp.Run(page1, chromedp.KeyEvent("a", chromedp.KeyModifiers(input.ModifierCtrl)), ); err != nil { panic(err) } time.Sleep(5 * time.Second) } ```
tuwibu commented 10 months ago

Yep, in chrome manually i can press Ctrl+a I had used your demo, its still error

ZekeLu commented 10 months ago

Thank you! I have managed to reproduce the issue on Windows (both Win 10 and Win 11).

chromedp sends these commands:

-> {"id":53,"sessionId":"28531763C0BD8289A494E20D666B8D77","method":"Input.dispatchKeyEvent","params":{"type":"keyDown","modifiers":2,"code":"KeyA","key":"a","windowsVirtualKeyCode":65,"nativeVirtualKeyCode":65,"autoRepeat":false,"isKeypad":false,"isSystemKey":false}}
-> {"id":54,"sessionId":"28531763C0BD8289A494E20D666B8D77","method":"Input.dispatchKeyEvent","params":{"type":"char","modifiers":2,"text":"a","unmodifiedText":"a","code":"KeyA","key":"a","windowsVirtualKeyCode":97,"nativeVirtualKeyCode":97,"autoRepeat":false,"isKeypad":false,"isSystemKey":false}}
-> {"id":55,"sessionId":"28531763C0BD8289A494E20D666B8D77","method":"Input.dispatchKeyEvent","params":{"type":"keyUp","modifiers":2,"code":"KeyA","key":"a","windowsVirtualKeyCode":65,"nativeVirtualKeyCode":65,"autoRepeat":false,"isKeypad":false,"isSystemKey":false}}

And I have tested Puppeteer with this code:

  await page.keyboard.down('Control');
  await page.keyboard.press('a');
  await page.keyboard.up('Control');

Puppeteer does not have this issue. It sends these commands:

SEND ► ['{"method":"Input.dispatchKeyEvent","params":{"type":"rawKeyDown","modifiers":2,"windowsVirtualKeyCode":17,"code":"ControlLeft","key":"Control","text":"","unmodifiedText":"","autoRepeat":false,"location":1,"isKeypad":false,"commands":[]},"id":27,"sessionId":"DF8CA216724291A14D662105A5CAFFF2"}']
SEND ► ['{"method":"Input.dispatchKeyEvent","params":{"type":"rawKeyDown","modifiers":2,"windowsVirtualKeyCode":65,"code":"KeyA","key":"a","text":"","unmodifiedText":"","autoRepeat":false,"location":0,"isKeypad":false},"id":28,"sessionId":"DF8CA216724291A14D662105A5CAFFF2"}']
SEND ► ['{"method":"Input.dispatchKeyEvent","params":{"type":"keyUp","modifiers":2,"key":"a","windowsVirtualKeyCode":65,"code":"KeyA","location":0},"id":29,"sessionId":"DF8CA216724291A14D662105A5CAFFF2"}']
SEND ► ['{"method":"Input.dispatchKeyEvent","params":{"type":"keyUp","modifiers":0,"key":"Control","windowsVirtualKeyCode":17,"code":"ControlLeft","location":1},"id":30,"sessionId":"DF8CA216724291A14D662105A5CAFFF2"}']

It's the following command that causes the trouble:

-> {"id":54,"sessionId":"28531763C0BD8289A494E20D666B8D77","method":"Input.dispatchKeyEvent","params":{"type":"char","modifiers":2,"text":"a","unmodifiedText":"a","code":"KeyA","key":"a","windowsVirtualKeyCode":97,"nativeVirtualKeyCode":97,"autoRepeat":false,"isKeypad":false,"isSystemKey":false}}

Maybe chromedp should not dispatch this char event when any of the modifiers besides shift is pressed.

@kenshaw Any suggestions?