c# 钩子类
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Linq;
5 using System.Runtime.InteropServices;
6 using System.Text;
7
8 namespace WindowsFormsApplication1
9 {
10 /// <summary>
11 /// 键盘Hook管理类
12
13 /// </summary>
14 public class KeyboardHookLib
15 {
16 private const int WH_KEYBOARD_LL = 13; //键盘
17
18 //键盘处理事件委托.
19 private delegate int HookHandle(int nCode, int wParam, IntPtr lParam);
20
21 //客户端键盘处理事件
22 public delegate void ProcessKeyHandle(HookStruct param, out bool handle);
23
24 //接收SetWindowsHookEx返回值
25 private static int _hHookValue = 0;
26
27 //勾子程序处理事件
28 private HookHandle _KeyBoardHookProcedure;
29
30 //Hook结构
31 [StructLayout(LayoutKind.Sequential)]
32 public class HookStruct
33 {
34 public int vkCode;
35 public int scanCode;
36 public int flags;
37 public int time;
38 public int dwExtraInfo;
39 }
40
41 //设置钩子
42 [DllImport("user32.dll")]
43 private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);
44
45 //取消钩子
46 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
47 private static extern bool UnhookWindowsHookEx(int idHook);
48
49 //调用下一个钩子
50 [DllImport("user32.dll")]
51 private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
52
53 //获取当前线程ID
54 [DllImport("kernel32.dll")]
55 private static extern int GetCurrentThreadId();
56
57 //Gets the main module for the associated process.
58 [DllImport("kernel32.dll")]
59 private static extern IntPtr GetModuleHandle(string name);
60
61 private IntPtr _hookWindowPtr = IntPtr.Zero;
62
63 //构造器
64 public KeyboardHookLib() { }
65
66 //外部调用的键盘处理事件
67 private static ProcessKeyHandle _clientMethod = null;
68
69 /// <summary>
70 /// 安装勾子
71 /// </summary>
72 /// <param name="hookProcess">外部调用的键盘处理事件</param>
73 public void InstallHook(ProcessKeyHandle clientMethod)
74 {
75 _clientMethod = clientMethod;
76
77 // 安装键盘钩子
78 if (_hHookValue == 0)
79 {
80 _KeyBoardHookProcedure = new HookHandle(GetHookProc);
81
82 _hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
83
84 _hHookValue = SetWindowsHookEx(
85 WH_KEYBOARD_LL,
86 _KeyBoardHookProcedure,
87 _hookWindowPtr,
88 0);
89
90 //如果设置钩子失败.
91 if (_hHookValue == 0)
92
93 UninstallHook();
94 }
95 }
96
97 //取消钩子事件
98 public void UninstallHook()
99 {
100 if (_hHookValue != 0)
101 {
102 bool ret = UnhookWindowsHookEx(_hHookValue);
103 if (ret) _hHookValue = 0;
104 }
105 }
106
107 //钩子事件内部调用,调用_clientMethod方法转发到客户端应用。
108 private static int GetHookProc(int nCode, int wParam, IntPtr lParam)
109 {
110 if (nCode >= 0)
111 {
112 //转换结构
113 HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct));
114
115 if (_clientMethod != null)
116 {
117 bool handle = false;
118 //调用客户提供的事件处理程序。
119 _clientMethod(hookStruct, out handle);
120 if (handle) return 1; //1:表示拦截键盘,return 退出
121 }
122 }
123 return CallNextHookEx(_hHookValue, nCode, wParam, lParam);
124 }
125
126 }
127 }
钩子类使用:
1 //钩子管理类
2 private KeyboardHookLib _keyBoardHook = null;
3
4 //安装钩子
5 private void button5_Click(object sender, EventArgs e)
6 {
7 _keyBoardHook = new KeyboardHookLib();
8 _keyBoardHook.InstallHook(this.KeyPress);
9
10 }
11
12 //取消钩子
13 private void button6_Click(object sender, EventArgs e)
14 {
15 if (_keyBoardHook != null) _keyBoardHook.UninstallHook();
16 }
17
18
19 public void KeyPress(KeyboardHookLib.HookStruct hookStruct, out bool handle)
20 {
21 handle = false; //预设不拦截任何键
22
23 if (hookStruct.vkCode == 91) // 截获左win(开始菜单键)
24 {
25 handle = true;
26 }
27
28 if (hookStruct.vkCode == 92)// 截获右win
29 {
30 handle = true;
31 }
32
33 //截获Ctrl+Esc
34 if (hookStruct.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control)
35 {
36 handle = true;
37 }
38
39 //截获alt+f4
40 if (hookStruct.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt)
41 {
42 handle = true;
43 }
44
45 //截获alt+tab
46 if (hookStruct.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt)
47 {
48 handle = true;
49 }
50
51 //截获F1
52 if (hookStruct.vkCode == (int)Keys.F1)
53 {
54 handle = true;
55 }
56
57 //截获Ctrl+Alt+Delete
58 if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete)
59 {
60 handle = true;
61 }
62
63 //如果键A~Z
64 if (hookStruct.vkCode >= (int)Keys.A && hookStruct.vkCode <= (int)Keys.Z)
65 {
66 //挡掉G键,想要挡掉哪些键就把下面的G换成那个要挡掉的键,同理可以挡多个
67 if (hookStruct.vkCode == (int)Keys.G)
68 hookStruct.vkCode = (int)Keys.None; //设键为0
69
70 handle = true;
71 }
72
73 Keys key = (Keys)hookStruct.vkCode;
74 label1.Text = "你按下:" + (key == Keys.None ? "None" : key.ToString());
75 }