Exemplo n.º 1
0
        private void SubscribeToGlobalKeyboardEvents()
        {
            // install Keyboard hook only if it is not installed and must be installed
            if (mKeyboardHookHandle == 0)
            {
                // See comment of this field. To avoid GC to clean it up.
                mKeyboardDelegate = KeyboardHookProc;

                // install hook
                //Marshal.GetHINSTANCE(typeof(KeyboardEventHookManager).Assembly.GetModules()[0]),
                mKeyboardHookHandle = NativeMethods.SetWindowsHookEx(
                    (int)HookEventEnum.WH_KEYBOARD_LL,
                    mKeyboardDelegate,
                    NativeMethods.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName),
                    0);

                // If SetWindowsHookEx fails.
                if (mKeyboardHookHandle == 0)
                {
                    // Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
                    int errorCode = Marshal.GetLastWin32Error();

                    // Initializes and throws a new instance of the Win32Exception class with the specified error.
                    throw new Win32Exception(errorCode);
                }
            }
        }
Exemplo n.º 2
0
        private void UnsunscribeFromGlobalKeyboardEvents()
        {
            if (mKeyboardHookHandle != 0)
            {
                // uninstall hook
                int result = NativeMethods.UnhookWindowsHookEx(mKeyboardHookHandle);

                // reset invalid handle
                mKeyboardHookHandle = 0;

                // Free up for GC
                mKeyboardDelegate = null;

                // if failed and exception must be thrown
                if (result == 0)
                {
                    // Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
                    int errorCode = Marshal.GetLastWin32Error();

                    // Initializes and throws a new instance of the Win32Exception class with the specified error.
                    throw new Win32Exception(errorCode);
                }
            }
        }
Exemplo n.º 3
0
 public static extern int SetWindowsHookEx(int idHook, HookProcessDelegate lpfn, IntPtr hMod, int dwThreadId);
Exemplo n.º 4
0
 public static extern int SetWindowsHookEx(int idHook, HookProcessDelegate lpfn, IntPtr hMod, int dwThreadId);