我正在进行一个个人项目,尝试创建类似APM计算器的东西。为了实现它,我需要能够检测到任何按键和鼠标点击。我正在使用钩子来捕获键盘事件和鼠标事件,但我找不到任何方法来捕获特殊鼠标按钮的点击...
当我谈论特殊鼠标按钮时,我的意思是像游戏鼠标上的附加按钮。例如:Logitech G600。
我想知道是否有任何方法可以检测来自任何鼠标按钮或特殊鼠标按钮的鼠标点击?
我已经找到了如何为常规按钮甚至导航按钮完成此操作的方法,但我似乎无法找到有关附加按钮的任何信息。
以下是我删除了所有键盘部分的代码:
谢谢。
当我谈论特殊鼠标按钮时,我的意思是像游戏鼠标上的附加按钮。例如:Logitech G600。
我想知道是否有任何方法可以检测来自任何鼠标按钮或特殊鼠标按钮的鼠标点击?
我已经找到了如何为常规按钮甚至导航按钮完成此操作的方法,但我似乎无法找到有关附加按钮的任何信息。
以下是我删除了所有键盘部分的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Input;
namespace MouseHook
{
static class Program
{
private const int WH_MOUSE_LL = 14;
private static LowLevelMouseProc _procMouse = HookCallbackMouse;
private static IntPtr _hookIDMouse = IntPtr.Zero;
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205,
WM_XBUTTONDOWN = 0x020B
// MISSING ADDITIONAL BUTTONS
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
_hookIDMouse = SetHookMouse(_procMouse);
Application.Run(new Form1());
UnhookWindowsHookEx(_hookIDMouse);
}
private static IntPtr SetHookMouse(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
private static IntPtr HookCallbackMouse(int nCode, IntPtr wParam, IntPtr lParam)
{
// DEAL WITH ANY BUTTON OR ADDITIONAL BUTTON MOUSE
return CallNextHookEx(_hookIDMouse, nCode, wParam, lParam);
}
}
}
谢谢。