44//
55
66using System ;
7+ using System . Collections . Generic ;
78using System . ComponentModel ;
89using System . Diagnostics ;
910using System . Runtime . InteropServices ;
10- using System . Collections . Generic ;
1111
1212namespace SharedLibraryNG
1313{
14+
1415 public class KeyboardHook
1516 {
16- // ReSharper disable InconsistentNaming
17- [ DllImport ( "user32.dll" , CharSet = CharSet . Auto , SetLastError = true ) ]
18- [ return : MarshalAs ( UnmanagedType . Bool ) ]
19- public static extern bool PostMessage ( IntPtr hWnd , Int32 Msg , IntPtr wParam , HookKeyMsgData lParam ) ;
20- // ReSharper restore InconsistentNaming
17+
18+ internal static class NativeMethods
19+ {
20+ // ReSharper disable InconsistentNaming
21+ [ DllImport ( "user32.dll" , CharSet = CharSet . Auto , SetLastError = true ) ]
22+ [ return : MarshalAs ( UnmanagedType . Bool ) ]
23+ public static extern bool PostMessage ( IntPtr hWnd , Int32 Msg , IntPtr wParam , HookKeyMsgData lParam ) ;
24+ // ReSharper restore InconsistentNaming
25+ }
2126
2227 [ Flags ]
2328 public enum ModifierKeys
@@ -40,18 +45,35 @@ public enum ModifierKeys
4045 protected class KeyNotificationEntry
4146 : IEquatable < KeyNotificationEntry >
4247 {
43- public IntPtr WindowHandle ;
48+ private IntPtr WindowHandle ;
4449 public Int32 KeyCode ;
4550 public ModifierKeys ModifierKeys ;
4651 public Boolean Block ;
4752
53+ public KeyNotificationEntry ( IntPtr h , Int32 k , ModifierKeys m , Boolean b )
54+ {
55+ WindowHandle = h ;
56+ KeyCode = k ;
57+ ModifierKeys = m ;
58+ Block = b ;
59+ }
4860 public bool Equals ( KeyNotificationEntry obj )
4961 {
5062 return ( WindowHandle == obj . WindowHandle &&
5163 KeyCode == obj . KeyCode &&
5264 ModifierKeys == obj . ModifierKeys &&
5365 Block == obj . Block ) ;
5466 }
67+
68+ public IntPtr getWinHdl ( )
69+ {
70+ return WindowHandle ;
71+ }
72+
73+ public void setWinHdl ( IntPtr hdl )
74+ {
75+ WindowHandle = hdl ;
76+ }
5577 }
5678
5779 public const string HookKeyMsgName = "HOOKKEYMSG-{56BE0940-34DA-11E1-B308-C6714824019B}" ;
@@ -62,9 +84,12 @@ public static Int32 HookKeyMsg
6284 {
6385 if ( _hookKeyMsg == 0 )
6486 {
65- _hookKeyMsg = Win32 . RegisterWindowMessage ( HookKeyMsgName ) . ToInt32 ( ) ;
66- if ( _hookKeyMsg == 0 )
67- throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
87+ _hookKeyMsg = Win32 . NativeMethods . RegisterWindowMessage ( HookKeyMsgName ) . ToInt32 ( ) ;
88+ if ( _hookKeyMsg == 0 )
89+ {
90+ logger . Log . WarnFormat ( "_hookKeyMsg == 0" ) ;
91+ throw new System . InvalidOperationException ( "_hookKeyMsg == 0" ) ;
92+ }
6893 }
6994 return _hookKeyMsg ;
7095 }
@@ -104,7 +129,7 @@ private static void SetHook()
104129 var curProcess = Process . GetCurrentProcess ( ) ;
105130 var curModule = curProcess . MainModule ;
106131
107- var hook = Win32 . SetWindowsHookEx ( Win32 . WH_KEYBOARD_LL , LowLevelKeyboardProcStaticDelegate , Win32 . GetModuleHandle ( curModule . ModuleName ) , 0 ) ;
132+ var hook = Win32 . NativeMethods . SetWindowsHookEx ( Win32 . WH_KEYBOARD_LL , LowLevelKeyboardProcStaticDelegate , Win32 . NativeMethods . GetModuleHandle ( curModule . ModuleName ) , 0 ) ;
108133 if ( hook == IntPtr . Zero )
109134 throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
110135
@@ -115,7 +140,7 @@ private static void UnsetHook()
115140 {
116141 if ( _hook == IntPtr . Zero ) return ;
117142
118- Win32 . UnhookWindowsHookEx ( _hook ) ;
143+ Win32 . NativeMethods . UnhookWindowsHookEx ( _hook ) ;
119144 _hook = IntPtr . Zero ;
120145 }
121146
@@ -139,7 +164,7 @@ private static IntPtr LowLevelKeyboardProc(Int32 nCode, IntPtr wParam, Win32.KBD
139164
140165 if ( result != 0 ) return new IntPtr ( result ) ;
141166
142- return Win32 . CallNextHookEx ( _hook , nCode , wParam , lParam ) ;
167+ return Win32 . NativeMethods . CallNextHookEx ( _hook , nCode , wParam , lParam ) ;
143168 }
144169
145170 private static int OnKey ( Int32 msg , Win32 . KBDLLHOOKSTRUCT key )
@@ -152,7 +177,7 @@ private static int OnKey(Int32 msg, Win32.KBDLLHOOKSTRUCT key)
152177 // Mainly when the station is unlocked, or after an admin password is asked
153178 try
154179 {
155- if ( GetFocusWindow ( ) == notificationEntry . WindowHandle && notificationEntry . KeyCode == key . vkCode )
180+ if ( GetFocusWindow ( ) == notificationEntry . getWinHdl ( ) && notificationEntry . KeyCode == key . vkCode )
156181 {
157182 var modifierKeys = GetModifierKeyState ( ) ;
158183 if ( ! ModifierKeysMatch ( notificationEntry . ModifierKeys , modifierKeys ) ) continue ;
@@ -165,7 +190,7 @@ private static int OnKey(Int32 msg, Win32.KBDLLHOOKSTRUCT key)
165190 WasBlocked = notificationEntry . Block ,
166191 } ;
167192
168- if ( ! PostMessage ( notificationEntry . WindowHandle , HookKeyMsg , wParam , lParam ) )
193+ if ( ! NativeMethods . PostMessage ( notificationEntry . getWinHdl ( ) , HookKeyMsg , wParam , lParam ) )
169194 throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
170195
171196 if ( notificationEntry . Block ) result = 1 ;
@@ -185,12 +210,12 @@ private static int OnKey(Int32 msg, Win32.KBDLLHOOKSTRUCT key)
185210 private static IntPtr GetFocusWindow ( )
186211 {
187212 var guiThreadInfo = new Win32 . GUITHREADINFO ( ) ;
188- if ( ! Win32 . GetGUIThreadInfo ( 0 , guiThreadInfo ) )
213+ if ( ! Win32 . NativeMethods . GetGUIThreadInfo ( 0 , guiThreadInfo ) )
189214 {
190215 var except = Marshal . GetLastWin32Error ( ) ;
191216 throw new Win32Exception ( except ) ;
192217 }
193- return Win32 . GetAncestor ( guiThreadInfo . hwndFocus , Win32 . GA_ROOT ) ;
218+ return Win32 . NativeMethods . GetAncestor ( guiThreadInfo . getFocus ( ) , Win32 . GA_ROOT ) ;
194219 }
195220
196221 protected static Dictionary < Int32 , ModifierKeys > ModifierKeyTable = new Dictionary < Int32 , ModifierKeys >
@@ -214,7 +239,7 @@ public static ModifierKeys GetModifierKeyState()
214239
215240 foreach ( KeyValuePair < Int32 , ModifierKeys > pair in ModifierKeyTable )
216241 {
217- if ( ( Win32 . GetAsyncKeyState ( pair . Key ) & Win32 . KEYSTATE_PRESSED ) != 0 ) modifierKeyState |= pair . Value ;
242+ if ( ( Win32 . NativeMethods . GetAsyncKeyState ( pair . Key ) & Win32 . KEYSTATE_PRESSED ) != 0 ) modifierKeyState |= pair . Value ;
218243 }
219244
220245 if ( ( modifierKeyState & ModifierKeys . LeftWin ) != 0 ) modifierKeyState |= ModifierKeys . Win ;
@@ -239,13 +264,7 @@ public static void RequestKeyNotification(IntPtr windowHandle, Int32 keyCode, Bo
239264
240265 public static void RequestKeyNotification ( IntPtr windowHandle , Int32 keyCode , ModifierKeys modifierKeys = ModifierKeys . None , Boolean block = false )
241266 {
242- var newNotificationEntry = new KeyNotificationEntry
243- {
244- WindowHandle = windowHandle ,
245- KeyCode = keyCode ,
246- ModifierKeys = modifierKeys ,
247- Block = block ,
248- } ;
267+ var newNotificationEntry = new KeyNotificationEntry ( windowHandle , keyCode , modifierKeys , block ) ;
249268
250269 foreach ( var notificationEntry in NotificationEntries )
251270 if ( notificationEntry == newNotificationEntry ) return ;
@@ -260,15 +279,9 @@ public static void CancelKeyNotification(IntPtr windowHandle, Int32 keyCode, Boo
260279
261280 public static void CancelKeyNotification ( IntPtr windowHandle , Int32 keyCode , ModifierKeys modifierKeys = ModifierKeys . None , Boolean block = false )
262281 {
263- var notificationEntry = new KeyNotificationEntry
264- {
265- WindowHandle = windowHandle ,
266- KeyCode = keyCode ,
267- ModifierKeys = modifierKeys ,
268- Block = block ,
269- } ;
270-
271- NotificationEntries . Remove ( notificationEntry ) ;
282+ var notificationEntry = new KeyNotificationEntry ( windowHandle , keyCode , modifierKeys , block ) ;
283+
284+ NotificationEntries . Remove ( notificationEntry ) ;
272285 }
273286 }
274287}
0 commit comments