Logo
HOWTO: Process the Esc or F1 keys in the toolwindow of a Visual Studio .NET add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: August 2011   Microsoft Visual Studio 2008
Updated: March 2013   Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012

Introduction

This article explains how to intercept special keys in a toolwindow to perform special actions such as:

  • F1: shows the help file of the add-in
  • Esc: closes the toolwindow

More Information

An add-in can create toolwindows as described in the article HOWTO: Create a dockable toolwindow from a Visual Studio .NET add-in. As explained in the article, toolwindows created by add-ins require a usercontrol. To intercept special dialog keys the ProcessCmdKey method of the usercontrol can be overriden:

Language: C#   Copy Code Copy Code (IE only)
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
   bool result = false;

   switch (keyData)
   {
      case Keys.Escape:

         // TODO: close the toolwindow
         // (the usercontrol must have a reference to the EnvDTE.Window of its hosting toolwindow and call the Close method)

         result = true;
         break;

      case Keys.F1:

         // TODO: show the help file

         result = true;
         break;

      default:

         result = base.ProcessCmdKey(ref msg, keyData);
         break;

   }

   return result;
}
Language: VB.NET   Copy Code Copy Code (IE only)
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

   Dim result As Boolean = False

   Select Case keyData

      Case Keys.Escape

         ' TODO: close the toolwindow
         ' (the usercontrol must have a reference to the EnvDTE.Window of its hosting toolwindow and call the Close method)

         result = True

      Case Keys.F1

         ' TODO: show the help file

         result = True

      Case Else

         result = MyBase.ProcessCmdKey(msg, keyData)

   End Select

   Return result

End Function

Related articles


Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


Top