次の方法で共有


TextDocumentKeyPressEventsClass.BeforeKeyPress イベント

テキスト エディターで文字を追加したり削除したりするような、キーを押すすべての処理に対して発生します。

名前空間:  EnvDTE80
アセンブリ:  EnvDTE80 (EnvDTE80.dll 内)

構文

'宣言
Public Overridable Event BeforeKeyPress As _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler
public virtual event _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler BeforeKeyPress
public:
virtual  event _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler^ BeforeKeyPress {
    void add (_dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler^ value);
    void remove (_dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler^ value);
}
abstract BeforeKeyPress : IEvent<_dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler,
    EventArgs>
override BeforeKeyPress : IEvent<_dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler,
    EventArgs>
JScript では、イベントは使用できません。

実装

_dispTextDocumentKeyPressEvents_Event.BeforeKeyPress

解説

BeforeKeyPress は、エディター (または他のフィルター) でキーの処理が実行される前に発生します。 ユーザーは、キーを押したことで発生する動作 (エディターに文字が表示されるなど) を、Cancel の値を true に設定することでキャンセルできます。

この例では、小さな文字列ディクショナリを作成して BeforeKeyPress イベントに接続することによって、色に関するいくつかの英単語を 16 進表現に自動変換します。 Connect.cs ファイル内のコードを次のプログラム例で置き換えます。 このアドインを実行し、Visual Studio 統合開発環境 (IDE) でテキスト文書を開きます。 テキスト ドキュメントに「red」、「green」、または「blue」と入力すると、BeforeKeyPress メソッドをキャプチャするイベントによって、テキストが "#ff0000"、"#00cc00"、または "#0000ff" に変換されます。 オートメーションの例の実行方法の詳細については、「方法 : オートメーション オブジェクト モデルのコード例をコンパイルおよび実行する」を参照してください。

namespace myAddin
{
  using System;
  using Microsoft.VisualStudio.CommandBars;
  using Extensibility;
  using EnvDTE;
  using EnvDTE80;
  using System.Windows.Forms;
  public class Connect : Object, IDTExtensibility2
  {
  public Connect()
  {
  }
  public void OnConnection(object application, 
ext_ConnectMode connectMode, object addInInst, ref Array custom)
  {
      _applicationObject = (DTE2)application;
      _addInInstance = (AddIn)addInInst;
      // Create a small string dictionary with keys and corresponding
      // values.
      myStringDictionary = new
 System.Collections.Specialized.StringDictionary();
      myStringDictionary.Add("red", "#ff0000");
      myStringDictionary.Add("green", "#00cc00");
      myStringDictionary.Add("blue", "#0000ff");
      EnvDTE80.Events2 events =
 (EnvDTE80.Events2)_applicationObject.Events;
      textDocKeyEvents =
 (EnvDTE80.TextDocumentKeyPressEvents)
events.get_TextDocumentKeyPressEvents(null);
      // Connect to the BeforeKeyPress delegate exposed by the
      // TextDocumentKeyPressEvents object retrieved above.
      textDocKeyEvents.BeforeKeyPress +=new
 _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler
(BeforeKeyPress);
  }
  public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
  {
      if (textDocKeyEvents != null)
      {
          textDocKeyEvents.BeforeKeyPress -= new 
_dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler
(BeforeKeyPress);
      }
  }
  public void OnAddInsUpdate(ref Array custom)
  {
  }
  public void OnStartupComplete(ref Array custom)
  {
  }
  public void OnBeginShutdown(ref Array custom)
  {
  }
  void BeforeKeyPress(string Keypress, EnvDTE.TextSelection Selection,
 bool InStatementCompletion, ref bool CancelKeypress)
  {
  if ((Keypress == " ") || (Keypress == "\t"))
      {
          EditPoint ep = Selection.ActivePoint.CreateEditPoint();
          EditPoint sp = ep.CreateEditPoint();
          sp.CharLeft(1);
          while (true)
          {
              string txt = sp.GetText(ep);
              if (myStringDictionary.ContainsKey(txt))
              {
                  sp.Delete(txt.Length);
                  sp.Insert(myStringDictionary[txt]);
                  CancelKeypress = true;
                  return;
              }
          sp.CharLeft(1);
          if ((ep.Line != sp.Line) || ((ep.DisplayColumn == 1) 
&& (ep.Line == 1)))
              break;
          }
       }
  }
  private DTE2 _applicationObject;
  private AddIn _addInInstance;
  private EnvDTE80.TextDocumentKeyPressEvents textDocKeyEvents;
  System.Collections.Specialized.StringDictionary myStringDictionary;
  }
}

.NET Framework セキュリティ

  • 直前の呼び出し元に対する完全な信頼。 このメンバーは、部分的に信頼されているコードから使用することはできません。 詳細については、「部分信頼コードからのライブラリの使用」を参照してください。

参照

参照

TextDocumentKeyPressEventsClass クラス

EnvDTE80 名前空間

TextDocumentKeyPressEvents