I'm currently working on a personal project where I'm trying to create an APM calculator like. In order to realize it I need to be able to detect any key pressed and any mouse click. I'm using hooks to catch keyboard events and mouse events but I can't find any way to capture special mouse button click...
When I'm talking about special mouse button, I mean additional buttons like on gaming mice. Example : Logitech G600.
I would like to know if there is any way to detect mouse click from any mouse button or special mouse button ?
I already found how to do it for conventional buttons and even for the navigation buttons, but I can't seem to find anything about additional buttons.
Here is my code where I removed all the keyboard part :
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);
}
}
}
Thanks.