利用鼠标钩子将鼠标中键转为左键
鼠标的左键由于使用的多,容易损坏,我用的一个微软鼠标左键就出现了问题,点击左键没有响应了.把鼠标拆开后,也没发现什么物理性的损伤.按键还是那么 清脆,但就是没反应.网上有说可以将中键取下来换到左键上,但是,需要用电焊,按键底下的接口是与线路板焊在一起的,这可没办法了,总不能再去买个焊枪.
硬的不行,就只有来软的了.通过截获鼠标按键消息,将中键的消息转换为左键消息发出去,实现中键转左键的功能.要实现这个功能,只能通过挂载鼠标构子函 数SetWindowsHookEx,在处理完中键转换任务后,一定要记着调用CallNextHookEx函数,将鼠标消息传下去,不然,鼠标可就动不 了了. 下面是实现的代码.
MouseHookWin32.cs代码文件:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace QingMouseHook
{
public class MouseHookWin32
{
// wParam对应码
public const int WM_LBUTTONDOWN = 0x201;
public const int WM_MBUTTONDOWN = 0x207;
public const int WM_LBUTTONUP = 0x202;
public const int WM_MBUTTONUP = 0x208;
// mouse_event消息码
public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
public const int MOUSEEVENTF_LEFTUP = 0x0004;
public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
public const int MOUSEEVENTF_MIDDLEUP = 0x0040;
public const int WH_MOUSE_LL = 14; // 表示鼠标钩子
public delegate int MouseHookHandler(int nCode, Int32 wParam, IntPtr lParam);
// 设置钩子函数
[DllImport("user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, MouseHookHandler lpfn, IntPtr hInstance, int threadId);
// 卸载钩子函数
[DllImport("user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
// 调用下一个钩子函数
[DllImport("user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
// 获取最后的错误
[DllImport("kernel32", EntryPoint = "GetLastError")]
public static extern int GetLastError();
[DllImport("user32")]
public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
}
}
MouseHook.cs代码文件,具体实现转换功能:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
namespace QingMouseHook
{
public class MouseHook
{
private static int hwndMouseHook = 0; // 加载鼠标钩子后的句柄
/// <summary>
/// 装载鼠标钩子
/// </summary>
public void SetHook()
{
if (hwndMouseHook == 0)
{
MouseHookWin32.MouseHookHandler handler = new MouseHookWin32.MouseHookHandler(this.HookHandle);
hwndMouseHook = MouseHookWin32.SetWindowsHookEx(MouseHookWin32.WH_MOUSE_LL,
handler, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
if (hwndMouseHook == 0)
{
this.UnHook();
int errno = MouseHookWin32.GetLastError();
throw new Exception(errno.ToString());
}
}
}
/// <summary>
/// 卸载鼠标钩子
/// </summary>
public void UnHook()
{
if (hwndMouseHook == 0) return;
bool isSuc = MouseHookWin32.UnhookWindowsHookEx(hwndMouseHook);
if (!isSuc) throw new Exception("卸载鼠标钩子失败");
}
/// <summary>
/// 处理钩子中的鼠标消息,发送鼠标模拟按键
/// </summary>
private int HookHandle(int nCode, int wParam, IntPtr lParam)
{
switch (wParam)
{
case MouseHookWin32.WM_MBUTTONDOWN :
MouseHookWin32.mouse_event(MouseHookWin32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
break;
case MouseHookWin32.WM_MBUTTONUP :
MouseHookWin32.mouse_event(MouseHookWin32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
break;
}
// 调用下一个钩子
return MouseHookWin32.CallNextHookEx(hwndMouseHook, nCode, wParam, lParam);
}
}
}
在调用程序的代码中,只要调用MouseHook的SetHook即可装载鼠标钩子,UnHook卸载钩子.