SendKey 进入浏览器控件 WinForm .Net

发布于 2025-01-01 15:24:54 字数 125 浏览 4 评论 0原文

我需要使用 GeckoFX Web Control 在网页中发送 Enter 按键事件。

我无法使用 SendKeys.Send({ENTER})

有没有办法在网页中通过 Javascript 发送输入?

i need to send the Enter key press Event in a Web Page using GeckoFX Web Control.

I can't use SendKeys.Send({ENTER})

Is there a way to send enter via Javascript in a WebPage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎樣才叫好 2025-01-08 15:24:54

由于您使用的是 geckofx,因此您可以使用 nsIDOMWindowUtils 接口来发送按键。

    var GeckoWebBrowser browser = ...;
    nsIDOMWindowUtils utils = Xpcom.QueryInterface<nsIDOMWindowUtils>(browser.Window.DomWindow);
    using (nsAString type = new nsAString("keypress"))
    {
      utils.SendKeyEvent(type, 0, 13, 0, false);
    }

请注意,通常无法通过普通 JavaScript 使用 nsIDOMWindowUtils 接口,因为它需要 UniversalXPConnect 特权。

Since you are using geckofx you can use the nsIDOMWindowUtils interface to send the keypress.

    var GeckoWebBrowser browser = ...;
    nsIDOMWindowUtils utils = Xpcom.QueryInterface<nsIDOMWindowUtils>(browser.Window.DomWindow);
    using (nsAString type = new nsAString("keypress"))
    {
      utils.SendKeyEvent(type, 0, 13, 0, false);
    }

Note one normally can't use the nsIDOMWindowUtils interface from normal javascript, as it requires the UniversalXPConnect privileged.

纸短情长 2025-01-08 15:24:54

这是巫术魔法:

C# COM 版本,将 ENTER 按键分派到 GeckoWebBrowser 中的 GeckoNode。
不幸的是我还没有在 GeckoFX 18 中找到合适的包装器,所以所有工作都是通过 xpcom 完成的。

13 是 Enter 的代码,如果需要发送字符,请将其设置为 0 并将字符代码用作 InitKeyEvent 的最后一个参数

。这里 e 是您要向其发送按键的对象。

 nsIDOMKeyEvent Event = Xpcom.QueryInterface<nsIDOMKeyEvent>(Browser.Window.DomWindow.GetDocumentAttribute().CreateEvent(new nsAString("KeyEvents")));
 Event.InitKeyEvent(new nsAString("keypress"), true, true, Browser.Window.DomWindow, false, false, false, false, (uint)13, (uint)0);
 Xpcom.QueryInterface<nsIDOMEventTarget>(e.DomObject).DispatchEvent(Event);

JavaScript 版本,如果您可以注入 javascript,这将在 javascript 环境中执行相同的操作

 var Event = document.createEvent("KeyEvents");
 Event.initKeyEvent('keypress', true, true, window, false, false, false, false, 13, 0);
 e.dispatchEvent(Event);

here is the voodoo magic:

C# COM version, dispatches ENTER keypress to GeckoNode in GeckoWebBrowser.
Unfortunately i havent found suitable wrapper in the GeckoFX 18, so all work is done via xpcom.

13 is the code for enter, if you need to send characters, set it to 0 and use the charcode as the last parameter to InitKeyEvent

Here e is the object youre dispatching key press to.

 nsIDOMKeyEvent Event = Xpcom.QueryInterface<nsIDOMKeyEvent>(Browser.Window.DomWindow.GetDocumentAttribute().CreateEvent(new nsAString("KeyEvents")));
 Event.InitKeyEvent(new nsAString("keypress"), true, true, Browser.Window.DomWindow, false, false, false, false, (uint)13, (uint)0);
 Xpcom.QueryInterface<nsIDOMEventTarget>(e.DomObject).DispatchEvent(Event);

JavaScript version, if you can inject javascript this will do the same from within javascript environment

 var Event = document.createEvent("KeyEvents");
 Event.initKeyEvent('keypress', true, true, window, false, false, false, false, 13, 0);
 e.dispatchEvent(Event);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文