Compartir a través de


TextDocumentKeyPressEventsClass.BeforeKeyPress (Evento)

Se inicia para todas las teclas presionadas que agregan o quitan caracteres en el editor de texto.

Espacio de nombres:  EnvDTE80
Ensamblado:  EnvDTE80 (en EnvDTE80.dll)

Sintaxis

'Declaración
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 no admite eventos.

Implementaciones

_dispTextDocumentKeyPressEvents_Event.BeforeKeyPress

Comentarios

BeforeKeyPress se produce antes de que el editor (u otro filtro) haga cualquier tipo de procesamiento de la tecla. El usuario puede cancelar cualquier comportamiento que pueda derivarse de presionar la tecla (incluso el carácter que aparece en el editor) estableciendo el valor de Cancel en true.

Ejemplos

Este ejemplo crea un pequeño diccionario de cadenas y lo utiliza para traducir automáticamente varios términos ingleses de colores a sus correspondientes representaciones hexadecimales conectándose a un evento BeforeKeyPress. Reemplace el código del archivo Connect.cs por el código de ejemplo siguiente. Ejecute este complemento y abra un documento de texto en el entorno de desarrollo integrado (IDE) de Visual Studio. En el documento de texto, escriba "red", "green" o "blue" para ver cómo el evento que captura el método BeforeKeyPress convierte el texto en "#ff0000", "#00cc00" y "#0000ff". Para obtener más información sobre cómo ejecutar ejemplos de automatización, vea Cómo: Compilar y ejecutar los ejemplos de código del modelo de objetos de automatización.

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

Seguridad de .NET Framework

Vea también

Referencia

TextDocumentKeyPressEventsClass Clase

EnvDTE80 (Espacio de nombres)

TextDocumentKeyPressEvents