Megosztás a következőn keresztül:


TextDocumentKeyPressEventsClass.BeforeKeyPress Event

Raised for all key presses that add or remove characters in the Text Editor.

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

Syntax

'Declaration
Public Overridable Event BeforeKeyPress As _dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler
'Usage
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 does not support events.

Implements

_dispTextDocumentKeyPressEvents_Event.BeforeKeyPress

Remarks

BeforeKeyPress occurs before the editor (or other filters) do any processing on the key. The user may cancel any further behavior that may be caused by the key press (including the character appearing in the editor) by setting the value of Cancel to true.

Examples

This example creates a small string dictionary and uses it to automatically translate some English words for colors to their hexadecimal representations by connecting to a BeforeKeyPress event. Replace the code in the Connect.cs file with the example code below. Run this add-in and open a text document in the Visual Studio integrated development environment (IDE). In the text document type "red", "green", or "blue" to see the event capturing BeforeKeyPress method translate the text to "#ff0000", "#00cc00" and "#0000ff". For more information about how to run the automation examples, see How to: Compile and Run the Automation Object Model Code Examples.

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 Security

See Also

Reference

TextDocumentKeyPressEventsClass Class

TextDocumentKeyPressEventsClass Members

EnvDTE80 Namespace

TextDocumentKeyPressEvents