I try to redirect the XButton 1 and 2 (Side Buttons of the mouse) to specific Visual Studio actions.
When I press the XButton1 I want to compile the project / build it. This action is bound to F6 by default.
When I press the XButton2 I want to switch between code and design view (WinForms). This is bound to F7.
After several attempts using using Visual Studio built-in tools, I created the following script using AutoHotKey:
XButton2::
IfWinActive Microsoft Visual Studio
{
Send {F7}
return
}
XButton1::
IfWinActive Microsoft Visual Studio
{
Send {F6}
return
}
However I am wondering if someone knows a native way of achieving the same with Visual Studio 2015?
Solution
The main idea is registering a global mouse hook and handling desired mouse event and run a visual studio command. To do so:
SetWindowsHookEx
by passingWH_MOUSE_LL
and handle desired mouse event, for exampleWM_XBUTTONDOWN
. Perform registration when the solution loads.Run desired Visual Studio Command using
DTE.ExecuteCommand
passing suitable command, for exampleBuild.BuildSolution
:UnhookWindowsHookEx
when the solution closed.Note:
To find the command that you need, go to
Tools → Options → Environment → KeyBoard
and find the command which you need.You will find lots of resources about how to register a global mouse hook like this which I changed a bit and used for test. At the end of the post you can find full source code.
In Visual Studio 2013 Add ins are deprecated, so while you can do the same using a Visual Studio Add-in project but it's better to do it using VSPackages.
Implementaion
Start by creating a Visual Studio Package project and change the code of package to the code which I post here. Also add the classes which I used for global mouse hook and windows API to the solution.
Package
Here is the full code for my Package:
Windows API messages, structures and methods
Global Mouse Hook
Since I don't have XButton in my mouse, I handled
WM_RBUTTONDOWN
event.