C#的全局钩子

看了许都的关于如何使用C#做全局钩子的博文,都有错误!今天,用一个时间专门来实现以下这个功能!

 这个demo指示演示如何屏蔽掉CapsLock这个大小写切换键!细心的读者看看丢能实现对指定按键或所有案件的屏蔽了!

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;//<------
using System.Diagnostics;
using System.Reflection;
namespace goableHook
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [StructLayout(LayoutKind.Sequential)]
        public class KeyBoardHookStruct //托管的结构体 用来记录Wparam传来的值
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        private delegate IntPtr CALLBACKHookProcHandle(int nCodeD, IntPtr wParamD, IntPtr lParam);

        IntPtr hookID;//安装钩子后的这个钩子的句柄

        [DllImport("kernel32.dll")]
        private static extern IntPtr GetModuleHandle(string name);

        [DllImport("kernel32.dll")]
        static extern int GetCurrentThreadId();

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr SetWindowsHookEx(
            int hookId, //用于调用钩子的类型 这里是键盘钩子
            CALLBACKHookProcHandle hookCallBack, //用于调用钩子的回调函数
            IntPtr currentThreadId, //用于安装钩子的当前进程的编号
            int hookState //安装那种类型的钩子0 为全局钩子的代码号
            );

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr CallNextHookEx(
            IntPtr hookHandle, //钩子的句柄
            int ncode, //由钩子的回调函数传入的一个根据具体钩子类型的值
            IntPtr wParam, //这个消息的附加信息
            IntPtr lParam
            );

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool UnhookWindowsHookEx(IntPtr hookId);//用于卸载钩子的句柄

        private void StartHook()
        {
            const int WH_KEYBOARD = 13;//这是键盘钩子
            CALLBACKHookProcHandle callBackHandle = new CALLBACKHookProcHandle(this.CALLBACKHookProc);
            this.hookID = SetWindowsHookEx(WH_KEYBOARD, callBackHandle, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
        }


        private IntPtr CALLBACKHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)//有按键按下
            {
                KeyBoardHookStruct k = (KeyBoardHookStruct)Marshal. PtrToStructure(lParam, typeof(KeyBoardHookStruct));//将非托管的结构体转换为托管的结构体
                if (k.vkCode == (int)Keys.CapsLock)//设置你要屏蔽的按键(这里屏蔽的是大小写切换键)
                {
                    return (IntPtr)1;//表示这个消息已被处理,不会在发送到其他应用程序的消息队列中去
                }

            }

            return CallNextHookEx(hookID, nCode, wParam, lParam);//将这个消息 放到钩子链中的其他钩子中

        }
        private void StopHook()
        {
            UnhookWindowsHookEx(hookID);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.StartHook();
            MessageBox.Show(this.hookID.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.StopHook();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            //MessageBox.Show(count.ToString());

        }
    }
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页