예제 #1
0
 /// <summary>
 /// Safely removes the hook without throwing exception.
 /// </summary>
 private void SafeRemove()
 {
     if (this._hKeyboardHook != IntPtr.Zero)
     {
         UnsafeNativeMethods.UnhookWindowsHookEx(this._hKeyboardHook);
         this._keyboardProc  = null;
         this._hKeyboardHook = IntPtr.Zero;
         this._keyData       = Keys.None;
         this.OnStateChanged(new WindowsHookLiba.StateChangedEventArgs(this.State));
     }
 }
예제 #2
0
 /// <summary>
 /// Removes the keyboard hook for this application.
 /// </summary>
 public void RemoveHook()
 {
     if (this._hKeyboardHook != IntPtr.Zero)
     {
         if (!UnsafeNativeMethods.UnhookWindowsHookEx(this._hKeyboardHook))
         {
             // Failed to remove the hook. Throw a HookException
             int eCode = Marshal.GetLastWin32Error();
             throw new WindowsHookException(new Win32Exception(eCode).Message);
         }
         else
         {
             this._keyboardProc  = null;
             this._hKeyboardHook = IntPtr.Zero;
             this._keyData       = Keys.None;
             this.OnStateChanged(new WindowsHookLiba.StateChangedEventArgs(this.State));
         }
     }
 }
예제 #3
0
 /// <summary>
 /// Installs the keyboard hook for this application.
 /// </summary>
 public void InstallHook()
 {
     if (this._hKeyboardHook == IntPtr.Zero)
     {
         this._keyboardProc = new KeyboardHook.KeyboardMessageEventHandler(KeyboardProc);
         IntPtr hinstDLL = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);
         this._hKeyboardHook = UnsafeNativeMethods.SetWindowsHookEx(UnsafeNativeMethods.WH_KEYBOARD_LL, this._keyboardProc, hinstDLL, 0);
         if (this._hKeyboardHook == IntPtr.Zero)
         {
             // Failed to hook. Throw a HookException
             int eCode = Marshal.GetLastWin32Error();
             this._keyboardProc = null;
             //throw new WindowsHookException(new Win32Exception(eCode).Message);
         }
         else
         {
             this.OnStateChanged(new WindowsHookLiba.StateChangedEventArgs(this.State));
         }
     }
 }