Freigeben über


TextDocumentKeyPressEventsClass.BeforeKeyPress-Ereignis

Wird für jeden Tastendruck ausgelöst, mit dem Zeichen im Text-Editor hinzugefügt oder entfernt werden.

Namespace:  EnvDTE80
Assembly:  EnvDTE80 (in EnvDTE80.dll)

Syntax

'Declaration
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 unterstützt keine Ereignisse.

Implementiert

_dispTextDocumentKeyPressEvents_Event.BeforeKeyPress

Hinweise

BeforeKeyPress tritt ein, bevor der Editor (oder andere Filter) Verarbeitungsvorgänge für die Taste ausführen. Die Benutzer können das weitere durch den Tastendruck verursachte Verhalten abbrechen (einschließlich der Anzeige des Zeichens im Editor), indem sie den Wert von Cancel auf true festlegen.

Beispiele

In diesem Beispiel wird ein kleines Wörterbuch für Zeichenfolgen erstellt, das zur automatischen Umwandlung einiger englischer Begriffe für Farben in die jeweils entsprechende hexadezimale Darstellung verwendet wird. Dabei wird eine Verbindung mit einem BeforeKeyPress-Ereignis hergestellt. Ersetzen Sie den Code in der Datei Connect.cs durch den folgenden Beispielcode. Führen Sie dieses Add-In aus, und öffnen Sie in der integrierten Entwicklungsumgebung (IDE) von Visual Studio ein Textdokument. Geben Sie im Textdokument "red", "green" oder "blue" ein, um zu beobachten, wie die BeforeKeyPress-Ereigniserfassungsmethode den Text in "#ff0000", "#00cc00" oder "#0000ff" übersetzt. Weitere Informationen zum Ausführen der Automatisierungsbeispiele finden Sie unter Gewusst wie: Kompilieren und Ausführen der Codebeispiele für das Automatisierungsobjektmodell.

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-Sicherheit

Siehe auch

Referenz

TextDocumentKeyPressEventsClass Klasse

EnvDTE80-Namespace

TextDocumentKeyPressEvents