将鼠标侧键绑定到 VisualStudio 操作
Bind Mouse Side Buttons to VisualStudio Actions
我尝试将 XButton 1 和 2(鼠标的侧键)重定向到特定的 Visual Studio 操作。
当我按下 XButton1 时,我想编译/构建项目。
这个动作默认绑定到F6。
当我按下 XButton2 时,我想在代码和设计视图 (WinForms) 之间切换。这绑定到 F7.
在多次尝试使用 Visual Studio 内置工具后,我使用 AutoHotKey 创建了以下脚本:
XButton2::
IfWinActive Microsoft Visual Studio
{
Send {F7}
return
}
XButton1::
IfWinActive Microsoft Visual Studio
{
Send {F6}
return
}
但是我想知道是否有人知道用 Visual Studio 2015 实现相同目标的本地方法?
解决方案
主要思想是注册一个全局鼠标钩子并处理所需的鼠标事件和运行一个visual studio命令。为此:
- 首先创建一个 Visual Studio Package 项目。
- 使用
SetWindowsHookEx
by passing WH_MOUSE_LL
and handle desired mouse event, for example WM_XBUTTONDOWN
注册全局鼠标挂钩。加载解决方案时执行注册。
运行 需要 Visual Studio 命令使用 DTE.ExecuteCommand
传递合适的命令,例如 Build.BuildSolution
:
var dte = (EnvDTE.DTE)this.GetService(typeof(EnvDTE.DTE));
dte.ExecuteCommand("Build.BuildSolution");
- 不要忘记在解决方案关闭时使用
UnhookWindowsHookEx
取消挂钩。
注:
要找到您需要的命令,请转至 Tools → Options → Environment → KeyBoard
并找到您需要的命令。
您会找到很多关于如何注册全局鼠标挂钩的资源,例如 this,我稍作改动并用于测试。在 post 的末尾,您可以找到完整的源代码。
在 Visual Studio 2013 中已弃用插件,因此虽然您可以使用 Visual Studio Add-in 项目执行相同操作,但最好使用 VSPackages 执行此操作。
实施
首先创建一个Visual Studio Package 项目并将package 的代码更改为我在这里post 的代码。还要将我用于全局鼠标挂钩的 类 和 windows API 添加到解决方案中。
套餐
这是我的包的完整代码:
using Microsoft.VisualStudio.Shell;
using System;
using System.Runtime.InteropServices;
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(GuidList.guidVSPackage1PkgString)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids80.SolutionExists)]
public sealed class VSPackage1Package : Package
{
public VSPackage1Package() { }
EnvDTE.DTE dte;
protected override void Initialize()
{
base.Initialize();
dte = (EnvDTE.DTE)this.GetService(typeof(EnvDTE.DTE));
dte.Events.SolutionEvents.Opened += SolutionEvents_Opened;
dte.Events.SolutionEvents.AfterClosing += SolutionEvents_AfterClosing;
}
void SolutionEvents_AfterClosing() { MouseHook.Stop(); }
void SolutionEvents_Opened()
{
MouseHook.Start();
MouseHook.MouseAction += MouseHook_MouseAction;
}
void MouseHook_MouseAction(object sender, EventArgs e)
{
dte.ExecuteCommand("Build.BuildSolution");
}
}
Windows API 消息、结构和方法
using System;
using System.Runtime.InteropServices;
public class Win32
{
public delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
public const int WH_MOUSE_LL = 14;
public enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201, WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200, WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204, WM_RBUTTONUP = 0x0205
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT { public int x; public int y; }
[StructLayout(LayoutKind.Sequential)]
public struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn,
IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam,
IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
}
全局鼠标挂钩
由于我的鼠标中没有 XButton,因此我处理了 WM_RBUTTONDOWN
事件。
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public static class MouseHook
{
public static event EventHandler MouseAction = delegate { };
private static Win32.LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
public static void Start() { _hookID = SetHook(_proc); }
public static void Stop() { Win32.UnhookWindowsHookEx(_hookID); }
private static IntPtr SetHook(Win32.LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
var handle = Win32.GetModuleHandle(curModule.ModuleName);
return Win32.SetWindowsHookEx(Win32.WH_MOUSE_LL, proc, handle, 0);
}
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 &&
Win32.MouseMessages.WM_RBUTTONDOWN == (Win32.MouseMessages)wParam)
{
Win32.MSLLHOOKSTRUCT hookStruct =
(Win32.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
typeof(Win32.MSLLHOOKSTRUCT));
MouseAction(null, new EventArgs());
}
return Win32.CallNextHookEx(_hookID, nCode, wParam, lParam);
}
}
我尝试将 XButton 1 和 2(鼠标的侧键)重定向到特定的 Visual Studio 操作。
当我按下 XButton1 时,我想编译/构建项目。 这个动作默认绑定到F6。
当我按下 XButton2 时,我想在代码和设计视图 (WinForms) 之间切换。这绑定到 F7.
在多次尝试使用 Visual Studio 内置工具后,我使用 AutoHotKey 创建了以下脚本:
XButton2::
IfWinActive Microsoft Visual Studio
{
Send {F7}
return
}
XButton1::
IfWinActive Microsoft Visual Studio
{
Send {F6}
return
}
但是我想知道是否有人知道用 Visual Studio 2015 实现相同目标的本地方法?
解决方案
主要思想是注册一个全局鼠标钩子并处理所需的鼠标事件和运行一个visual studio命令。为此:
- 首先创建一个 Visual Studio Package 项目。
- 使用
SetWindowsHookEx
by passingWH_MOUSE_LL
and handle desired mouse event, for exampleWM_XBUTTONDOWN
注册全局鼠标挂钩。加载解决方案时执行注册。
运行 需要 Visual Studio 命令使用
DTE.ExecuteCommand
传递合适的命令,例如Build.BuildSolution
:var dte = (EnvDTE.DTE)this.GetService(typeof(EnvDTE.DTE)); dte.ExecuteCommand("Build.BuildSolution");
- 不要忘记在解决方案关闭时使用
UnhookWindowsHookEx
取消挂钩。
注:
要找到您需要的命令,请转至
Tools → Options → Environment → KeyBoard
并找到您需要的命令。您会找到很多关于如何注册全局鼠标挂钩的资源,例如 this,我稍作改动并用于测试。在 post 的末尾,您可以找到完整的源代码。
在 Visual Studio 2013 中已弃用插件,因此虽然您可以使用 Visual Studio Add-in 项目执行相同操作,但最好使用 VSPackages 执行此操作。
实施
首先创建一个Visual Studio Package 项目并将package 的代码更改为我在这里post 的代码。还要将我用于全局鼠标挂钩的 类 和 windows API 添加到解决方案中。
套餐
这是我的包的完整代码:
using Microsoft.VisualStudio.Shell;
using System;
using System.Runtime.InteropServices;
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(GuidList.guidVSPackage1PkgString)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids80.SolutionExists)]
public sealed class VSPackage1Package : Package
{
public VSPackage1Package() { }
EnvDTE.DTE dte;
protected override void Initialize()
{
base.Initialize();
dte = (EnvDTE.DTE)this.GetService(typeof(EnvDTE.DTE));
dte.Events.SolutionEvents.Opened += SolutionEvents_Opened;
dte.Events.SolutionEvents.AfterClosing += SolutionEvents_AfterClosing;
}
void SolutionEvents_AfterClosing() { MouseHook.Stop(); }
void SolutionEvents_Opened()
{
MouseHook.Start();
MouseHook.MouseAction += MouseHook_MouseAction;
}
void MouseHook_MouseAction(object sender, EventArgs e)
{
dte.ExecuteCommand("Build.BuildSolution");
}
}
Windows API 消息、结构和方法
using System;
using System.Runtime.InteropServices;
public class Win32
{
public delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
public const int WH_MOUSE_LL = 14;
public enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201, WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200, WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204, WM_RBUTTONUP = 0x0205
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT { public int x; public int y; }
[StructLayout(LayoutKind.Sequential)]
public struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn,
IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam,
IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
}
全局鼠标挂钩
由于我的鼠标中没有 XButton,因此我处理了 WM_RBUTTONDOWN
事件。
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public static class MouseHook
{
public static event EventHandler MouseAction = delegate { };
private static Win32.LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
public static void Start() { _hookID = SetHook(_proc); }
public static void Stop() { Win32.UnhookWindowsHookEx(_hookID); }
private static IntPtr SetHook(Win32.LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
var handle = Win32.GetModuleHandle(curModule.ModuleName);
return Win32.SetWindowsHookEx(Win32.WH_MOUSE_LL, proc, handle, 0);
}
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 &&
Win32.MouseMessages.WM_RBUTTONDOWN == (Win32.MouseMessages)wParam)
{
Win32.MSLLHOOKSTRUCT hookStruct =
(Win32.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,
typeof(Win32.MSLLHOOKSTRUCT));
MouseAction(null, new EventArgs());
}
return Win32.CallNextHookEx(_hookID, nCode, wParam, lParam);
}
}