Issue With CefSharp Browser SendKeys
Asked Answered
C

2

9

I am using chromium browser to automate some task.

Basically I want to load images for that I have to click "Add Image" anchor tag on the webpage.

So I cannot directly click it not sure why when other works with same code. But I have managed to bring the focus on that anchor tag but sending :

 KeyEvent k = new KeyEvent();
 k.WindowsKeyCode = 9;  // TAB KEY
 browser.GetBrowser().GetHost().SendKeyEvent(k); 
 //browser = ChromiumWebBrowser browser;

The above work perfectly fine as i see it coming to the correct anchor Tag.

Now i do :

 KeyEvent k = new KeyEvent();
 k.WindowsKeyCode = 13;   //ENTER KEY
 browser.GetBrowser().GetHost().SendKeyEvent(k);

To simulate enter key , however nothing happens but if I press "Enter" manually on Keyword it works fine so why the above is not acting same as me clicking "Enter Key"

Any suggestions.

Crinite answered 24/6, 2017 at 13:27 Comment(0)
L
16

I used your code like this, and it works for me.

KeyEvent k = new KeyEvent();
k.WindowsKeyCode = 0x0D;
k.FocusOnEditableField = true;
k.IsSystemKey = false;
k.Type = KeyEventType.Char;
Browser.GetBrowser().GetHost().SendKeyEvent(k);
La answered 13/7, 2017 at 13:23 Comment(1)
Hey, thanks for this! If I wanted to replace setting the HEX value with rather a char, is that possible? Also would love to differentiate from 'd' (68) vs 'D' (100).Miskolc
S
5

So much time spent, but found the accepted answer useful to get to the bottom of this:

KeyEvent k = new KeyEvent
{
    WindowsKeyCode = 0x0D, // Enter
    FocusOnEditableField = true,
    IsSystemKey = false,
    Type = KeyEventType.KeyDown
};

_browser.GetBrowser().GetHost().SendKeyEvent(k);

Thread.Sleep(100);

k = new KeyEvent
{
    WindowsKeyCode = 0x0D, // Enter
    FocusOnEditableField = true,
    IsSystemKey = false,
    Type = KeyEventType.KeyUp
};

_browser.GetBrowser().GetHost().SendKeyEvent(k);

Thread.Sleep(100);

Notice the KeyEventType.KeyDown and then KeyEventType.KeyUp is sent.

Inspired by CEF Simulate Mousedown and Keysend and CefSharp webpage element click.

Stacte answered 23/11, 2018 at 19:32 Comment(1)
Hey, thanks for this! If I wanted to replace setting the HEX value with rather a char, is that possible? Also would love to differentiate from 'd' (68) vs 'D' (100)Miskolc

© 2022 - 2024 — McMap. All rights reserved.