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 事件来将某些带有颜色的英语单词自动转换为其十六进制表示形式。 将 Connect.cs 文件中的代码替换为下面的代码示例。 运行此外接程序并在 Visual Studio 集成开发环境 (IDE) 中打开一个文本文档。 在该文本文档中,键入“red”、“green”或“blue”,以查看用来将文本转换为“#ff0000”、“#00cc00”和“#0000ff”的事件捕获 BeforeKeyPress 方法。 有关如何运行自动化示例的更多信息,请参见 如何:编译和运行自动化对象模型代码示例

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