/
KeyBoardHook.cs
365 lines (319 loc) · 11.6 KB
/
KeyBoardHook.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/// KEYBOARD.CS
/// (c) 2006 by Emma Burrows
/// This file contains the following items:
/// - KeyboardHook: class to enable low-level keyboard hook using
/// the Windows API.
/// - KeyboardHookEventHandler: delegate to handle the KeyIntercepted
/// event raised by the KeyboardHook class.
/// - KeyboardHookEventArgs: EventArgs class to contain the information
/// returned by the KeyIntercepted event.
///
/// Change history:
/// 17/06/06: 1.0 - First version.
/// 18/06/06: 1.1 - Modified proc assignment in constructor to make class backward
/// compatible with 2003.
/// 10/07/06: 1.2 - Added support for modifier keys:
/// -Changed filter in HookCallback to WM_KEYUP instead of WM_KEYDOWN
/// -Imported GetKeyState from user32.dll
/// -Moved native DLL imports to a separate internal class as this
/// is a Good Idea according to Microsoft's guidelines
/// 13/02/07: 1.3 - Improved modifier key support:
/// -Added CheckModifiers() method
/// -Deleted LoWord/HiWord methods as they weren't necessary
/// -Implemented Barry Dorman's suggestion to AND GetKeyState
/// values with 0x8000 to get their result
/// 23/03/07: 1.4 - Fixed bug which made the Alt key appear stuck
/// - Changed the line
/// if (nCode >= 0 && (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP))
/// to
/// if (nCode >= 0)
/// {
/// if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
/// ...
/// Many thanks to "Scottie Numbnuts" for the solution.
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// Low-level keyboard intercept class to trap and suppress system keys.
/// </summary>
public class KeyboardHook : IDisposable
{
/// <summary>
/// Parameters accepted by the KeyboardHook constructor.
/// </summary>
public enum Parameters
{
None,
AllowAltTab,
AllowWindowsKey,
AllowAltTabAndWindows,
PassAllKeysToNextApp
}
//Internal parameters
private bool PassAllKeysToNextApp = false;
private bool AllowAltTab = false;
private bool AllowWindowsKey = false;
//Keyboard API constants
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYUP = 0x0101;
private const int WM_SYSKEYUP = 0x0105;
//Modifier key constants
private const int VK_SHIFT = 0x10;
private const int VK_CONTROL = 0x11;
private const int VK_MENU = 0x12;
private const int VK_CAPITAL = 0x14;
//Variables used in the call to SetWindowsHookEx
private HookHandlerDelegate proc;
private IntPtr hookID = IntPtr.Zero;
internal delegate IntPtr HookHandlerDelegate(
int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
/// <summary>
/// Event triggered when a keystroke is intercepted by the
/// low-level hook.
/// </summary>
public event KeyboardHookEventHandler KeyIntercepted;
// Structure returned by the hook whenever a key is pressed
internal struct KBDLLHOOKSTRUCT
{
public int vkCode;
int scanCode;
public int flags;
int time;
int dwExtraInfo;
}
#region Constructors
/// <summary>
/// Sets up a keyboard hook to trap all keystrokes without
/// passing any to other applications.
/// </summary>
public KeyboardHook()
{
proc = new HookHandlerDelegate(HookCallback);
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
hookID = NativeMethods.SetWindowsHookEx(WH_KEYBOARD_LL, proc,
NativeMethods.GetModuleHandle(curModule.ModuleName), 0);
}
}
/// <summary>
/// Sets up a keyboard hook with custom parameters.
/// </summary>
/// <param name="param">A valid name from the Parameter enum; otherwise, the
/// default parameter Parameter.None will be used.</param>
public KeyboardHook(string param)
: this()
{
if (!String.IsNullOrEmpty(param) && Enum.IsDefined(typeof(Parameters), param))
{
SetParameters((Parameters)Enum.Parse(typeof(Parameters), param));
}
}
/// <summary>
/// Sets up a keyboard hook with custom parameters.
/// </summary>
/// <param name="param">A value from the Parameters enum.</param>
public KeyboardHook(Parameters param)
: this()
{
SetParameters(param);
}
private void SetParameters(Parameters param)
{
switch (param)
{
case Parameters.None:
break;
case Parameters.AllowAltTab:
AllowAltTab = true;
break;
case Parameters.AllowWindowsKey:
AllowWindowsKey = true;
break;
case Parameters.AllowAltTabAndWindows:
AllowAltTab = true;
AllowWindowsKey = true;
break;
case Parameters.PassAllKeysToNextApp:
PassAllKeysToNextApp = true;
break;
}
}
#endregion
#region Check Modifier keys
/// <summary>
/// Checks whether Alt, Shift, Control or CapsLock
/// is enabled at the same time as another key.
/// Modify the relevant sections and return type
/// depending on what you want to do with modifier keys.
/// </summary>
private void CheckModifiers()
{
StringBuilder sb = new StringBuilder();
if ((NativeMethods.GetKeyState(VK_CAPITAL) & 0x0001) != 0)
{
//CAPSLOCK is ON
sb.AppendLine("Capslock is enabled.");
}
if ((NativeMethods.GetKeyState(VK_SHIFT) & 0x8000) != 0)
{
//SHIFT is pressed
sb.AppendLine("Shift is pressed.");
}
if ((NativeMethods.GetKeyState(VK_CONTROL) & 0x8000) != 0)
{
//CONTROL is pressed
sb.AppendLine("Control is pressed.");
}
if ((NativeMethods.GetKeyState(VK_MENU) & 0x8000) != 0)
{
//ALT is pressed
sb.AppendLine("Alt is pressed.");
}
Console.WriteLine(sb.ToString());
}
#endregion Check Modifier keys
#region Hook Callback Method
/// <summary>
/// Processes the key event captured by the hook.
/// </summary>
private IntPtr HookCallback(
int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
{
bool AllowKey = PassAllKeysToNextApp;
//Filter wParam for KeyUp events only
if (nCode >= 0)
{
if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
{
// Check for modifier keys, but only if the key being
// currently processed isn't a modifier key (in other
// words, CheckModifiers will only run if Ctrl, Shift,
// CapsLock or Alt are active at the same time as
// another key)
if (!(lParam.vkCode >= 160 && lParam.vkCode <= 164))
{
CheckModifiers();
}
// Check for key combinations that are allowed to
// get through to Windows
//
// Ctrl+Esc or Windows key
if (AllowWindowsKey)
{
switch (lParam.flags)
{
//Ctrl+Esc
case 0:
if (lParam.vkCode == 27)
AllowKey = true;
break;
//Windows keys
case 1:
if ((lParam.vkCode == 91) || (lParam.vkCode == 92))
AllowKey = true;
break;
}
}
// Alt+Tab
if (AllowAltTab)
{
if ((lParam.flags == 32) && (lParam.vkCode == 9))
AllowKey = true;
}
OnKeyIntercepted(new KeyboardHookEventArgs(lParam.vkCode, AllowKey));
}
//If this key is being suppressed, return a dummy value
if (AllowKey == false)
return (System.IntPtr)1;
}
//Pass key to next application
return NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam);
}
#endregion
#region Event Handling
/// <summary>
/// Raises the KeyIntercepted event.
/// </summary>
/// <param name="e">An instance of KeyboardHookEventArgs</param>
public void OnKeyIntercepted(KeyboardHookEventArgs e)
{
if (KeyIntercepted != null)
KeyIntercepted(e);
}
/// <summary>
/// Delegate for KeyboardHook event handling.
/// </summary>
/// <param name="e">An instance of InterceptKeysEventArgs.</param>
public delegate void KeyboardHookEventHandler(KeyboardHookEventArgs e);
/// <summary>
/// Event arguments for the KeyboardHook class's KeyIntercepted event.
/// </summary>
public class KeyboardHookEventArgs : System.EventArgs
{
private string keyName;
private int keyCode;
private bool passThrough;
/// <summary>
/// The name of the key that was pressed.
/// </summary>
public string KeyName
{
get { return keyName; }
}
/// <summary>
/// The virtual key code of the key that was pressed.
/// </summary>
public int KeyCode
{
get { return keyCode; }
}
/// <summary>
/// True if this key combination was passed to other applications,
/// false if it was trapped.
/// </summary>
public bool PassThrough
{
get { return passThrough; }
}
public KeyboardHookEventArgs(int evtKeyCode, bool evtPassThrough)
{
keyName = ((Keys)evtKeyCode).ToString();
keyCode = evtKeyCode;
passThrough = evtPassThrough;
}
}
#endregion
#region IDisposable Members
/// <summary>
/// Releases the keyboard hook.
/// </summary>
public void Dispose()
{
NativeMethods.UnhookWindowsHookEx(hookID);
}
#endregion
#region Native methods
[ComVisibleAttribute(false),
System.Security.SuppressUnmanagedCodeSecurity()]
internal class NativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook,
HookHandlerDelegate lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);
}
#endregion
}