利用鼠标钩子将鼠标中键转为左键

   利用鼠标钩子将鼠标中键转为左键

 鼠标的左键由于使用的多,容易损坏,我用的一个微软鼠标左键就出现了问题,点击左键没有响应了.把鼠标拆开后,也没发现什么物理性的损伤.按键还是那么 清脆,但就是没反应.网上有说可以将中键取下来换到左键上,但是,需要用电焊,按键底下的接口是与线路板焊在一起的,这可没办法了,总不能再去买个焊枪.
    硬的不行,就只有来软的了.通过截获鼠标按键消息,将中键的消息转换为左键消息发出去,实现中键转左键的功能.要实现这个功能,只能通过挂载鼠标构子函 数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卸载钩子.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值