#region 屏蔽Windows功能键(快捷键)
        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,并不能对系统键盘截取,会在你截取之前获得键盘。 
        private static HookProc KeyBoardHookProcedure;
        //键盘Hook结构函数 
        [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);
 
        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();
 [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);
 
         //如果函数执行成功,返回值不为0。
         //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
         [DllImport("user32.dll", SetLastError = true)]
         public static extern bool RegisterHotKey(
             IntPtr hWnd,                //要定义热键的窗口的句柄
             int id,                     //定义热键ID(不能与其它ID重复)           
             int fsModifiers,   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
             Keys vk                     //定义热键的内容
             );
 
         [DllImport("user32.dll", SetLastError = true)]
         public static extern bool UnregisterHotKey(
             IntPtr hWnd,                //要取消热键的窗口的句柄 
             int id                      //要取消热键的ID
             );
 
        public static 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();
                }
            }
        }
 
        //取消钩子事件 
        public static void Hook_Clear()
        {
            bool retKeyboard = true;
            if (hHook != 0)
            {
                retKeyboard = UnhookWindowsHookEx(hHook);
                hHook = 0;
            }
            //如果去掉钩子失败. 
            if (!retKeyboard) throw new Exception("Hook去除失败");
        }
 
//这里可以添加自己想要的信息处理 
        private static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
 
                if (kbh.vkCode == 91) // 截获左win(开始菜单键) 
                {
                    return 1;
                }
 
                if (kbh.vkCode == 92)// 截获右win(开始菜单键) 
                {
                    return 1;
                }
 
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control) //截获Ctrl+Esc 
                {
                    return 1;
                }
 
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Alt) //截获Alt+Esc 
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt) //截获alt+f4 
                {
                    return 1;
                }
  if (kbh.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt) //截获alt+tab
                {
                    return 1;
                }
 
                if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift) //截获Ctrl+Shift+Esc
                {
                    return 1;
                }
              
                if (kbh.vkCode == (int)Keys.Space && (int)Control.ModifierKeys == (int)Keys.Alt) //截获alt+空格 
                {
                    return 1;
                }
 
                if (kbh.vkCode == 241) //截获F1 
                {
                    return 1;
                }
 
                if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete)      //截获Ctrl+Alt+Delete 
                {
                    return 1;
                }
 
                if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift) //截获Ctrl+Shift 
                {
                    return 1;
                }
 
                if (kbh.vkCode == (int)Keys.Space && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt) //截获Ctrl+Alt+空格 
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.LWin)
                {
                    return 1;
                }
                if (kbh.vkCode == (int)Keys.RWin)
                {
 
                } return 1;
     }
 
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }
 
        /// <summary>
        /// 开打任务管理器快捷键为Windows底层按键
        /// </summary>
        /// <param name="bLock"></param>
        public static void TaskMgrLocking(bool bLock)
        {
            if (bLock)//屏蔽任务管理器、并且不出现windows提示信息“任务管理器已被管理员禁用”
            {
                Process p = new Process();
                p.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
                p.StartInfo.FileName = "taskmgr.exe";
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.Start(); 
            }
            else//设置任务管理器为可启动状态 
            {
                Process[] p = Process.GetProcesses();
 
                foreach (Process p1 in p)
                {
                    try
                    {
                        if (p1.ProcessName.ToLower().Trim() == "taskmgr")//这里判断是任务管理器    
                        {
                            p1.Kill();
                            RegistryKey r = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
                            r.SetValue("DisableTaskmgr", "0"); //设置任务管理器为可启动状态 
                            Registry.CurrentUser.DeleteSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
                              }
                    }
                    catch
                    {
                        return;
                    }
                }
                
            }
            
        }
        #endregion
 
        //调用
        private void button1_Click(object sender, EventArgs e)
        {
            //启动钩子,处理钩子事件
            Hook_Start();
            //屏蔽任务管理器
            TaskMgrLocking(true);
        }
        /// <summary>
        /// 关闭窗口时事件
        /// </summary>
        //private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        //{   //注销Id号为100的热键设定   
        //    UnregisterHotKey(Handle, 100);   //注销Id号为101的热键设定  
        //    UnregisterHotKey(Handle, 101);   //注销Id号为102的热键设定   
        //    UnregisterHotKey(Handle, 102);   //注销Id号为103的热键设定   
        //    UnregisterHotKey(Handle, 103);
        //    Hook_Clear();
        //    TaskMgrLocking(false);
      //  }

C# 通过Hook的方法 屏蔽快捷键的更多相关文章

  1. C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入

    原文:C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入 Win32.cs /* * * FileCreate By Bluefire * Used To Import WindowsApi ...

  2. {Django基础十之Form和ModelForm组件}一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 ModelForm

    Django基础十之Form和ModelForm组件 本节目录 一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 Model ...

  3. idea快捷键:查找类中所有方法的快捷键

    查找类中所有方法的快捷键 第一种:ctal+f12,如下图 第二种:alt+7,如下图

  4. Android进程so注入Hook java方法

    本文博客链接:http://blog.csdn.net/qq1084283172/article/details/53769331 Andorid的Hook方式比较多,现在来学习下,基于Android ...

  5. idea 查看 类所有方法的快捷键

    idea 查看 类 所有方法的快捷键 Idea:ctrl+F12 Eclipse:Ctrl+O

  6. 【Eclipse】_Eclipse自动补全增强方法 & 常用快捷键

    一,Eclipse自动补全增强方法 在Eclipse中,从Window -> preferences -> Java -> Editor -> Content assist - ...

  7. Eclipse自动生成方法注释 快捷键

    自动生成方法的注释格式,例如 /*** @param str* @return* @throws ParseException*/ 快捷键是 ALT + SHIFT + J,将光标放在方法名上,按快捷 ...

  8. c# 屏蔽快捷键

    前言 有时候开发会遇到这样一个需求,软件需要屏蔽用户的组合快捷键或某些按键,避免强制退出软件,防止勿操作等. 原理 1.要实现组合键,按键拦截,需要用到user32.dll中的SetWindowsHo ...

  9. 【转】微软教学:三种方法屏蔽Win7/Win8.1升级Win10推送

    原文地址:http://www.ithome.com/html/win10/199961.htm 微软在2015年6月就开启了Win10升级推送工作,主要是靠<获取Windows10>工具 ...

随机推荐

  1. Using a USB host controller security extension for controlling changes in and auditing USB topology

    Protecting computer systems from attacks that attempt to change USB topology and for ensuring that t ...

  2. 标准C程序设计七---70

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  3. 驱动12.移植dm9000驱动程序

    1 确定相异性 1.1 选中网卡芯片nGCS4 1.2 确定相异性:基地址,中断号,设置时序(内存控制器BWSCON,BANKCONn) 1.3 修改相应的部分 2 测试DM9000C驱动程序:2.1 ...

  4. inline关键词的使用(转载)

    (一)inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把min()指定为内联. inline int min(int first, int ...

  5. Ui大屏

    http://www.uimaker.com/plus/view.php?aid=128661&pageno=1

  6. AC日记——[SDOI2011]染色 洛谷 P2486

    题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例#1: 6 5 2 2 1 2 1 1 1 2 1 3 2 4 2 5 2 6 Q 3 5 C ...

  7. 多协议底层攻击工具Yesinia

    多协议底层攻击工具Yesinia   Yesinia是一款底层协议攻击工具.它提供多种运行模式,如终端文本模式.GTK图形模式.NCurses模式.守护进程模式.它利用各种底层协议的漏洞实施攻击,支持 ...

  8. window 驱动开发

    http://blog.csdn.net/chenyujing1234/article/category/1147469/5

  9. Xcode: This device is no longer connected error

    Quit the xcode and connect again will all right.

  10. Oracle SOA Suit Medicator and OSB

    Medicator和OSB (Oracle Service Bus)存在的目的,从架构的设计模式上看,和解耦多态等理念非常的相似. 通过Proxy代理的方式,把真正某个Service的实现进行隐藏,让 ...