C# 实现WindowsHook 类
1
using System;
2
using System.Runtime.InteropServices;
3
using System.Reflection;
4
using System.Threading;
5
6
using System.Windows.Forms;
7
8
9
namespace GlobalHookDemo {
10
11
/// <summary>
12
/// This class allows you to tap keyboard and mouse and / or to detect their activity even when an
13
/// application runes in background or does not have any user interface at all. This class raises
14
/// common .NET events with KeyEventArgs and MouseEventArgs so you can easily retrive any information you need.
15
/// </summary>
16
/// <remarks>
17
/// created by - Georgi
18
/// created on - 22.05.2004 13:08:01
19
/// </remarks>
20
public class UserActivityHook : object {
21
22
/// <summary>
23
/// Default constructor - starts hooks automatically
24
/// </summary>
25
public UserActivityHook() {
26
Start();
27
}
28
29
~UserActivityHook() {
30
Stop();
31
}
32
33
public event MouseEventHandler OnMouseActivity;
34
public event KeyEventHandler KeyDown;
35
public event KeyPressEventHandler KeyPress;
36
public event KeyEventHandler KeyUp;
37
38
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
39
40
static int hMouseHook = 0; //Declare mouse hook handle as int.
41
static int hKeyboardHook = 0; //Declare keyboard hook handle as int.
42
43
//values from Winuser.h in Microsoft SDK.
44
public const int WH_MOUSE_LL = 14; //mouse hook constant
45
public const int WH_KEYBOARD_LL = 13; //keyboard hook constant
46
47
HookProc MouseHookProcedure; //Declare MouseHookProcedure as HookProc type.
48
HookProc KeyboardHookProcedure; //Declare KeyboardHookProcedure as HookProc type.
49
50
51
//Declare wrapper managed POINT class.
52
[StructLayout(LayoutKind.Sequential)]
53
public class POINT
54
{
55
public int x;
56
public int y;
57
}
58
59
//Declare wrapper managed MouseHookStruct class.
60
[StructLayout(LayoutKind.Sequential)]
61
public class MouseHookStruct
62
{
63
public POINT pt;
64
public int hwnd;
65
public int wHitTestCode;
66
public int dwExtraInfo;
67
}
68
69
//Declare wrapper managed KeyboardHookStruct class.
70
[StructLayout(LayoutKind.Sequential)]
71
public class KeyboardHookStruct
72
{
73
public int vkCode; //Specifies a virtual-key code. The code must be a value in the range 1 to 254.
74
public int scanCode; // Specifies a hardware scan code for the key.
75
public int flags; // Specifies the extended-key flag, event-injected flag, context code, and transition-state flag.
76
public int time; // Specifies the time stamp for this message.
77
public int dwExtraInfo; // Specifies extra information associated with the message.
78
}
79
80
81
//Import for SetWindowsHookEx function.
82
//Use this function to install a hook.
83
[DllImport("user32.dll",CharSet=CharSet.Auto,
84
CallingConvention=CallingConvention.StdCall)]
85
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
86
IntPtr hInstance, int threadId);
87
88
//Import for UnhookWindowsHookEx.
89
//Call this function to uninstall the hook.
90
[DllImport("user32.dll",CharSet=CharSet.Auto,
91
CallingConvention=CallingConvention.StdCall)]
92
public static extern bool UnhookWindowsHookEx(int idHook);
93
94
//Import for CallNextHookEx.
95
//Use this function to pass the hook information to next hook procedure in chain.
96
[DllImport("user32.dll",CharSet=CharSet.Auto,
97
CallingConvention=CallingConvention.StdCall)]
98
public static extern int CallNextHookEx(int idHook, int nCode,
99
Int32 wParam, IntPtr lParam);
100
101
public void Start()
102
{
103
// install Mouse hook
104
if(hMouseHook == 0)
105
{
106
// Create an instance of HookProc.
107
MouseHookProcedure = new HookProc(MouseHookProc);
108
109
hMouseHook = SetWindowsHookEx( WH_MOUSE_LL,
110
MouseHookProcedure,
111
Marshal.GetHINSTANCE(
112
Assembly.GetExecutingAssembly().GetModules()[0]),
113
0);
114
115
//If SetWindowsHookEx fails.
116
if(hMouseHook == 0 ) {
117
Stop();
118
throw new Exception("SetWindowsHookEx failed.");
119
}
120
}
121
122
// install Keyboard hook
123
if(hKeyboardHook == 0)
124
{
125
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
126
hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL,
127
KeyboardHookProcedure,
128
Marshal.GetHINSTANCE(
129
Assembly.GetExecutingAssembly().GetModules()[0]),
130
0);
131
132
//If SetWindowsHookEx fails.
133
if(hKeyboardHook == 0 ) {
134
Stop();
135
throw new Exception("SetWindowsHookEx ist failed.");
136
}
137
}
138
}
139
140
public void Stop()
141
{
142
bool retMouse =true;
143
bool retKeyboard = true;
144
if(hMouseHook != 0)
145
{
146
retMouse = UnhookWindowsHookEx(hMouseHook);
147
hMouseHook = 0;
148
}
149
150
if(hKeyboardHook != 0)
151
{
152
retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
153
hKeyboardHook = 0;
154
}
155
156
//If UnhookWindowsHookEx fails.
157
if (!(retMouse && retKeyboard)) throw new Exception("UnhookWindowsHookEx failed.");
158
}
159
160
161
162
private const int WM_MOUSEMOVE = 0x200;
163
private const int WM_LBUTTONDOWN = 0x201;
164
private const int WM_RBUTTONDOWN = 0x204;
165
private const int WM_MBUTTONDOWN = 0x207;
166
private const int WM_LBUTTONUP = 0x202;
167
private const int WM_RBUTTONUP = 0x205;
168
private const int WM_MBUTTONUP = 0x208;
169
private const int WM_LBUTTONDBLCLK = 0x203;
170
private const int WM_RBUTTONDBLCLK = 0x206;
171
private const int WM_MBUTTONDBLCLK = 0x209;
172
173
private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
174
{
175
// if ok and someone listens to our events
176
if ((nCode >= 0) && (OnMouseActivity!=null)) {
177
178
MouseButtons button=MouseButtons.None;
179
switch (wParam)
180
{
181
case WM_LBUTTONDOWN:
182
//case WM_LBUTTONUP:
183
//case WM_LBUTTONDBLCLK:
184
button=MouseButtons.Left;
185
break;
186
case WM_RBUTTONDOWN:
187
//case WM_RBUTTONUP:
188
//case WM_RBUTTONDBLCLK:
189
button=MouseButtons.Right;
190
break;
191
case WM_MBUTTONDOWN:
192
button=MouseButtons.Middle;
193
break;
194
}
195
int clickCount=0;
196
if (button!=MouseButtons.None)
197
if (wParam==WM_LBUTTONDBLCLK || wParam==WM_RBUTTONDBLCLK) clickCount=2;
198
else clickCount=1;
199
200
//Marshall the data from callback.
201
MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
202
MouseEventArgs e=new MouseEventArgs(
203
button,
204
clickCount,
205
MyMouseHookStruct.pt.x,
206
MyMouseHookStruct.pt.y,
207
0 );
208
OnMouseActivity(this, e);
209
}
210
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
211
}
212
213
214
//The ToAscii function translates the specified virtual-key code and keyboard state to the corresponding character or characters. The function translates the code using the input language and physical keyboard layout identified by the keyboard layout handle.
215
[DllImport("user32")]
216
public static extern int ToAscii(int uVirtKey, //[in] Specifies the virtual-key code to be translated.
217
int uScanCode, // [in] Specifies the hardware scan code of the key to be translated. The high-order bit of this value is set if the key is up (not pressed).
218
byte[] lpbKeyState, // [in] Pointer to a 256-byte array that contains the current keyboard state. Each element (byte) in the array contains the state of one key. If the high-order bit of a byte is set, the key is down (pressed). The low bit, if set, indicates that the key is toggled on. In this function, only the toggle bit of the CAPS LOCK key is relevant. The toggle state of the NUM LOCK and SCROLL LOCK keys is ignored.
219
byte[] lpwTransKey, // [out] Pointer to the buffer that receives the translated character or characters.
220
int fuState); // [in] Specifies whether a menu is active. This parameter must be 1 if a menu is active, or 0 otherwise.
221
222
//The GetKeyboardState function copies the status of the 256 virtual keys to the specified buffer.
223
[DllImport("user32")]
224
public static extern int GetKeyboardState(byte[] pbKeyState);
225
226
private const int WM_KEYDOWN = 0x100;
227
private const int WM_KEYUP = 0x101;
228
private const int WM_SYSKEYDOWN = 0x104;
229
private const int WM_SYSKEYUP = 0x105;
230
231
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
232
{
233
// it was ok and someone listens to events
234
if ((nCode >= 0) && (KeyDown!=null || KeyUp!=null || KeyPress!=null))
235
{
236
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct) Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
237
// raise KeyDown
238
if ( KeyDown!=null && ( wParam ==WM_KEYDOWN || wParam==WM_SYSKEYDOWN ))
239
{
240
Keys keyData=(Keys)MyKeyboardHookStruct.vkCode;
241
KeyEventArgs e = new KeyEventArgs(keyData);
242
KeyDown(this, e);
243
}
244
245
// raise KeyPress
246
if ( KeyPress!=null && wParam ==WM_KEYDOWN )
247
{
248
byte[] keyState = new byte[256];
249
GetKeyboardState(keyState);
250
251
byte[] inBuffer= new byte[2];
252
if (ToAscii(MyKeyboardHookStruct.vkCode,
253
MyKeyboardHookStruct.scanCode,
254
keyState,
255
inBuffer,
256
MyKeyboardHookStruct.flags)==1)
257
{
258
KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);
259
KeyPress(this, e);
260
}
261
}
262
263
// raise KeyUp
264
if ( KeyUp!=null && ( wParam ==WM_KEYUP || wParam==WM_SYSKEYUP ))
265
{
266
Keys keyData=(Keys)MyKeyboardHookStruct.vkCode;
267
KeyEventArgs e = new KeyEventArgs(keyData);
268
KeyUp(this, e);
269
}
270
271
}
272
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
273
}
274
}
275
}
276
using System;2
using System.Runtime.InteropServices;3
using System.Reflection;4
using System.Threading;5

6
using System.Windows.Forms;7

8

9
namespace GlobalHookDemo {10
11
/// <summary>12
/// This class allows you to tap keyboard and mouse and / or to detect their activity even when an 13
/// application runes in background or does not have any user interface at all. This class raises 14
/// common .NET events with KeyEventArgs and MouseEventArgs so you can easily retrive any information you need.15
/// </summary>16
/// <remarks>17
/// created by - Georgi18
/// created on - 22.05.2004 13:08:0119
/// </remarks>20
public class UserActivityHook : object {21
22
/// <summary>23
/// Default constructor - starts hooks automatically24
/// </summary>25
public UserActivityHook() {26
Start();27
}28
29
~UserActivityHook() { 30
Stop();31
} 32

33
public event MouseEventHandler OnMouseActivity;34
public event KeyEventHandler KeyDown;35
public event KeyPressEventHandler KeyPress;36
public event KeyEventHandler KeyUp;37

38
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);39

40
static int hMouseHook = 0; //Declare mouse hook handle as int.41
static int hKeyboardHook = 0; //Declare keyboard hook handle as int.42

43
//values from Winuser.h in Microsoft SDK.44
public const int WH_MOUSE_LL = 14; //mouse hook constant45
public const int WH_KEYBOARD_LL = 13; //keyboard hook constant 46

47
HookProc MouseHookProcedure; //Declare MouseHookProcedure as HookProc type.48
HookProc KeyboardHookProcedure; //Declare KeyboardHookProcedure as HookProc type.49
50

51
//Declare wrapper managed POINT class.52
[StructLayout(LayoutKind.Sequential)]53
public class POINT 54
{55
public int x;56
public int y;57
}58

59
//Declare wrapper managed MouseHookStruct class.60
[StructLayout(LayoutKind.Sequential)]61
public class MouseHookStruct 62
{63
public POINT pt;64
public int hwnd;65
public int wHitTestCode;66
public int dwExtraInfo;67
}68

69
//Declare wrapper managed KeyboardHookStruct class.70
[StructLayout(LayoutKind.Sequential)]71
public class KeyboardHookStruct72
{73
public int vkCode; //Specifies a virtual-key code. The code must be a value in the range 1 to 254. 74
public int scanCode; // Specifies a hardware scan code for the key. 75
public int flags; // Specifies the extended-key flag, event-injected flag, context code, and transition-state flag.76
public int time; // Specifies the time stamp for this message.77
public int dwExtraInfo; // Specifies extra information associated with the message. 78
}79

80

81
//Import for SetWindowsHookEx function.82
//Use this function to install a hook.83
[DllImport("user32.dll",CharSet=CharSet.Auto,84
CallingConvention=CallingConvention.StdCall)]85
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, 86
IntPtr hInstance, int threadId);87

88
//Import for UnhookWindowsHookEx.89
//Call this function to uninstall the hook.90
[DllImport("user32.dll",CharSet=CharSet.Auto,91
CallingConvention=CallingConvention.StdCall)]92
public static extern bool UnhookWindowsHookEx(int idHook);93
94
//Import for CallNextHookEx.95
//Use this function to pass the hook information to next hook procedure in chain.96
[DllImport("user32.dll",CharSet=CharSet.Auto,97
CallingConvention=CallingConvention.StdCall)]98
public static extern int CallNextHookEx(int idHook, int nCode, 99
Int32 wParam, IntPtr lParam); 100

101
public void Start()102
{103
// install Mouse hook 104
if(hMouseHook == 0)105
{106
// Create an instance of HookProc.107
MouseHookProcedure = new HookProc(MouseHookProc);108

109
hMouseHook = SetWindowsHookEx( WH_MOUSE_LL,110
MouseHookProcedure, 111
Marshal.GetHINSTANCE(112
Assembly.GetExecutingAssembly().GetModules()[0]),113
0);114

115
//If SetWindowsHookEx fails.116
if(hMouseHook == 0 ) {117
Stop();118
throw new Exception("SetWindowsHookEx failed.");119
}120
}121
122
// install Keyboard hook 123
if(hKeyboardHook == 0)124
{125
KeyboardHookProcedure = new HookProc(KeyboardHookProc);126
hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL,127
KeyboardHookProcedure, 128
Marshal.GetHINSTANCE(129
Assembly.GetExecutingAssembly().GetModules()[0]),130
0);131

132
//If SetWindowsHookEx fails.133
if(hKeyboardHook == 0 ) {134
Stop();135
throw new Exception("SetWindowsHookEx ist failed.");136
}137
}138
}139

140
public void Stop()141
{142
bool retMouse =true;143
bool retKeyboard = true;144
if(hMouseHook != 0)145
{146
retMouse = UnhookWindowsHookEx(hMouseHook);147
hMouseHook = 0;148
} 149
150
if(hKeyboardHook != 0)151
{152
retKeyboard = UnhookWindowsHookEx(hKeyboardHook);153
hKeyboardHook = 0;154
} 155
156
//If UnhookWindowsHookEx fails.157
if (!(retMouse && retKeyboard)) throw new Exception("UnhookWindowsHookEx failed.");158
}159

160

161

162
private const int WM_MOUSEMOVE = 0x200;163
private const int WM_LBUTTONDOWN = 0x201;164
private const int WM_RBUTTONDOWN = 0x204;165
private const int WM_MBUTTONDOWN = 0x207;166
private const int WM_LBUTTONUP = 0x202;167
private const int WM_RBUTTONUP = 0x205;168
private const int WM_MBUTTONUP = 0x208;169
private const int WM_LBUTTONDBLCLK = 0x203;170
private const int WM_RBUTTONDBLCLK = 0x206;171
private const int WM_MBUTTONDBLCLK = 0x209;172

173
private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)174
{175
// if ok and someone listens to our events176
if ((nCode >= 0) && (OnMouseActivity!=null)) {177
178
MouseButtons button=MouseButtons.None;179
switch (wParam)180
{181
case WM_LBUTTONDOWN: 182
//case WM_LBUTTONUP: 183
//case WM_LBUTTONDBLCLK: 184
button=MouseButtons.Left; 185
break;186
case WM_RBUTTONDOWN: 187
//case WM_RBUTTONUP: 188
//case WM_RBUTTONDBLCLK: 189
button=MouseButtons.Right; 190
break;191
case WM_MBUTTONDOWN:192
button=MouseButtons.Middle;193
break;194
}195
int clickCount=0;196
if (button!=MouseButtons.None)197
if (wParam==WM_LBUTTONDBLCLK || wParam==WM_RBUTTONDBLCLK) clickCount=2;198
else clickCount=1;199
200
//Marshall the data from callback.201
MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));202
MouseEventArgs e=new MouseEventArgs(203
button, 204
clickCount, 205
MyMouseHookStruct.pt.x, 206
MyMouseHookStruct.pt.y, 207
0 );208
OnMouseActivity(this, e);209
}210
return CallNextHookEx(hMouseHook, nCode, wParam, lParam); 211
}212

213

214
//The ToAscii function translates the specified virtual-key code and keyboard state to the corresponding character or characters. The function translates the code using the input language and physical keyboard layout identified by the keyboard layout handle.215
[DllImport("user32")] 216
public static extern int ToAscii(int uVirtKey, //[in] Specifies the virtual-key code to be translated. 217
int uScanCode, // [in] Specifies the hardware scan code of the key to be translated. The high-order bit of this value is set if the key is up (not pressed). 218
byte[] lpbKeyState, // [in] Pointer to a 256-byte array that contains the current keyboard state. Each element (byte) in the array contains the state of one key. If the high-order bit of a byte is set, the key is down (pressed). The low bit, if set, indicates that the key is toggled on. In this function, only the toggle bit of the CAPS LOCK key is relevant. The toggle state of the NUM LOCK and SCROLL LOCK keys is ignored.219
byte[] lpwTransKey, // [out] Pointer to the buffer that receives the translated character or characters. 220
int fuState); // [in] Specifies whether a menu is active. This parameter must be 1 if a menu is active, or 0 otherwise. 221

222
//The GetKeyboardState function copies the status of the 256 virtual keys to the specified buffer. 223
[DllImport("user32")] 224
public static extern int GetKeyboardState(byte[] pbKeyState);225

226
private const int WM_KEYDOWN = 0x100;227
private const int WM_KEYUP = 0x101;228
private const int WM_SYSKEYDOWN = 0x104;229
private const int WM_SYSKEYUP = 0x105;230

231
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)232
{233
// it was ok and someone listens to events234
if ((nCode >= 0) && (KeyDown!=null || KeyUp!=null || KeyPress!=null))235
{236
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct) Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));237
// raise KeyDown238
if ( KeyDown!=null && ( wParam ==WM_KEYDOWN || wParam==WM_SYSKEYDOWN ))239
{240
Keys keyData=(Keys)MyKeyboardHookStruct.vkCode;241
KeyEventArgs e = new KeyEventArgs(keyData);242
KeyDown(this, e);243
}244
245
// raise KeyPress246
if ( KeyPress!=null && wParam ==WM_KEYDOWN )247
{248
byte[] keyState = new byte[256];249
GetKeyboardState(keyState);250

251
byte[] inBuffer= new byte[2];252
if (ToAscii(MyKeyboardHookStruct.vkCode,253
MyKeyboardHookStruct.scanCode,254
keyState,255
inBuffer,256
MyKeyboardHookStruct.flags)==1) 257
{258
KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);259
KeyPress(this, e);260
}261
}262
263
// raise KeyUp264
if ( KeyUp!=null && ( wParam ==WM_KEYUP || wParam==WM_SYSKEYUP ))265
{266
Keys keyData=(Keys)MyKeyboardHookStruct.vkCode;267
KeyEventArgs e = new KeyEventArgs(keyData);268
KeyUp(this, e);269
}270

271
}272
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); 273
}274
}275
}276



浙公网安备 33010602011771号