public void SetValueWhenOnKeyPress(string eventName, NameValueCollection eventProperties) { if (eventName != "onKeyPress" || eventProperties == null) { return; } var keys = eventProperties.GetValues("keyCode"); if (keys == null || keys.Length <= 0) { return; } var addChar = keys[0]; var newValue = _ieElement.GetAttributeValue("value") + ((char)int.Parse(addChar)); _ieElement.SetAttributeValue("value", newValue); }
/// <summary> /// Fires the given event on the given element. /// </summary> /// <param name="eventName">Name of the event to fire</param> /// <param name="eventProperties">The event object properties.</param> public void FireEvent(string eventName, NameValueCollection eventProperties) { if (eventName == "onKeyPress" && eventProperties != null) { var keys = eventProperties.GetValues("keyCode"); if (keys != null && keys.Length > 0) { var addChar = keys[0]; var newValue = _ieElement.GetAttributeValue("value") + ((char)int.Parse(addChar)); _ieElement.SetAttributeValue("value", newValue); } } var scriptCode = CreateJavaScriptFireEventCode(eventProperties, eventName); try { var window = _ieElement.ParentWindow; IEUtils.RunScript(scriptCode, window); } catch (RunScriptException) { // In a cross domain automation scenario a System.UnauthorizedAccessException // is thrown. This code does cause the registered client event handlers to be fired // but it does not deliver the event to the control itself. Consequently the // control state may need to be updated directly (eg. as with key press event). object prototypeEvent = null; object eventObj = ((IHTMLDocument4)_ieElement.AsHtmlElement.document).CreateEventObject(ref prototypeEvent); for (var index = 0; index < eventProperties.Count; index++) { var property = eventProperties.GetKey(index); var value = eventProperties.GetValues(index)[0]; ((IHTMLEventObj2)eventObj).setAttribute(property, value, 0); } _ieElement.AsDispHTMLBaseElement.FireEvent(eventName, ref eventObj); } }