我正在开发一个个人项目,我正在尝试创建一个APM计算器。为了实现它,我需要能够检测任何按下的键和任何鼠标点击。我正在使用钩子来捕捉键盘事件和鼠标事件,但我找不到任何方法来捕获特殊的鼠标按钮点击...
当我谈论特殊鼠标按钮时,我指的是游戏鼠标上的其他按钮。示例:Logitech G600。
我想知道是否有办法从任何鼠标按钮或特殊鼠标按钮检测鼠标?
我已经找到了如何为conventional buttons甚至the navigation buttons执行此操作,但我似乎无法找到有关其他按钮的内容。
这是我删除所有键盘部分的代码:
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);
}
}
}
感谢。
答案 0 :(得分:0)
如果我理解你是正确的,那么一旦你点击其他鼠标按钮,你就想找到正在发送的WindowsMessages
。为了检测这些,我建议您创建一个自定义MessageFilter
,它实现了接口IMessageFilter
。
class MessageFilter : IMessageFilter
{
int[] msgToIgnore = new int[] {512, 799, 49632, 49446, 1847, 15, 96, 275, 160, 1848, 674 , 513, 675, 514, 280, 161, 274, 673, 515, 516, 517, 518, 519, 520, 521, 522, 163, 164, 167, 168, 169, 165, 166};
public bool PreFilterMessage(ref Message m)
{
bool match = false;
foreach (var msg in msgToIgnore)
{
if (m.Msg == msg)
match = true;
}
if (!match)
{
MessageBox.Show(m.Msg.ToString());
return true;
}
else
return false;
}
}
MessageFilter
过滤每个发送到您的应用程序的WindowsMessage
。所以我基本上添加了msgToIgnore
数组来忽略所有其他基本WindowsMessages
,就像任何正常的鼠标点击,移动,......或应用程序启动时发生的任何事情一样。 (我用一个空的普通表单测试它并点击,双击,拖动,...在表单的每个部分上)这忽略了所有WindowsMessages
,但是一旦你发送了另一个WindowsMessage
,所以点击其他鼠标按钮,WindowsMessage
将显示在MessageBox
。
要激活此MessageFilter
,您只需将此项添加到您的应用中,我建议您在InitializeComponent()
之后在构造函数中执行此操作。
MessageFilter msgFilter = new MessageFilter();
Application.AddMessageFilter(msgFilter);