Visual Studio intercepts F1 help command

I need to write a visual studio add-on that can intercept the default online help command and grab the MSDN library URL when calling F1 help in a class or type.

For example, I put the cursor on the keyword line and press F1 , it usually automatically opens the browser and goes to the help documentation for the type of link. I want to capture the URL passed to the browser before it reaches the browser.

Is it possible to write a visual studio addin / extension that can intercept the default F1 help command?

If above you can make any instructions as to where to start?

+7
source share
2 answers

About 10 years ago, when I worked at Microsoft, I wrote a specification for the original "Online-F1" function in Visual Studio 2005. Thus, my knowledge is somewhat authoritative, but probably outdated .; -)

You cannot change the URL that Visual Studio uses (at least I don’t know how to change it), but you can just write another add-in that steals the F1 key binding, uses the same help context as the F1 handler defaults to and directs the user to your own URL or application.

Firstly, information on how Online F1 works:

  • components of the Visual Studio IDE keyword identifiers in the "Help context F1", which is a bag of properties of information about what the user is doing: for example, the current selection in the code editor, the type of file being edited, the type of project being edited, etc.

  • when the user presses F1, IDE packages that help the context in the URL and open a browser pointing to the MSDN.

Here is an example URL, in this case, when you press F1 in the VS2012 HTML editor when the CSS width property was selected

msdn.microsoft.com/query/dev11.query? appId=Dev11IDEF1& l=EN-US& k=k(width); k(vs.csseditor); k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.0); k(DevLang-CSS)& rd=true 

The "k" parameter above contains the help context inside the visual studio. The help context contains both “keywords” (text strings) and “attributes” (name / value pairs) that various windows inside Visual Studio use to tell the IDE what the user is doing right now.

The CSS editor clicked on two keywords: the "width" that I selected, and the "vs .csseditor", which MSDN can use as "backup" if, for example, my choice is not found in MSDN.

There are also some context filtering attributes:

 TargetFrameworkMoniker = NETFramework,Version=v4.0 DevLang=CSS 

This ensures that F1 loads the page for the correct language or technology, in this case CSS. (Another filter for .NET 4.0 exists because the project I downloaded is targeting .NET 4.0)

Note that the context is streamlined. The keyword "width" is more important than those below it.

The actual help content on MSDN contains metadata (manually set by the commands that create the documentation) that contain the keywords and name / value context properties associated with this page. For example, the css width documentation in MSDN, when it is stored on MSDN servers, has a list of keywords associated with it (in this case: "width") and a list of contextual properties (in this case: "DevLang = CSS"). Pages can have several keywords (for example, "System.String", "String") and several context properties (for example, "DevLang = C #", "DevLang = VB", etc.).

When a list of keywords falls into the MSDN Online F1 service, the algorithm is similar to this, with the caveat that it may change over the past few years:

  • take the first keyword
  • find all pages that match this keyword
  • exclude all pages that match the name of the context attribute (for example, "DevLang") but do not have a match for the value. This, for example, would exclude the Control.Width page, because it would be marked as "DevLang = C #", "DevLang = VB". But that would not exclude pages without the DevLang attribute.
  • If there are no results left, but more keywords are left, start C # 1 again with the next keyword (in order), if you have no keywords left. If there are no keywords, perform a “backup” operation, which may return a list of MSDN search results, may display “page cannot be found,” or some other solution.
  • Separate the remaining results. I don’t remember the exact ranking algorithm, and it probably has changed since then, but I believe that the general idea was to show pages that first met higher specifications and showed more popular matches first.
  • Show the highest score in the browser

Here is sample code for the Visual Studio Add-in:

  • take the keyword F1
  • when F1 is pressed, get the help context and turn it into a set of name = value pairs
  • pass this set of name = value pairs to some external code to do something with the F1 request.

I leave all the code for the Visual Studio add-in template - if you need it too, there should be many examples in Google.

 using System; using Extensibility; using EnvDTE; using EnvDTE80; using Microsoft.VisualStudio.CommandBars; using System.Resources; using System.Reflection; using System.Globalization; using System.Collections; using System.Collections.Generic; using System.Text; namespace ReplaceF1 { /// <summary>The object for implementing an Add-in.</summary> /// <seealso class='IDTExtensibility2' /> public class Connect : IDTExtensibility2, IDTCommandTarget { /// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary> public Connect() { } MsdnExplorer.MainWindow Explorer = new MsdnExplorer.MainWindow(); /// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary> /// <param term='application'>Root object of the host application.</param> /// <param term='connectMode'>Describes how the Add-in is being loaded.</param> /// <param term='addInInst'>Object representing this Add-in.</param> /// <seealso class='IDTExtensibility2' /> public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; if(connectMode == ext_ConnectMode.ext_cm_UISetup) { object []contextGUIDS = new object[] { }; Commands2 commands = (Commands2)_applicationObject.Commands; string toolsMenuName; try { // If you would like to move the command to a different menu, change the word "Help" to the // English version of the menu. This code will take the culture, append on the name of the menu // then add the command to that menu. You can find a list of all the top-level menus in the file // CommandBar.resx. ResourceManager resourceManager = new ResourceManager("ReplaceF1.CommandBar", Assembly.GetExecutingAssembly()); CultureInfo cultureInfo = new System.Globalization.CultureInfo(_applicationObject.LocaleID); string resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Help"); toolsMenuName = resourceManager.GetString(resourceName); } catch { //We tried to find a localized version of the word Tools, but one was not found. // Default to the en-US word, which may work for the current culture. toolsMenuName = "Help"; } //Place the command on the tools menu. //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items: Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["MenuBar"]; //Find the Tools command bar on the MenuBar command bar: CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName]; CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl; //This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in, // just make sure you also update the QueryStatus/Exec method to include the new command names. try { //Add a command to the Commands collection: Command command = commands.AddNamedCommand2(_addInInstance, "ReplaceF1", "MSDN Advanced F1", "Brings up context-sensitive Help via the MSDN Add-in", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton); command.Bindings = new object[] { "Global::F1" }; } catch(System.ArgumentException) { //If we are here, then the exception is probably because a command with that name // already exists. If so there is no need to recreate the command and we can // safely ignore the exception. } } } /// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary> /// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param> /// <param term='custom'>Array of parameters that are host application specific.</param> /// <seealso class='IDTExtensibility2' /> public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) { } /// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary> /// <param term='custom'>Array of parameters that are host application specific.</param> /// <seealso class='IDTExtensibility2' /> public void OnAddInsUpdate(ref Array custom) { } /// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary> /// <param term='custom'>Array of parameters that are host application specific.</param> /// <seealso class='IDTExtensibility2' /> public void OnStartupComplete(ref Array custom) { } /// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary> /// <param term='custom'>Array of parameters that are host application specific.</param> /// <seealso class='IDTExtensibility2' /> public void OnBeginShutdown(ref Array custom) { } /// <summary>Implements the QueryStatus method of the IDTCommandTarget interface. This is called when the command availability is updated</summary> /// <param term='commandName'>The name of the command to determine state for.</param> /// <param term='neededText'>Text that is needed for the command.</param> /// <param term='status'>The state of the command in the user interface.</param> /// <param term='commandText'>Text requested by the neededText parameter.</param> /// <seealso class='Exec' /> public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText) { if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone) { if(commandName == "ReplaceF1.Connect.ReplaceF1") { status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported|vsCommandStatus.vsCommandStatusEnabled; return; } } } /// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary> /// <param term='commandName'>The name of the command to execute.</param> /// <param term='executeOption'>Describes how the command should be run.</param> /// <param term='varIn'>Parameters passed from the caller to the command handler.</param> /// <param term='varOut'>Parameters passed from the command handler to the caller.</param> /// <param term='handled'>Informs the caller if the command was handled or not.</param> /// <seealso class='Exec' /> public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault) { if (commandName == "ReplaceF1.Connect.ReplaceF1") { // Get a reference to Solution Explorer. Window activeWindow = _applicationObject.ActiveWindow; ContextAttributes contextAttributes = activeWindow.DTE.ContextAttributes; contextAttributes.Refresh(); List<string> attributes = new List<string>(); try { ContextAttributes highPri = contextAttributes == null ? null : contextAttributes.HighPriorityAttributes; highPri.Refresh(); if (highPri != null) { foreach (ContextAttribute CA in highPri) { List<string> values = new List<string>(); foreach (string value in (ICollection)CA.Values) { values.Add(value); } string attribute = CA.Name + "=" + String.Join(";", values.ToArray()); attributes.Add(CA.Name + "="); } } } catch (System.Runtime.InteropServices.COMException e) { // ignore this exception-- means there no High Pri values here string x = e.Message; } catch (System.Reflection.TargetInvocationException e) { // ignore this exception-- means there no High Pri values here string x = e.Message; } catch (System.Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); // ignore this exception-- means there no High Pri values here string x = e.Message; } // fetch context attributes that are not high-priority foreach (ContextAttribute CA in contextAttributes) { List<string> values = new List<string>(); foreach (string value in (ICollection)CA.Values) { values.Add (value); } string attribute = CA.Name + "=" + String.Join(";", values.ToArray()); attributes.Add (attribute); } // Replace this call with whatever you want to do with the help context info HelpHandler.HandleF1 (attributes); } } } private DTE2 _applicationObject; private AddIn _addInInstance; } } 
+8
source

Everything is very interesting, but potentially more designed? I have a programmable mouse, like most. I set one of the buttons to search. those. click on a word and the browser will open in your favorite search engine for that word. This list usually contains MSDN help. AS are SO links. I like efficient and simple souls :-)

+1
source

All Articles