TextDocumentKeyPressEventsClass.BeforeKeyPress 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
对添加或删除“文本编辑器”中的字符的所有按键操作引发。
public:
virtual event EnvDTE80::_dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler ^ BeforeKeyPress;
public virtual event EnvDTE80._dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler BeforeKeyPress;
member this.BeforeKeyPress : EnvDTE80._dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler
Public Overridable Custom Event BeforeKeyPress As _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler Implements BeforeKeyPress
事件类型
实现
示例
此示例将创建一个小的字符串字典,并使用它通过连接到事件来自动将颜色的某些英语字词转换为其十六进制表示形式 BeforeKeyPress 。 将 Connect.cs 文件中的代码替换为下面的示例代码。 在集成开发环境中,运行此示例并打开文本文档 Visual Studio (IDE) 。 在 "红色"、"绿色" 或 "蓝色" 文本文档中,查看事件捕获 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;
}
}
注解
BeforeKeyPress 在编辑器 (或其他筛选器) 对该项执行任何处理之前发生。 用户可能会通过将的值设置为,取消按键 (包括显示在编辑器) 中的字符等任何其他行为 Cancel true 。