作者: Blue_Pen
查看: 5865|回复: 5
打印 上一主题 下一主题

[教程] 【屏蔽系统组合键】Hook键盘钩子(屏蔽键盘按键)

[复制链接]
跳转到指定楼层
楼主
Blue_Pen 发表于 2014-9-2 12:17:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查看: 5865|回复: 5
描述:
软件有时候不允许 用户使用Alt+Tab键切换程序,但是Key_Down事件中只能捕获Alt+F4组合键,找了很多资料,整理了一下,最后分装在下面这个Hook类中,完美实现屏蔽系统组合键
使用说明
实例化Hook类:Hook hk=new Hook();
启动Hook钩子:hk.Hook_Start();

下面是Hook类的代码
[C#] 纯文本查看 复制代码
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace HookClass
{
/// <summary>
/// 键盘钩子(屏蔽键盘按键)
/// </summary>
public class Hook
{
//键盘Hook结构函数 
[StructLayout(LayoutKind.Sequential)]
public class KeyBoardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}

//委托 
public delegate int HookProc(int nCode, int wParam, IntPtr lParam);

private static int hHook = 0;
public const int WH_KEYBOARD_LL = 13;

//LowLevel键盘截获,如果是WH_KEYBOARD=2,并不能对系统键盘截取,Acrobat Reader会在你截取之前获得键盘。 
public HookProc KeyBoardHookProcedure;

#region [DllImport("user32.dll")]

//设置钩子 
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
//抽掉钩子 
public static extern bool UnhookWindowsHookEx(int idHook);

[DllImport("user32.dll")]
//调用下一个钩子 
public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

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

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

#endregion

#region 安装键盘钩子

/// <summary>
/// 安装键盘钩子
/// </summary>
public void Hook_Start()
{
if (hHook == 0)
{
KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure,
GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
//如果设置钩子失败. 
if (hHook == 0)
{
Hook_Clear();
}
}
}

#endregion

#region 取消钩子事件

/// <summary>
/// 取消钩子事件
/// </summary>
public void Hook_Clear()
{
bool retKeyboard = true;
if (hHook != 0)
{
retKeyboard = UnhookWindowsHookEx(hHook);
hHook = 0;
}
}

#endregion

#region 屏蔽键盘

/// <summary>
/// 屏蔽键盘
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode >= 0)
{
KeyBoardHookStruct kbh =
(KeyBoardHookStruct) Marshal.PtrToStructure(lParam, typeof (KeyBoardHookStruct));
// 屏蔽左"WIN"、右"Win"
if ((kbh.vkCode == (int) Keys.LWin) || (kbh.vkCode == (int) Keys.RWin))
{
return 1;
}
//屏蔽Ctrl+Esc
if (kbh.vkCode == (int) Keys.Escape && (int) Control.ModifierKeys == (int) Keys.Control)
{
return 1;
}
//屏蔽Alt+f4 
if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt)
{
return 1;
}
//屏蔽Alt+Esc
if (kbh.vkCode == (int) Keys.Escape && (int) Control.ModifierKeys == (int) Keys.Alt)
{
return 1;
}
//屏蔽Alt+Tab 
if (kbh.vkCode == (int) Keys.Tab && (int) Control.ModifierKeys == (int) Keys.Alt)
{
return 1;
}
//截获Ctrl+Shift+Esc 
if (kbh.vkCode == (int) Keys.Escape &&
(int) Control.ModifierKeys == (int) Keys.Control + (int) Keys.Shift)
{
return 1;
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}

#endregion
}
}



评分

参与人数 1金钱 +2 收起 理由
乔克斯 + 2 感谢LZ对论坛做出的贡献~

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 转播转播
回复 论坛版权

使用道具 举报

沙发
wtujoxk 发表于 2014-9-4 19:42:29 | 只看该作者
这要写可以屏蔽除calt+alt+del的任何按键,包括各种组合键
[C#] 纯文本查看 复制代码
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace LockScreen
{
    class MyHook
    {
        //消息函数的委托
        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
        static int hHook = 0;
        public const int WH_KEYBOARD_LL = 13;//底层键盘钩子
        HookProc KeyBoardHookProcedure;

        //按键信息结构
        [StructLayout(LayoutKind.Sequential)]
        public class KeyBoardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }

        //安装钩子
        [DllImport("user32.dll")]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

        //卸载钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        //下一个钩挂的函数
        [DllImport("user32.dll")]
        public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        //返回当前线程 ID
        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();

        //得到模块的句柄
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);

        //安装钩子
        public void InsertHook()
        {
            if (hHook == 0)
            {
                KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
                hHook = SetWindowsHookEx(WH_KEYBOARD_LL,
                          KeyBoardHookProcedure,
                        GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
                if (hHook == 0)
                {
                    UnHook();
                    throw new Exception("设置Hook失败!");
                }
            }
        }

        //卸载钩子
        public void UnHook()
        {
            bool retKeyboard = true;
            if (hHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hHook);
                hHook = 0;
            }
            if (!retKeyboard) throw new Exception("卸载Hook失败!");
        }

        //按键消息的处理函数
        public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
                //添加自己的判断语句,如果符合要求的按键,就 return 1; 
                //没有判断直接 return 1;那么就屏蔽所有按键除了ctrl+alt+del
                
                return 1;
            }
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
    }
}
板凳
wtujoxk 发表于 2014-9-4 19:45:26 | 只看该作者
地板
liwei034 发表于 2015-9-28 08:20:54 | 只看该作者
学习了
回复

使用道具 举报

5#
caizm 发表于 2015-9-29 09:20:16 | 只看该作者
学习了              
6#
amborse 发表于 2019-9-12 11:48:35 | 只看该作者
66666666666666666666666
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-11-16 08:43, Processed in 0.547452 second(s), 31 queries , Gzip On.

快速回复 返回顶部 返回列表