【发布时间】:2020-08-19 17:47:34
【问题描述】:
我有一个基于 CefSharp v83.4.20 的应用程序,并尝试从 C# 代码中注入双击。 Winform 和 WPF 都会发生这种情况。
这是用于测试的Html:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<div style="position: absolute; width: 100px; height: 100px; z-index: 100; background: rgb(0, 0, 0); left: 100px; top: 100px;"></div>
</body>
</html>
<script>
var elem = document.getElementsByTagName('div')[0];
elem.addEventListener('mousedown', function () {
console.log('mousedown');
});
elem.addEventListener('mouseup', function () {
console.log('mouseup');
});
elem.addEventListener('click', function () {
console.log('click');
});
elem.addEventListener('dblclick', function () {
// this never gets called when injecting two clicks, but works when manually double clicking
console.log('doubleclicked');
});
</script>
我可以像这样从 C# 代码轻松地向 Html 注入点击:
int x = 150;
int y = 150;
var host = chromeBrowserInstance.GetBrowser().GetHost();
host.SendMouseClickEvent(x, y, MouseButtonType.Left, false, 0, CefEventFlags.LeftMouseButton);
System.Threading.Thread.Sleep(50);
host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 0, CefEventFlags.LeftMouseButton);
但是我怎样才能注入双击呢? 如上图简单发送两次点击失败:
int x = 150;
int y = 150;
var host = chromeBrowserInstance.GetBrowser().GetHost();
host.SendMouseClickEvent(x, y, MouseButtonType.Left, false, 0, CefEventFlags.LeftMouseButton);
System.Threading.Thread.Sleep(50);
host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 0, CefEventFlags.LeftMouseButton);
System.Threading.Thread.Sleep(100);
host.SendMouseClickEvent(x, y, MouseButtonType.Left, false, 0, CefEventFlags.LeftMouseButton);
System.Threading.Thread.Sleep(50);
host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 0, CefEventFlags.LeftMouseButton);
这会给出以下输出(缺少预期的“双击”):
mousedown
mouseup
click
mousedown
mouseup
click
手动双击时会出现预期的“双击”:
mousedown
mouseup
click
mousedown
mouseup
click
doubleclicked
我试图调整时间,但没有任何帮助。任何想法如何注入有效的双击?
编辑:
感谢您的输入,我试过了:
MouseEvent me = new MouseEvent(150, 150, CefEventFlags.LeftMouseButton);
bool mouseup = true;
int clickCount = 2;
host.SendMouseClickEvent(
me,
MouseButtonType.Left,
mouseup,
clickCount
);
但是无论我为clickCount(甚至是0)设置什么,它只会产生一个mouseup /
鼠标按下事件。
在我发现的正文中添加随机文本后,使用上述方法
clickCount 是 2 或 3 和 mouseup = true 正文中的文本得到
就像手动双击/三次单击时一样突出显示。
所以它以某种方式工作,但不会产生预期的事件。
【问题讨论】:
-
尝试使用cefsharp.github.io/api/83.4.x/html/… 并指定点击次数
-
github.com/cefsharp/CefSharp.MinimalExample 中包含的 WPF 示例是否按预期工作?它在内部使用鼠标功能。
-
对不起,我忘了说我使用 Winforms 进行测试。但是使用 MinimalExample WPF 版本测试相同的代码会产生完全相同的结果。
-
您是否在 WPF 示例中加载了 html 并使用了鼠标? WPF 版本在内部使用这些方法,如果它在 WPF 示例中有效,您可以添加一些日志记录来锻炼您需要的调用。
标签: javascript c# html cefsharp chromium-embedded