/// <summary> /// Installs both or one of mouse and/or keyboard hooks and starts rasing events /// </summary> /// <exception cref="Win32ErrorException">Any windows problem.</exception> public void Start() { // install Mouse hook only if it is not installed and must be installed if (hMouseHook == IntPtr.Zero) { // Create an instance of HookProc. mouseHookProcedure = new USER32.HookProc(MouseHookProc); //install hook hMouseHook = USER32.SetWindowsHookEx(WH.MOUSE_LL, mouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); //If SetWindowsHookEx fails. KERNEL32.CheckLastError(); if (hMouseHook == IntPtr.Zero) { //do cleanup Stop(false); throw new Win32ErrorException(); } } // install Keyboard hook only if it is not installed and must be installed if (hKeyboardHook == IntPtr.Zero) { // Create an instance of HookProc. keyboardHookProcedure = new USER32.HookProc(KeyboardHookProc); //install hook hKeyboardHook = USER32.SetWindowsHookEx(WH.KEYBOARD_LL, keyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); //If SetWindowsHookEx fails. KERNEL32.CheckLastError(); if (hKeyboardHook == IntPtr.Zero) { //do cleanup Stop(false); throw new Win32ErrorException(); } } }
/// <summary> /// Constructor /// </summary> public MouseHook() { this.mseCallbackDelegate = new USER32.HookProc(this.MouseCallback); }
/// <summary> /// Constructor /// </summary> public MouseHook() { this.mseCallbackDelegate = new USER32.HookProc(this.MouseCallback); }
/// <summary> /// Constructor /// </summary> public KeyboardHook() { this.kbdCallbackDelegate = new USER32.HookProc(this.KeyboardCallback); }