Share via


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”的事件捕获方法。 有关如何运行自动化示例的更多信息,请参见 如何:编译和运行自动化对象模型代码示例

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 命名空间