CEF Simulate Mousedown and Keysend
Asked Answered
S

2

7

I want to use CEF to control a Flash Application, so I need to simulate MouseDown and KeySend without JavaScript. I am using offscreen rendering. This is what I tried:

managedCefBrowserAdapter.OnMouseButton(
    500, 500, 0, true, 2, CefEventFlags.LeftMouseButton); 

or

MouseEvent a = new MouseEvent();
a.Modifiers = CefEventFlags.LeftMouseButton;
a.X = 500;
a.Y = 500;

or

managedCefBrowserAdapter.SendKeyEvent(0, 0, 0);

But nothing works.

Sabol answered 30/6, 2015 at 9:55 Comment(1)
Have you run the CefSharp.Wpf.Example app putting in breakpoints into the mouse click methods to see what it does? From memory I think you'll find you need to send two events, a down followed by an up. Same applies for key presses.Servetnick
T
17

This code works for me. Clicks once on the position 0, 0 (left-upper corner)

browser.GetBrowser().GetHost().SendMouseClickEvent(0, 0, MouseButtonType.Left, false, 1, CefEventFlags.None);
System.Threading.Thread.Sleep(100);
browser.GetBrowser().GetHost().SendMouseClickEvent(0, 0, MouseButtonType.Left, true, 1, CefEventFlags.None);
Tourniquet answered 21/1, 2016 at 20:58 Comment(3)
How do i get points to a specific button on a page ? is it guessing in the wild? and how do i tell if the click is performed.Globeflower
Just figured out by putting a Right click i can see the location where it is clicking :DGlobeflower
Could you please explain the parameters here. what does "false" and "true" and "1" mean?Amah
J
9

The answer above works ok. I just like to add some information that I think is very important...


Simulate a Mouse Click (and Move):

// get browser host
var wbHost = chromiumWebBrowser1.GetBrowser().GetHost();

// clicking cordinates
int xPosition = 100;
int yPosition = 300;

// send mouse move event
wbHost.SendMouseMoveEvent(xPosition, yPosition, false, CefEventFlags.None);
Thread.Sleep(50);

// send click
wbHost.SendMouseClickEvent(xPosition, yPosition, MouseButtonType.Left, false, 1, CefEventFlags.None);
Thread.Sleep(50);
wbHost.SendMouseClickEvent(xPosition, yPosition, MouseButtonType.Left, true, 1, CefEventFlags.None);

1 - Sometimes (specially with anti-crawler environments) it's necessary to also mimic the mouse movement. I do it using the method SendMouseMoveEvent as shown in the first line.

2 - The clicking occurs at the specified x and y coordinates, but is very important to know that this coordinates are relative to the browser component window (viewport). So if your web browser component is only 100px tall and you send a clicking command to y coordinate as 150px the clicking will occur but outside the web browser, hence, not in the web page.

3 - One trick to see if the click is really being executed, and where exactly, is change it from MouseButtonType.Left to MouseButtonType.Right. Doing it, if there is no restrictions on the web page, you will be able to see the "mouse right button menu". If you are clicking outside, chances are that you'll see a "mouse right button menu" on the OS (Windows here, and I was able to see that I was clicking outside my component with this trick) .

4 - The true and false on SendMouseClickEvent are to send a "Mouse Button Click Down" event and "Mouse Button Click UP" event, respectively.

5 - To get a position of an object on the DOM, you can use the approach on my answer here:
CefSharp webpage element click

6 - So if you have a very tall page loaded in your component, and you need to click on a button really outside the boundaries of the component, you need to scroll the web page. You can do it with code below:


Simulate a Mouse Scroll (Mouse Wheel Event):

// send scroll command
// (mouse position X, mouse position Y, how much to scroll X, how much to scroll Y, events)
chromiumWebBrowser1.GetBrowser().GetHost().SendMouseWheelEvent(10, 10, 0, -100, CefEventFlags.None); 
Thread.Sleep(300);

The biggest part of the parameters are self-explanatory, but the most important part here is the 3rd and 4th parameters, which are respectively "how much to scroll X" and "how much to scroll Y". To scroll down, just use negative values, and to scroll up, positive ones. In the code above, it scrolls nothing (zero pixels) horizontally (x axis) and 100px down vertically (y axis).

You can use this code inside a loop to scroll how much you need. I use it in conjunction with a JavaScript code to retrieve the position of a button, to detect if it's on the viewport, or if i need to send a scroll event again.

Jeff answered 6/12, 2020 at 2:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.