1- using System ;
1+ using System ;
22using System . Reflection ;
33using System . Runtime . InteropServices ;
44using System . Windows . Forms ;
55using System . Collections . Generic ;
66
77namespace RT . Util
88{
9- /// <summary>
10- /// A class that manages a global low-level keyboard hook
11- /// </summary>
9+ /// <summary>Manages a global low-level keyboard hook.</summary>
1210 public sealed class GlobalKeyboardListener
1311 {
1412 /// <summary>
@@ -31,11 +29,11 @@ public sealed class GlobalKeyboardListener
3129 /// <summary>
3230 /// Occurs when one of the hooked keys is pressed.
3331 /// </summary>
34- public event KeyEventHandler KeyDown ;
32+ public event GlobalKeyEventHandler KeyDown ;
3533 /// <summary>
3634 /// Occurs when one of the hooked keys is released.
3735 /// </summary>
38- public event KeyEventHandler KeyUp ;
36+ public event GlobalKeyEventHandler KeyUp ;
3937 #endregion
4038
4139 #region Constructors and Destructors
@@ -78,13 +76,10 @@ private void unhook()
7876 WinAPI . UnhookWindowsHookEx ( _hHook ) ;
7977 }
8078
81- /// <summary>
82- /// The callback for the keyboard hook
83- /// </summary>
84- /// <param name="code">The hook code, if it isn't >= 0, the function shouldn't do anyting</param>
85- /// <param name="wParam">The event type</param>
86- /// <param name="lParam">The keyhook event information</param>
87- /// <returns></returns>
79+ /// <summary>The callback for the keyboard hook.</summary>
80+ /// <param name="code">The hook code. If this is < 0, the callback shouldn’t do anyting.</param>
81+ /// <param name="wParam">The event type. Only <c>WM_(SYS)?KEY(DOWN|UP)</c> events are handled.</param>
82+ /// <param name="lParam">Information about the key pressed/released.</param>
8883 private int hookProc ( int code , int wParam , ref WinAPI . KeyboardHookStruct lParam )
8984 {
9085 if ( code >= 0 )
@@ -93,7 +88,7 @@ private int hookProc(int code, int wParam, ref WinAPI.KeyboardHookStruct lParam)
9388
9489 if ( HookAllKeys || _hookedKeys . Contains ( key ) )
9590 {
96- KeyEventArgs kea = new KeyEventArgs ( key ) ;
91+ var kea = new GlobalKeyEventArgs ( key , lParam . scanCode ) ;
9792 if ( ( wParam == WinAPI . WM_KEYDOWN || wParam == WinAPI . WM_SYSKEYDOWN ) && ( KeyDown != null ) )
9893 {
9994 KeyDown ( this , kea ) ;
@@ -110,4 +105,26 @@ private int hookProc(int code, int wParam, ref WinAPI.KeyboardHookStruct lParam)
110105 }
111106 #endregion
112107 }
108+
109+ /// <summary>Contains arguments for the KeyUp/KeyDown event in a <see cref="GlobalKeyboardListener"/>.</summary>
110+ public sealed class GlobalKeyEventArgs : EventArgs
111+ {
112+ /// <summary>The virtual-key code of the key being pressed or released.</summary>
113+ public Keys VirtualKeyCode { get ; private set ; }
114+ /// <summary>The scancode of the key being pressed or released.</summary>
115+ public int ScanCode { get ; private set ; }
116+ /// <summary>Set this to ‘true’ to prevent further processing of the keystroke (i.e. to ‘swallow’ it).</summary>
117+ public bool Handled { get ; set ; }
118+
119+ /// <summary>Constructor.</summary>
120+ public GlobalKeyEventArgs ( Keys virtualKeyCode , int scanCode )
121+ {
122+ VirtualKeyCode = virtualKeyCode ;
123+ ScanCode = scanCode ;
124+ Handled = false ;
125+ }
126+ }
127+
128+ /// <summary>Used to trigger the KeyUp/KeyDown events in <see cref="GlobalKeyboardListener"/>.</summary>
129+ public delegate void GlobalKeyEventHandler ( object sender , GlobalKeyEventArgs e ) ;
113130}
0 commit comments