Reputation: 455
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?
Upvotes: 0
Views: 2051
Reputation: 11
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);
Upvotes: 1
Reputation: 6719
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.
Upvotes: 1