-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hook.cs
160 lines (142 loc) · 5.09 KB
/
Hook.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
namespace HookLock
{
public class Hook
{
#region 私有变量
/// <summary>
/// 键盘钩子句柄
/// </summary>
private IntPtr m_pKeyboardHook = IntPtr.Zero;
/// <summary>
/// 钩子委托声明
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
/// <summary>
/// 键盘钩子委托实例
/// </summary>
/// <remarks>
/// 不要试图省略此变量,否则将会导致
/// 激活 CallbackOnCollectedDelegate 托管调试助手 (MDA)。
/// 详细请参见MSDN中关于 CallbackOnCollectedDelegate 的描述
/// </remarks>
private HookProc m_KeyboardHookProcedure;
// 底层键盘钩子
public const int idHook = 13;
/// <summary>
/// 安装钩子
/// </summary>
/// <param name="idHook"></param>
/// <param name="lpfn"></param>
/// <param name="hInstance"></param>
/// <param name="threadId"></param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn,
IntPtr pInstance, int threadId);
/// <summary>
/// 卸载钩子
/// </summary>
/// <param name="idHook"></param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(IntPtr pHookHandle);
/// <summary>
/// 传递钩子
/// </summary>
/// <param name="pHookHandle">是您自己的钩子函数的句柄。用该句柄可以遍历钩子链</param>
/// <param name="nCode">把传入的参数简单传给CallNextHookEx即可</param>
/// <param name="wParam">把传入的参数简单传给CallNextHookEx即可</param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(IntPtr pHookHandle, int nCode,
Int32 wParam, IntPtr lParam);
#endregion 私有变量
#region 私有方法
/// <summary>
/// 键盘钩子处理函数
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
/// <remarks>此版本的键盘事件处理不是很好,还有待修正.</remarks>
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
//return 1;
KeyMSG m = (KeyMSG)Marshal.PtrToStructure(lParam, typeof(KeyMSG));
if (m_pKeyboardHook != IntPtr.Zero)
{
switch (((Keys)m.vkCode))
{
case Keys.LWin:
case Keys.RWin:
case Keys.Delete:
case Keys.Alt:
case Keys.Escape:
case Keys.F4:
case Keys.Control:
case Keys.Tab:
return 1;
}
}
return 0;
}
#endregion 私有方法
#region 公共方法
/// <summary>
/// 安装钩子
/// </summary>
/// <returns></returns>
public bool InstallHook()
{
//IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule);
IntPtr pInstance = (IntPtr)4194304;
if (this.m_pKeyboardHook == IntPtr.Zero)
{
this.m_KeyboardHookProcedure = new HookProc(KeyboardHookProc);
this.m_pKeyboardHook = SetWindowsHookEx(idHook, m_KeyboardHookProcedure, pInstance, 0);
if (this.m_pKeyboardHook == IntPtr.Zero)
{
this.UnInstallHook();
return false;
}
}
return true;
}
/// <summary>
/// 卸载钩子
/// </summary>
/// <returns></returns>
public bool UnInstallHook()
{
bool result = true;
if (this.m_pKeyboardHook != IntPtr.Zero)
{
result = (UnhookWindowsHookEx(this.m_pKeyboardHook) && result);
this.m_pKeyboardHook = IntPtr.Zero;
}
return result;
}
[StructLayout(LayoutKind.Sequential)]
public struct KeyMSG
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
#endregion 公共方法
}
}