共用方式為


TextDocumentKeyPressEvents Interface

Definition

Defines events associated with a key press. Use this object for functionality and refer to TextDocumentKeyPressEventsClass for this object's member documentation.

public interface class TextDocumentKeyPressEvents : EnvDTE80::_dispTextDocumentKeyPressEvents_Event, EnvDTE80::_TextDocumentKeyPressEvents
public interface class TextDocumentKeyPressEvents : EnvDTE80::_dispTextDocumentKeyPressEvents_Event, EnvDTE80::_TextDocumentKeyPressEvents
__interface TextDocumentKeyPressEvents : EnvDTE80::_dispTextDocumentKeyPressEvents_Event, EnvDTE80::_TextDocumentKeyPressEvents
[System.Runtime.InteropServices.CoClass(typeof(EnvDTE80.TextDocumentKeyPressEventsClass))]
[System.Runtime.InteropServices.Guid("505B7600-8FCC-487C-9E4F-C7FD0B5FB690")]
public interface TextDocumentKeyPressEvents : EnvDTE80._dispTextDocumentKeyPressEvents_Event, EnvDTE80._TextDocumentKeyPressEvents
[System.Runtime.InteropServices.CoClass(typeof(EnvDTE80.TextDocumentKeyPressEventsClass))]
[System.Runtime.InteropServices.Guid("505B7600-8FCC-487C-9E4F-C7FD0B5FB690")]
[System.Runtime.InteropServices.ComVisible(false)]
public interface TextDocumentKeyPressEvents : EnvDTE80._dispTextDocumentKeyPressEvents_Event, EnvDTE80._TextDocumentKeyPressEvents
[<System.Runtime.InteropServices.CoClass(typeof(EnvDTE80.TextDocumentKeyPressEventsClass))>]
[<System.Runtime.InteropServices.Guid("505B7600-8FCC-487C-9E4F-C7FD0B5FB690")>]
type TextDocumentKeyPressEvents = interface
    interface _TextDocumentKeyPressEvents
    interface _dispTextDocumentKeyPressEvents_Event
[<System.Runtime.InteropServices.CoClass(typeof(EnvDTE80.TextDocumentKeyPressEventsClass))>]
[<System.Runtime.InteropServices.Guid("505B7600-8FCC-487C-9E4F-C7FD0B5FB690")>]
[<System.Runtime.InteropServices.ComVisible(false)>]
type TextDocumentKeyPressEvents = interface
    interface _TextDocumentKeyPressEvents
    interface _dispTextDocumentKeyPressEvents_Event
Public Interface TextDocumentKeyPressEvents
Implements _dispTextDocumentKeyPressEvents_Event, _TextDocumentKeyPressEvents
Derived
Attributes
Implements

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 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 method translate the text to "#ff0000", "#00cc00", and "#0000ff", respectively.

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

Methods

add_AfterKeyPress(_dispTextDocumentKeyPressEvents_AfterKeyPressEventHandler) (Inherited from _dispTextDocumentKeyPressEvents_Event)
add_BeforeKeyPress(_dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler) (Inherited from _dispTextDocumentKeyPressEvents_Event)
remove_AfterKeyPress(_dispTextDocumentKeyPressEvents_AfterKeyPressEventHandler) (Inherited from _dispTextDocumentKeyPressEvents_Event)
remove_BeforeKeyPress(_dispTextDocumentKeyPressEvents_BeforeKeyPressEventHandler) (Inherited from _dispTextDocumentKeyPressEvents_Event)

Events

AfterKeyPress

This API supports the product infrastructure and is not intended to be used directly from your code.

Microsoft Internal Use Only.

(Inherited from _dispTextDocumentKeyPressEvents_Event)
BeforeKeyPress

This API supports the product infrastructure and is not intended to be used directly from your code.

Microsoft Internal Use Only.

(Inherited from _dispTextDocumentKeyPressEvents_Event)

Applies to