【问题标题】:C# CefSharp Offscreen mouse events, keyboard events emulating exampleC# CefSharp 离屏鼠标事件、键盘事件模拟示例
【发布时间】:2016-09-10 21:38:12
【问题描述】:

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

鼠标在屏幕上向下 x=100,y=100....移动到 x=200,y=200 并向上鼠标。 按“Enter”键后。

谢谢。

【问题讨论】:

  • WPF 控件使用相同的离屏渲染引擎,您可以添加一些调试打印语句来记录发生的情况。记得设置焦点。没有我知道的例子,尽管贡献一些!
  • magpcss.org/ceforum/viewtopic.php?f=6&t=13220 是相关的,代码应该很容易映射到CefSharp,确保你阅读了主题的两页。

标签: c# events mouse cefsharp emulation


【解决方案1】:

后来偶然发现了这个2y 6m,想把它发给可能处于相同位置的人。 解决方案包括使用CefSharp 框架和通过CefSharp 执行JavaScript

//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);
}

特别感谢以下帖子:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 2012-06-26
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多