다음을 통해 공유


TextDocumentKeyPressEventsClass.BeforeKeyPress 이벤트

텍스트 편집기에서 문자를 추가하거나 제거하는 모든 키 누름에 대해 발생합니다.

네임스페이스:  EnvDTE80
어셈블리:  EnvDTE80(EnvDTE80.dll)

구문

‘선언
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에서는 이벤트를 지원하지 않습니다.

구현

_dispTextDocumentKeyPressEvents_Event.BeforeKeyPress

설명

BeforeKeyPress는 편집기나 기타 필터에서 키에 대한 작업을 처리하기 전에 발생합니다.Cancel의 값을 true로 설정하면 편집기에 나타나는 문자를 포함하여 키 누름으로 발생할 수 있는 이후의 모든 동작을 사용자가 취소할 수 있습니다.

예제

이 예제에서는 작은 문자열 사전을 만들고 BeforeKeyPress 이벤트에 연결한 다음 이 사전을 사용하여 색상을 나타내는 몇 가지 영어 단어를 해당 16진수 표현으로 자동 변환합니다.Connect.cs 파일의 코드를 아래에 있는 예제 코드로 바꿉니다.Visual Studio IDE(통합 개발 환경)에서 이 추가 기능을 실행하고 텍스트 문서를 엽니다.텍스트 문서에 "red", "green" 또는 "blue"를 입력하면 BeforeKeyPress 메서드를 캡처하는 이벤트를 통해 텍스트가 "#ff0000", "#00cc00" 또는 "#0000ff"로 변환됩니다.자동화 예제를 실행하는 방법에 대한 자세한 내용은 방법: 자동화 개체 모델 코드의 예제 컴파일 및 실행을 참조하십시오.

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 보안

  • 직접 실행 호출자의 경우 완전히 신뢰합니다. 이 멤버는 부분적으로 신뢰할 수 있는 코드에서 사용할 수 없습니다. 자세한 내용은 부분 신뢰 코드에서 라이브러리 사용을 참조하십시오.

참고 항목

참조

TextDocumentKeyPressEventsClass 클래스

EnvDTE80 네임스페이스

TextDocumentKeyPressEvents