かなウェルでHキー押下時、「とは」検索してみました。
「コクリコ坂から」を選択後・・・
WebBroserのキー押下検出には、OnPreviewKeyDownを使います。
「OnEnterPressWithTextSelection」("Enter"とはなっていますが、"H"の間違いです)レイズ先メソッドで、検索バーに選択文字列+"とは"をセットしています。
public enum ETabWebCurrentFocusControl { TEXTBOX, PASSWORDBOX, TEXTARA, ELSE } #region "H"ボタン押下時、選択文字列+"とは"でGoogle検索 //選択文字列を引数にHボタン押下イベントを発生させる。 public event ActionOnEnterPressWithTextSelection; //2回連続で同じ選択文字列を検索しないようにするため、 //1回目の選択文字列をこの変数に保存 protected string prevSelectedText = null; protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e) { base.OnPreviewKeyDown(e); //Hボタン押下 if ( e.KeyCode == Keys.H ) { string strgSelected = null; //選択文字列をクリップボードにコピー this.Document.ExecCommand("Copy", false, null); //クリップボードから文字列取得 try { strgSelected = Clipboard.GetText(); } catch (Exception excp) { return; } //文字列が空でなく、かつ、2回連続同じ文字列でない場合、「Hボタン押下イベント」を発生させる。 if (!string.IsNullOrEmpty(strgSelected) && (this.prevSelectedText != strgSelected)) { //現在フォーカスのあるコントロールが、 //TextBox,Passowrd,TextArea以外の場合のみイベントを発生させる。 if (this.ETabWebCFC == ETabWebCurrentFocusControl.ELSE) { //今回の文字列を保存 this.prevSelectedText = strgSelected; //Hボタン押下イベントを発生させる this.OnEnterPressWithTextSelection(strgSelected); } } } } //TextBox, Password, TextArea にフォーカスが入ったら、フラグをたてる //フォーカスを失ったら、フラグを下ろす private ETabWebCurrentFocusControl ETabWebCFC = ETabWebCurrentFocusControl.ELSE; protected void evOnTextBoxAreaGotFocus(object sender, EventArgs e) { HtmlElement el = sender as HtmlElement; if (el == null) { return; } //フラグを立てる if (el.GetAttribute("type").ToString().ToLower() == "text") { this.ETabWebCFC = ETabWebCurrentFocusControl.TEXTBOX; } else if (el.GetAttribute("type").ToString().ToLower() == "password") { this.ETabWebCFC = ETabWebCurrentFocusControl.PASSWORDBOX; } else if (el.GetAttribute("type").ToString().ToLower() == "textarea") { this.ETabWebCFC = ETabWebCurrentFocusControl.TEXTARA; } else { this.ETabWebCFC = ETabWebCurrentFocusControl.ELSE; } } protected void evOnTextBoxAreaLostFocus(object sender, EventArgs e) { //フラグを下ろす this.ETabWebCFC = ETabWebCurrentFocusControl.ELSE; } #endregion
上のソースの「evOnTextBoxAreaGotFocus」と「evOnTextBoxAreaLostFocus」イベントハンドラは、
「OnDocumentCompleted」イベント発生時に、以下の「ChainOnDocumentCompleted」を、呼び出す時に、セットしています。
(「HtmlElementEventHandler」は無視して下さい。)
protected void ChainOnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e) { //zoom this.ChangeZoomSize(this.zoomSize); //base.OnDocumentCompleted(e); HtmlElementCollection ecol = this.Document.Body.GetElementsByTagName("input"); foreach (HtmlElement el in ecol) { if (el.GetAttribute("type").ToString().ToLower() == "text" || el.GetAttribute("type").ToString().ToLower() == "password") { el.Click += new HtmlElementEventHandler(MyTabWebBrowser_MyCustomClick); el.GotFocus += this.evOnTextBoxAreaGotFocus; el.LostFocus += this.evOnTextBoxAreaLostFocus; } } HtmlElementCollection ecol2 = this.Document.Body.GetElementsByTagName("textarea"); foreach (HtmlElement el in ecol2) { el.Click += new HtmlElementEventHandler(MyTabWebBrowser_MyCustomClick); el.GotFocus += this.evOnTextBoxAreaGotFocus; el.LostFocus += this.evOnTextBoxAreaLostFocus; } HtmlElementCollection ecol3 = this.Document.All; foreach (HtmlElement el in ecol3) { if (el.TagName.ToLower() != "input" && el.TagName.ToLower() != "textarea") { el.MouseEnter += this.MyOnMouseMoveEnter; } } }