Partager via


TextDocumentKeyPressEventsClass.BeforeKeyPress, événement

Mise à jour : novembre 2007

Déclenché pour toutes les actions de touche qui ajoutent ou suppriment des caractères dans l'éditeur de texte.

Espace de noms :  EnvDTE80
Assembly :  EnvDTE80 (dans EnvDTE80.dll)

Syntaxe

Public Overridable Event BeforeKeyPress As _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler

Dim instance As TextDocumentKeyPressEventsClass
Dim handler As _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler

AddHandler instance.BeforeKeyPress, handler
public virtual event _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler BeforeKeyPress
public:
virtual  event _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler^ BeforeKeyPress {
    void add (_dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler^ value);
    void remove (_dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler^ value);
}
JScript ne prend pas en charge les événements.

Implémentations

_dispTextDocumentKeyPressEvents_Event.BeforeKeyPress

Notes

BeforeKeyPress se produit avant que l'éditeur ou d'autres filtres n'effectuent le moindre traitement de la touche. L'utilisateur peut annuler tout autre comportement pouvant être provoqué par l'action de la touche (y compris l'affichage du caractère correspondant dans l'éditeur) en définissant la valeur true à Cancel.

Exemples

Cet exemple crée un petit dictionnaire de chaînes et l'utilise pour traduire automatiquement des noms de couleurs en anglais en leur représentation hexadécimale en se connectant à un événement BeforeKeyPress. Remplacez le code du fichier Connect.cs par l'exemple de code ci-dessous. Exécutez ce complément et ouvrez un document texte dans l'environnement de développement intégré (IDE) Visual Studio. Dans le document texte, tapez "red", "green" ou "blue" pour observer la méthode BeforeKeyPress à capture d'événement traduire le texte en "#ff0000", "#00cc00" et "#0000ff." Pour plus d'informations sur l'exécution des exemples, consultez Comment : compiler et exécuter les exemples de code du modèle objet 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 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;
  }
}

Autorisations

Voir aussi

Référence

TextDocumentKeyPressEventsClass, classe

Membres TextDocumentKeyPressEventsClass

EnvDTE80, espace de noms

TextDocumentKeyPressEvents