C#CefSharp屏幕外鼠标事件,键盘事件模拟示例 [英] C# CefSharp Offscreen mouse events, keyboard events emulating example

查看:2625
本文介绍了C#CefSharp屏幕外鼠标事件,键盘事件模拟示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以向我展示CefSharp.OffScreen浏览器的鼠标和键盘事件的example(C#)模拟吗? 对不起,我的英语... 例如:

Anybody can show me example(C#) of CefSharp.OffScreen browser mouse and keyboard events emulating? Sorry about my english... For example:

将鼠标移至屏幕x = 100,y = 100上....移至x = 200,y = 200,然后将鼠标移至上方. 按"Enter"键后.

mouse down on screen x=100,y=100....move to x=200,y=200 and mouse up. After press 'Enter' key.

谢谢.

推荐答案

稍后偶然发现了这个2y 6m,并想将其发布给可能处于同一位置的人. 解决方案包括使用CefSharp框架和通过CefSharp执行JavaScript.

Stumbled upon this 2y 6m later and wanted to put it out to someone who might be in the same position. Solution includes usage of CefSharp framework and JavaScript execution via CefSharp.

//1. Find the Html element x and y coordinates with something like this:

var scriptTask = _browser.EvaluateScriptAsync(@"
    var play = document.getElementByClassName('image')[0]
    function findPos(obj)
    {
        var curleft = 0;
        var curtop = 0;

        if (obj.offsetParent)
        {
            do
            {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);

            return { X: curleft,Y: curtop};
        }
    }
    findPos(play)"
)
.ContinueWith(x =>
{
// 2. Continue with finding the coordinates and using MouseClick method 
// for pressing left mouse button down and releasing it at desired end position.
    var responseForMouseClick = x.Result;

    if (responseForMouseClick.Success && responseForMouseClick.Result != null)
    {
        var xy = responseForMouseClick.Result;
        var json = JsonConvert.SerializeObject(xy).ToString();
        var coordx = json.Substring(json.IndexOf(':') + 1, 3);
        var coordy = json.Substring(json.LastIndexOf(':') + 1, 3);

        MouseLeftDown(int.Parse(coordx) + 5, int.Parse(coordy) + 5);
        MouseLeftUp(int.Parse(coordx) + 100, int.Parse(coordy) + 100);
    }

// 3. Repeat the finding of coordinates for making focus with a click. 
// Use the HitEnter method to send the KeyEvent.
    _browser.EvaluateScriptAsync(@"
        var objForHittingEnter = document
             .getElementsByClassName('class-name-for-hitting-enter-on')[0]
                 findPos(objForHittingEnter)") // Already defined earlier
    .ContinueWith(y =>
    {
        var responseForEnter = y.Result;

        if (responseForEnter.Success && responseForEnter.Result != null)
        {
            var xy = responseForEnter.Result;
            var json = JsonConvert.SerializeObject(xy).ToString();
            var coordx = json.Substring(json.IndexOf(':') + 1, 3);
            var coordy = json.Substring(json.LastIndexOf(':') + 1, 3);

            HitEnter(int.Parse(coordx) + 2, int.Parse(coordy) + 2);
        }
    });
});


public void MouseLeftDown(int x, int y)
{
    _browser.GetBrowser().GetHost()
        .SendMouseClickEvent(x, y, MouseButtonType.Left, false, 1, CefEventFlags.None);
    Thread.Sleep(15);
}

public void MouseLeftUp(int x, int y)
{
    _browser.GetBrowser().GetHost()
        .SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.None);
    Thread.Sleep(15);
}

public void HitEnter(int x, int y)
{
    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);
}

特别感谢以下帖子:

  • CefSharp webpage element click
  • Issue With CefSharp Browser SendKeys
  • CEF Simulate Mousedown and Keysend

这篇关于C#CefSharp屏幕外鼠标事件,键盘事件模拟示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆