TextDocumentKeyPressEvents 介面

定義與按下按鍵關聯的事件。 請使用這個物件的功能,並且參考 TextDocumentKeyPressEventsClass,以取得此物件成員的說明文件。

命名空間:  EnvDTE80
組件:  EnvDTE80 (在 EnvDTE80.dll 中)

語法

'宣告
<GuidAttribute("505B7600-8FCC-487C-9E4F-C7FD0B5FB690")> _
Public Interface TextDocumentKeyPressEvents _
    Inherits _TextDocumentKeyPressEvents, _dispTextDocumentKeyPressEvents_Event
[GuidAttribute("505B7600-8FCC-487C-9E4F-C7FD0B5FB690")]
public interface TextDocumentKeyPressEvents : _TextDocumentKeyPressEvents, 
    _dispTextDocumentKeyPressEvents_Event
[GuidAttribute(L"505B7600-8FCC-487C-9E4F-C7FD0B5FB690")]
public interface class TextDocumentKeyPressEvents : _TextDocumentKeyPressEvents, 
    _dispTextDocumentKeyPressEvents_Event
[<GuidAttribute("505B7600-8FCC-487C-9E4F-C7FD0B5FB690")>]
type TextDocumentKeyPressEvents =  
    interface
        interface _TextDocumentKeyPressEvents
        interface _dispTextDocumentKeyPressEvents_Event
    end
public interface TextDocumentKeyPressEvents extends _TextDocumentKeyPressEvents, _dispTextDocumentKeyPressEvents_Event

TextDocumentKeyPressEvents 型別會公開下列成員。

方法

  名稱 說明
公用方法 add_AfterKeyPress 基礎架構。僅限 Microsoft 內部使用。 (繼承自 _dispTextDocumentKeyPressEvents_Event)。
公用方法 add_BeforeKeyPress 基礎架構。僅限 Microsoft 內部使用。 (繼承自 _dispTextDocumentKeyPressEvents_Event)。
公用方法 remove_AfterKeyPress 基礎架構。僅限 Microsoft 內部使用。 (繼承自 _dispTextDocumentKeyPressEvents_Event)。
公用方法 remove_BeforeKeyPress 基礎架構。僅限 Microsoft 內部使用。 (繼承自 _dispTextDocumentKeyPressEvents_Event)。

回頁首

事件

  名稱 說明
公用事件 AfterKeyPress 基礎架構。僅限 Microsoft 內部使用。 (繼承自 _dispTextDocumentKeyPressEvents_Event)。
公用事件 BeforeKeyPress 基礎架構。僅限 Microsoft 內部使用。 (繼承自 _dispTextDocumentKeyPressEvents_Event)。

回頁首

範例

這個範例會建立小型的字串字典,並利用這個字典透過連接 BeforeKeyPress 事件的方式,自動將表示為顏色之英文字翻譯為十六進位的表示法。 以下列程式碼取代 Connect.cs 檔案中的程式碼。 執行這個增益集,並在 Visual Studio 整合式開發環境 (IDE) 中開啟文字文件。 在這個文字文件中輸入 "red"、"green" 或 "blue",便可看到這個事件擷取方法會分別將所輸入的文字翻譯為 "#ff0000"、"#00cc00" 和 "#0000ff"。 如需如何執行 Automation 範例的詳細資訊,請參閱 HOW TO:編譯和執行 Automation 物件模型程式碼範例

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 from 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;
  }
}

請參閱

參考

EnvDTE80 命名空間