代码改变世界

Geckofx 45 正确模拟键盘输入的事件绑定方法

2017-04-26 14:10  Dorisoy  阅读(1775)  评论(1编辑  收藏  举报
          var inputs = selection.GetElementsByTagName("input").Select(p => p as Gecko.DOM.GeckoInputElement).Where(p => p.GetAttribute("placeholder") == "Stake");
                    foreach (var input in inputs)
                    {
                        var curInput = input as Gecko.DOM.GeckoInputElement;

                        //curInput.SetAttribute("value", bet.BetAmount.ToString("0"));
                        #region
                        DomEventArgs ev = myGeckoWebBrowser.Document.CreateEvent("KeyEvents");
                        var webEvent = new Event(myGeckoWebBrowser.Window.DomWindow, ev.DomEvent as nsISupports);
                        //add focus event
                        webEvent.InitEvent("focus", true, false);
                        curInput.GetEventTarget().DispatchEvent(ev);
                        //.46
                        //0-9  48~57
                        //add keypress event
                        var nsIEventVar = ((nsIDOMKeyEvent)ev.DomEvent);
                        char[] amountChars = bet.BetAmount.ToString().ToCharArray();
                        foreach (var vchar in amountChars)
                        {
                            int keyCode = CommonHelper.ToKeyValue(vchar);
                            nsIEventVar.InitKeyEvent(new nsAString("keypress"), true, false, myGeckoWebBrowser.Window.DomWindow, false, false, false, false, (uint)keyCode, (uint)keyCode);
                            Xpcom.QueryInterface<nsIDOMEventTarget>(curInput.DomObject).DispatchEvent(nsIEventVar);
                        }
                        //add blur event
                        webEvent.InitEvent("blur", true, false);
                        curInput.GetEventTarget().DispatchEvent(ev);
                        #endregion
                        Application.DoEvents();
                        isetAmount = true;
                    }