2012-02-03 99 views

回答

1

由于您使用的是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特权。

1

这里是巫术:

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

13是输入代码,如果你需要发送的字符,将其设置为0,并使用中charCode作为最后一个参数InitKeyEvent

e是调度按键对象的youre。

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