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
|
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MediaBrowser.UI.UserInput
{
/// <summary>
/// Provides a basic low-level keyboard listener
/// Inspired by http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx
/// Use the KeyDown event to listen for keys.
/// Make sure to detach from the event when not needed.
/// </summary>
public static class KeyboardListener
{
#region KeyDown EventHandler
/// <summary>
/// The _ key down
/// </summary>
static volatile EventHandler<KeyEventArgs> _KeyDown;
/// <summary>
/// Fires whenever CurrentItem changes
/// </summary>
public static event EventHandler<KeyEventArgs> KeyDown
{
add
{
if (_KeyDown == null)
{
StartListening();
}
_KeyDown += value;
}
remove
{
_KeyDown -= value;
if (_KeyDown == null && _hookID != IntPtr.Zero)
{
StopListening();
}
}
}
/// <summary>
/// Raises the <see cref="E:KeyDown" /> event.
/// </summary>
/// <param name="e">The <see cref="KeyEventArgs" /> instance containing the event data.</param>
private static void OnKeyDown(KeyEventArgs e)
{
e.SuppressKeyPress = false;
if (_KeyDown != null)
{
// For now, don't async this
// This will give listeners a chance to modify SuppressKeyPress if they want
try
{
_KeyDown(null, e);
}
catch (Exception ex)
{
}
}
}
#endregion
/// <summary>
/// The W h_ KEYBOAR d_ LL
/// </summary>
private const int WH_KEYBOARD_LL = 13;
/// <summary>
/// The W m_ KEYDOWN
/// </summary>
private const int WM_KEYDOWN = 0x0100;
/// <summary>
/// The W m_ SYSKEYDOWN
/// </summary>
private const int WM_SYSKEYDOWN = 0x0104;
/// <summary>
/// The _hook ID
/// </summary>
private static IntPtr _hookID = IntPtr.Zero;
/// <summary>
/// The _proc
/// </summary>
private static LowLevelKeyboardProc _proc = HookCallback;
/// <summary>
/// Starts the listening.
/// </summary>
private static void StartListening()
{
_hookID = SetHook(_proc);
}
/// <summary>
/// Stops the listening.
/// </summary>
private static void StopListening()
{
UnhookWindowsHookEx(_hookID);
_hookID = IntPtr.Zero;
}
/// <summary>
/// Sets the hook.
/// </summary>
/// <param name="proc">The proc.</param>
/// <returns>IntPtr.</returns>
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (var curProcess = Process.GetCurrentProcess())
using (var curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
/// <summary>
/// Hooks the callback.
/// </summary>
/// <param name="nCode">The n code.</param>
/// <param name="wParam">The w param.</param>
/// <param name="lParam">The l param.</param>
/// <returns>IntPtr.</returns>
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
var suppressKeyPress = false;
if (nCode >= 0)
{
if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
{
var vkCode = Marshal.ReadInt32(lParam);
var keyData = (Keys)vkCode;
var e = new KeyEventArgs(keyData);
OnKeyDown(e);
suppressKeyPress = e.SuppressKeyPress;
}
}
if (suppressKeyPress)
{
return IntPtr.Zero;
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
/// <summary>
/// Delegate LowLevelKeyboardProc
/// </summary>
/// <param name="nCode">The n code.</param>
/// <param name="wParam">The w param.</param>
/// <param name="lParam">The l param.</param>
/// <returns>IntPtr.</returns>
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
#region Imports
/// <summary>
/// Sets the windows hook ex.
/// </summary>
/// <param name="idHook">The id hook.</param>
/// <param name="lpfn">The LPFN.</param>
/// <param name="hMod">The h mod.</param>
/// <param name="dwThreadId">The dw thread id.</param>
/// <returns>IntPtr.</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
/// <summary>
/// Unhooks the windows hook ex.
/// </summary>
/// <param name="hhk">The HHK.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
/// <summary>
/// Calls the next hook ex.
/// </summary>
/// <param name="hhk">The HHK.</param>
/// <param name="nCode">The n code.</param>
/// <param name="wParam">The w param.</param>
/// <param name="lParam">The l param.</param>
/// <returns>IntPtr.</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
/// <summary>
/// Gets the module handle.
/// </summary>
/// <param name="lpModuleName">Name of the lp module.</param>
/// <returns>IntPtr.</returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
#endregion
}
}
|